1 #-*-coding:utf-8-*-2 ImportHtmltestreport3 ImportHtmltestrunner4 ImportOS5 ImportSYS6 Import Time7 ImportUnitTest8 fromSeleniumImportWebdriver9 Ten One classBaidu (unittest. TestCase): A defsetUp (self): -Self.driver =Webdriver. Firefox () -Self.driver.implicitly_wait (30) the Self.driver.maximize_window () - #Self.base_url = "https://www.baidu.com" - #self.driver.get (Self.base_url) -Self.driver.get ("https://www.baidu.com") + - deftest_case1 (self): + """Design test Failure case""" #* * * * The effect is to show the test name in the test report * * * * A Print("======== "case_0001" open Baidu search =============") at #current_time = Time.strftime ("%y-%m-%d-%h-%m-%s", Time.localtime (Time.time ())) - # "." Indicates that the path created is the address where the. py file is located, \ \ is escaped with \ - #Pic_path = '. \\result\\image\\ ' + current_time + '. png ' -Current_time = Time.strftime ("%y-%m-%d-%h_%m_%s", Time.localtime (Time.time ())) -Pic_path ='. \\result\\image\\'+'2017-07-17\\'+ Current_time +'. PNG' - Print(Pic_path)#Print the address of the picture inTime.sleep (2) -Self.driver.save_screenshot (Pic_path)#, get test results toSelf.assertequal ('Baidu a bit, you will know', Self.driver.title)#assert whether the test was successful and determine if the title is Baidu (case of design failure) + - defTest_case2 (self): the """Case for error during design test""" * Print("======== "case_0002" Search Selenium =============") $SELF.DRIVER.FIND_ELEMENT_BY_ID ("kw"). Clear ()Panax NotoginsengSELF.DRIVER.FIND_ELEMENT_BY_ID ("kw"). Send_keys (U"Selenium") -SELF.DRIVER.FIND_ELEMENT_BY_ID ('su'). Click () theTime.sleep (2) + #current_time = Time.strftime ("%y-%m-%d-%h-%m-%s", Time.localtime (Time.time ())) ACurrent_time = Time.strftime ("%y-%m-%d-%h_%m_%s", Time.localtime (Time.time ())) the # "." Indicates that the path created is the address where the. py file is located, \ \ is escaped with \ +Pic_path ='. \\result\\image\\'+'2017-07-17\\'+ Current_time +'. PNG' - Print(Pic_path)#Print the address of the picture $Time.sleep (2) $Self.driver.save_screenshot (Pic_path)#, get test results -Self.assertin ('Selenium', Self.driver.title)#assertion writing error, resulting in case error - the deftest_case3 (self): - """Case for design test success"""Wuyi Print("======== "case_0003" Search Dream Rainfall blog =============") theSELF.DRIVER.FIND_ELEMENT_BY_ID ("kw"). Clear () -SELF.DRIVER.FIND_ELEMENT_BY_ID ("kw"). Send_keys (U"Dream Rainfall") WuSELF.DRIVER.FIND_ELEMENT_BY_ID ('su'). Click () - #current_time = Time.strftime ("%y-%m-%d-%h-%m-%s", Time.localtime (Time.time ())) AboutCurrent_time = Time.strftime ("%y-%m-%d-%h_%m_%s", Time.localtime (Time.time ())) $ # "." Indicates that the path created is the address where the. py file is located, \ \ is escaped with \ -Pic_path ='. \\result\\image\\2017-07-17\\'+ Current_time +'. PNG' - Print(Pic_path)#Print the address of the picture -Time.sleep (2) ASelf.driver.save_screenshot (Pic_path)#, get test results + theSelf.assertin ('Dream Rainfall', Self.driver.title) - $ defTearDown (self): the self.driver.quit () the the the if __name__=="__main__": - " "Generate test Reports" " inCurrent_time = Time.strftime ("%y-%m-%d-%h_%m_%s", Time.localtime (Time.time ())) theTestunit =UnitTest. TestSuite () theTestunit.addtest (Baidu ("test_case1")) AboutTestunit.addtest (Baidu ("Test_case2")) theTestunit.addtest (Baidu ("TEST_CASE3")) theReport_path =". \\result\\SoftTestReport_"+ Current_time +'. html' #to generate a test report path thefp = open (Report_path,"WB") +Runner = Htmltestreport.htmltestrunner (STREAM=FP, Title=u"Automated test reports", description='Automated Test Demo report', tester='FYR') -# runner = Htmltestrunner.htmltestrunner (STREAM=FP, Title=u"Automated test reports", description='Automated Test Demo report') the Runner.run (testunit)BayiFp.close ()
Precautions:
1. The format for getting the current time is:
%y-%m-%d-%h_%m_%s, not%y-%m-%d-%h-%m-%s.
2. Fill in the storage address "\", to be escaped with the escape character "\", to "\ \"
3.runner = Htmltestreport.htmltestrunner (STREAM=FP, title=u "Automated test Report", description= ' Automated Test Presentation report ', tester= ' FYR ') The test report:
4.runner = Htmltestrunner.htmltestrunner (STREAM=FP, title=u "Automated test Report", description= ' Automated Test Demo report '):
Python+selenium test Report (2)