__author__='Administrator'#Coding=utf-8 fromSeleniumImportWebdriver fromSelenium.webdriver.common.byImport by fromSelenium.webdriver.common.keysImportKeys fromSelenium.webdriver.support.uiImportSelect fromSelenium.common.exceptionsImportnosuchelementexceptionImportUnitTest, time, reImportHtmltestrunner#Introducing the Htmltestrunner packageclassBaidu (unittest. TestCase):defsetUp (self): Self.driver=Webdriver. Firefox () self.driver.implicitly_wait (30) Self.base_url="http://www.baidu.com/"self.verificationerrors=[] Self.accept_next_alert=TruedefTest_baidu_search (self): driver=self.driver driver.get (Self.base_url+"/") driver.find_element_by_id ("kw"). Send_keys ("163") driver.find_element_by_id ("su"). Click () time.sleep (2) Driver.close ()#Baidu set up use cases defTest_baidu_set (self): driver=Self.driver#go to the Search Settings pageDriver.get (Self.base_url +"/gaoji/preferences.html") #Set search results per page to 100Driver.implicitly_wait (30) M=driver.find_element_by_xpath (".//*[@id = ' Gxsz ']/tbody/tr[4]/td[2]/select") M.find_element_by_xpath ("//option[@value = ' + ')"). Click () time.sleep (5) #saving information for settingsDriver.find_element_by_xpath (".//*[@id = ' Gxszbutton ']/input[1]"). Click () time.sleep (2) Driver.switch_to_alert (). Accept ()defTearDown (self): Self.driver.quit () self.assertequal ([], self.verificationerrors)if __name__=="__main__": #Define a unit test Containertestunit=UnitTest. TestSuite ()#Adding test cases to the test ContainerTestunit.addtest (Baidu ("Test_baidu_search")) Testunit.addtest (Baidu ("Test_baidu_set")) #define a report storage path that supports relative pathsfilename ='d:\\report\\result.html'FP= File (filename,'WB') #Defining test ReportsRunner =Htmltestrunner.htmltestrunner (Stream=FP, title=u'Baidu Search test Report', Description=u'use case execution:') #Run test CasesRunner.run (Testunit)
"Selenium" Htmltestrunner test report generation