1、实例demo是用谷歌浏览器在百度首页搜python的动作脚本;
BaiDu_test是指脚本中自己定义的类名,test_get是指你的类中定义的testcase方法。
在if name函数下加入testcase时,python3.6版本的方法是
suiter = addtest(类名('测试方法'))
suiter = addTest(BaiDu_test("test_get"))
from selenium import webdriver import unittest import time from HTMLTestRunner import HTMLTestRunner class BaiDu_test(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.url = "https://www.baidu.com/" self.driver.implicitly_wait(10) self.driver.maximize_window() def test_get(self): driver = self.driver driver.get(self.url) print("当前网址"+driver.current_url) time.sleep(5) driver.find_element_by_id("kw").send_keys("python") time.sleep(2) driver.find_element_by_id("su").click() def tardown(self): #清除缓存 self.driver.refresh() self.driver.quit() if __name__ == "__main__": suite = unittest.TestSuite() suite.addTest(BaiDu_test('test_get')) sop = open(r'D:\test_02.html','wb') runner = HTMLTestRunner(stream=sop, title="asd", description='asdasd') runner.run(suite)
2、在python3.7版本中是以引用方法加入用例
suiter = addtest(类名 . 测试方法())
suiter = addTest(myclass.testcase())
from selenium import webdriver import unittest import time from HTMLTestRunner import HTMLTestRunner class BaiDu_test(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.url = "https://www.baidu.com/" self.driver.implicitly_wait(10) self.driver.maximize_window() def test_get(self): driver = self.driver driver.get(self.url) print("当前网址"+driver.current_url) time.sleep(5) driver.find_element_by_id("kw").send_keys("python") time.sleep(2) driver.find_element_by_id("su").click() def tardown(self): #清除缓存 self.driver.refresh() self.driver.quit() if __name__ == "__main__": suite = unittest.TestSuite() suite.addTest(BaiDu_test.test_get()) sop = open(r'D:\test_02.html','wb') runner = HTMLTestRunner(stream=sop, title="asd", description='asdasd') runner.run(suite)