Python3-selenium自动化测试框架,结合HTMLTestRunner测试报告!
import unittest
from selenium import webdriver
from time import sleep
from HTMLTestRunner import HTMLTestRunner
class TestUntitled(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "http://www.google.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_untitled(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("lst-ib").click()
driver.find_element_by_id("lst-ib").clear()
driver.find_element_by_id("lst-ib").send_keys("Selenium")
driver.find_element_by_name("btnK").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to.alert()
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert.text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
def suite():
suite = unittest.TestSuite()
suite.addTest(TestUntitled("test_untitled"))
return suite
if __name__ == "__main__":
runner = unittest.TextTestRunner()
runner.run(suite())
with open('test_report.html', 'wb') as f:
runner = HTMLTestRunner(stream=f, title='Google Search Test', description='Test case for Google Search')
runner.run(suite())
这段代码修复了原代码中的一些问题,并添加了HTMLTestRunner来生成测试报告。在测试用例中,我们打开Google首页,点击搜索框,清除默认文本并输入“Selenium”,然后点击搜索按钮。在测试结束后,我们使用HTMLTestRunner生成了一个HTML格式的测试报告。这个报告将被保存为test_report.html
文件。
评论已关闭