Today in the advanced application of automated testing, the first one is to generate an HTML test report, because the name of the test report is not set, so the runtime will overwrite the original report without manually modifying the previous report name before running the test. This is obviously a hassle, and the best solution is to include the current time in the report name so that the generated reports will not overlap and will be clearer about when the report was generated.
But in the process of running the code, an error occurred:
Traceback (most recent):
File "d:/python/selenium/test_baidu.py", line at <module>
fp = open (filename, ' WB ')
OSError: [Errno] Invalid argument: './2017-07-25 16:52:00result.html '
At first glance I thought it was something that was going on, and I didn't feel a problem checking my code carefully. Thinking for a long time, suddenly realized that I set the time of the file name to now = Time.strftime ("%y-%m-%d%h:%m:%s")
The file name is not allowed to appear similar to/\: such as special characters. In a moment I was clear, immediately corrected, and again allowed, sure enough success.
From selenium import Webdriver
Import UnitTest
Import time
From Htmltestrunner import Htmltestrunner
Class Baidu (UnitTest. TestCase):
"Baidu Search Test"
def setUp (self):
Self.driver = Webdriver. Firefox ()
Self.driver.implicitly_wait (10)
Self.base_url = "http://www.baidu.com/"
def test_baidu_search (self):
"Keyword search: Htmltestrunner"
Driver = Self.driver
Driver.get (Self.base_url)
driver.find_element_by_id ("kw"). Send_keys ("Htmltestrunner")
driver.find_element_by_id ("su"). Click ()
def tearDown (self):
Self.driver.quit ()
if __name__ = = "__main__":
Testunit = UnitTest. TestSuite ()
Testunit.addtest (Baidu ("Test_baidu_search"))
#按照一定格式获取当前时间
Now = Time.strftime ("%y-%m-%d%h_%m_%s") #一定不要包括禁止使用的文件名特殊符号
#定义报告存放路径
filename = './' + now + ' result.html '
fp = open (filename, ' WB ')
#定义测试报告
Runner = Htmltestrunner (STREAM=FP,
Title= ' Baidu Search test Report ',
description= ' use case execution: ')
Runner.run (Testunit) #运行测试用例
Fp.close () #关闭报告文件
Selenium2 + Python3.6 Combat (V): Generate an HTML test report Invalid argument