This article mainly implements the selenium automated test Baidu search + Youdao translation Simple test case execution, and through the HTML Testrunner generates an HTML test report. This is the first Htmltestrunner script, based on the Python3 script, that was followed shortly after learning the video.
Test_baidu,py
#Coding=utf-8ImportUnitTestImportHtmltestrunnerImport Time#define a directory for test file lookupsTest_dir='E:\\pycode\\unittest_baidu\\test_case'#define parameters for the Discover methodDiscover=unittest.defaulttestloader.discover (test_dir, pattern ='test_b*.py', top_level_dir=None) now=time.strftime ('%y_%m_%d%h_%m_%s')Print(now)#define a report storage pathFilename='e:\\pycode\\unittest_baidu\\report\\'+now+'result.html'FP=open (filename,'WB')if __name__=='__main__': #Defining test ReportsRunner = Htmltestrunner.htmltestrunner (STREAM=FP, Title=u'Baidu Search test Report', Description=u'use case Execution') #Run test CasesRunner.run (Discover)#Close Report FileFp.close ()
test_youdao.py
#Coding=utf-8 fromSeleniumImportWebdriverImportUnitTest fromTimeImportSleepclassMyTest (unittest. TestCase):defsetUp (self): Self.driver=Webdriver. Firefox () Self.driver.maximize_window () Self.base_url="http://www.youdao.com"Sleep (5) defTest_youdao (self): driver=self.driver driver.get (Self.base_url+"/") driver.find_element_by_id ("translatecontent"). Clear () driver.find_element_by_id ("translatecontent"). Send_keys ("Webdriver") driver.find_element_by_id ("translatecontent"). Submit () Sleep (2) Title=driver.title self.assertequal (U"what does "Webdriver" mean _ English Webdriver Translation _ Phonetic Transcription _ pronunciation _ Usage _ _ _ _", title)defTearDown (self): Self.driver.quit ()if __name__=="__main__": Unittest.main ()
all_test.py
#Coding=utf-8ImportUnitTestImportHtmltestrunnerImport Time#define a directory for test file lookupsTest_dir='E:\\pycode\\unittest_baidu\\test_case'#define parameters for the Discover methodDiscover=unittest.defaulttestloader.discover (test_dir, pattern ='test_*.py', top_level_dir=None) now=time.strftime ('%y_%m_%d%h_%m_%s')Print(now)#define a report storage pathFilename='e:\\pycode\\unittest_baidu\\report\\'+now+'result.html'FP=open (filename,'WB')if __name__=='__main__': #Defining test ReportsRunner = Htmltestrunner.htmltestrunner (STREAM=FP, Title=u'Baidu Search test Report', Description=u'use case Execution') #Run test CasesRunner.run (Discover)#Close Report FileFp.close ()
The script can run normally, if it fails, check if the environment configuration is OK, if the browser driver is installed correctly.
Selenium Baidu search + Youdao translation Simple testcase execution-----use case report (Htmltestrunner)