白月黑羽教python之selenium:课后练习作业
程序员文章站
2022-05-22 07:51:26
...
白月黑羽教python之selenium:课后练习
最近自学了白月黑羽老师的自动化测试课程,并非课后作业的准确答案,只是为了个人学习记录,如有不足欢迎留言指点,谢谢大家。
测试用例截图
个人完成代码
"""
本段代码为测试’tcs-selenium.xlsx‘文件中管理员登录测试用例
测试用例内容包含UI-0001、UI-0002、UI-0003、UI-0004、UI-0005
"""
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
# 隐式等待最大10秒
driver.implicitly_wait(10)
# 打开待测试环境
driver.get('http://127.0.0.1/mgr/sign.html')
class Input_Info():
def __init__(self, username, password):
self.username = username
self.password = password
def test_username_password_input(self):
# 找到用户名输入框输入
element = driver.find_element_by_id('username')
element.clear()
element.send_keys(self.username)
# 找到密码输入框输入
element = driver.find_element_by_id('password')
element.clear()
element.send_keys(self.password)
# 找到登录按钮,并点击
elementbutton = driver.find_element_by_tag_name('button')
elementbutton.click()
class Check_test_case_function(Input_Info):
def __init__(self, username, password, case_number, expected_result):
super(Check_test_case_function, self).__init__(username, password)
self.case_number = case_number
self.expected_result = expected_result
def check_result(self):
# 获取alert弹出框
# 判断是否有弹出框
try:
sleep(2)
alert = driver.switch_to.alert
text = alert.text
alert.accept()
except Exception as e:
return e
# 实际结果
actual_result = text
print('页面提示错误信息为: ', actual_result)
# 预期结果
print('预期结果为: ', self.expected_result)
# 通过try抛出异常进行断言判断
try:
assert self.expected_result == actual_result
print('%s PASS,实际结果与预期结果一致!' % self.case_number)
except Exception as e:
print('%s FAIL,实际结果与预期结果不一致!' % self.case_number, format(e))
driver.refresh()
if __name__ == '__main__':
def test_case_function(cls, username, password, case_number, expected_result):
result = cls(username, password, case_number, expected_result)
result.test_username_password_input()
result.check_result()
test_case_function(Check_test_case_function, '', '88888888', 'UI_01', '请输入用户名')
test_case_function(Check_test_case_function, 'byhy', '', 'UI_02', '请输入密码')
test_case_function(Check_test_case_function, 'byh', '88888888', 'UI_03', '登录失败 : 用户名或者密码错误')
test_case_function(Check_test_case_function, 'byhy', '8888888', 'UI_04', '登录失败 : 用户名或者密码错误')
test_case_function(Check_test_case_function, 'byhy', '888888888', 'UI_05', '登录失败 : 用户名或者密码错误')
上一篇: APP端跟PHP端进行数据互通