测试用例模块化特点:为po模型做准备。
1、提取公共方法。
2、提取数据。
3、提取逻辑。
# @Author:lsj # @version V1.0 # -*- coding:UTF-8 -*- import unittest from selenium import webdriver from time import sleep from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import HTMLTestRunner def openBroswer(): driver = webdriver.Chrome() return driver def openUrl(driver, url): driver.get(url) driver.maximize_window() # 元素是否存在 def ele_is_presence(driver,tuple_arg): return WebDriverWait(driver, 10).until(EC.presence_of_element_located(tuple_arg)) #点击操作 def ele_click(driver,tuple_arg): ele_is_presence(driver,tuple_arg).click() # 发送文本 def ele_sendKeys(driver,tuple_arg,keyword): ele_is_presence(driver,tuple_arg).send_keys(keyword) # 检查登录结果 def checkResult(driver, loginfo): try: ele_is_presence(driver, loginfo) print ("登录状态验证通过!") except: print ("登录失败!") class Test_Login(unittest.TestCase): def testm1(self): # 用例模式一 url = 'http://daxue.qysxy.com.cn/admin/static/front/html/login.html' account=['fuguang'] pwd=['123456'] c = ['1111'] username=(By.NAME,"username") # 用户名输入框 password=(By.NAME,"password") # 密码输入框 code = (By.ID,"code") # 验证码输入框 loginbtn = (By.ID, 'loginBtn') # 登录按钮 # 实例化driver driver = openBroswer() # 打开url openUrl(driver, url) # 输入用户名 ele_sendKeys(driver,username,account) # 输入密码 ele_sendKeys(driver,password,pwd) # 输入验证码 ele_sendKeys(driver,code,c) # 点击登录按钮 ele_click(driver,loginbtn) print("模式一执行完成!!") def testm2(self): # 用例模式二 url = "http://daxue.qysxy.com.cn/admin/static/front/html/login.html" username = (By.NAME, 'username')# 用户名输入框 password = (By.NAME, 'password')# 密码输入框 code = (By.ID, "code") # 验证码输入框 loginbtn = (By.ID, 'loginBtn') # 登录按钮 driver=openBroswer() openUrl(driver,url) sleep(3) # 输入用户名 ele_sendKeys(driver,username,'fuguang') # 输入密码 ele_sendKeys(driver,password,'123456') # 输入验证码 ele_sendKeys(driver,code,'1111') # 点击登录按钮 ele_click(driver,loginbtn) print("模式二执行完成!!") sleep(3) driver.quit() if __name__ == '__main__': # Test_Login() # 测试报告 testCase = unittest.TestLoader().loadTestsFromTestCase(Test_Login) # 将多个测试类加载到测试集合中 suite = unittest.TestSuite([testCase]) # 原始版本 f = open("report.html", "wb") runner = HTMLTestRunner.HTMLTestRunner(stream=f, title="测试报告", description="测试报告描述") runner.run(suite)
出测试报告