【selenium】selenium+python+unittest实现生成测试报告
以慕课网https://www.imooc.com/为例
1,loginSuccess.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | # testLogin 2022-05-17 by Pearl # coding:utf-8 import unittest from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.keys import Keys import time import sys reload(sys) sys.setdefaultencoding( 'utf8' ) class LoginSuccessTestCase(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() #需要提前下载chromedriver.exe,存放路径:python/Scripts或Chrome(‘这里写出另外存放的路径’) self.driver.maximize_window() self.driver. get ( "https://www.imooc.com/" ) self.driver.implicitly_wait(15) self.expect_result = u '我的课程' # 登录前的cookie cookie1 = self.driver.get_cookies() print( 'cookie11' , cookie1) def test_login(self): self.driver.find_element_by_id( 'js-signin-btn' ).click() time.sleep(10) uName_tag = self.driver.find_element_by_name( "email" ) uPwd_tag = self.driver.find_element_by_name( "password" ) uName_tag.click() uName_tag.send_keys( 'your username' ) time.sleep(5) uPwd_tag.click() uPwd_tag.send_keys( 'your password' ) time.sleep(5) self.driver.find_element_by_class_name( 'xa-login' ).click() time.sleep(10) # 获取登录后的cookie 方便跳过验证码 cookie2 = self.driver.get_cookies() print( 'cookie2' , cookie2) result = self.driver.find_element_by_xpath( "//div[@id='login-area']/ul/li[4]/a/span" ).text # 断言 self.assertEqual(result, self.expect_result) def tearDown(self): self.driver.close() if __name__ == '__main__' : unittest.main() |
2,runner.py,HTMLTestRunner下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # coding:utf-8 import unittest import HTMLTestRunner #导入HTMLTestRunner,存放路径:python/Scripts from time import strftime, localtime, time from loginSuccess import * from loginFail import * import sys reload(sys) sys.setdefaultencoding( 'utf8' ) # suite = unittest.TestSuite() suite1 = unittest.TestLoader().loadTestsFromTestCase(LoginSuccessTestCase) suite2 = unittest.TestLoader().loadTestsFromTestCase(LoginFailTestCase) suite = unittest.TestSuite([suite1, suite2]) # 把测试用例添加到测试容器中 now = strftime( "%Y-%m-%M-%H_%M_%S" , localtime()) # 获取当前时间 filename = now + "test.html" # 文件名 fp = file(filename, 'wb' ) runner = HTMLTestRunner.HTMLTestRunner( stream=fp, verbosity=2, title= "测试报告" , description= "测试报告的详情" ) runner.run(suite) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构