Selenium+python's HTML test report

Source: Internet
Author: User

R First, prepare

1, the module--htmltestrunner

1) Download:: http://tungwaiyip.info/software/HTMLTestRunner.html

right mouse button → save target as, saved to local.

2) Install: Copy the downloaded htmltestrunner.py file to the Python installation directory

... \python36\lib

3) Verification: the introduction of the Htmltestrunner module in Python interactive mode, if the system did not error, the added success

2, modify the Htmltestrunner

Because Htmltestrunner is developed based on Python2, it is necessary to modify some of its content if it is used in a Python3 environment. Use notepad++ to open the htmltestrunner.py file and modify the following:

Line 94th, change import Stringio to import IO

Line No. 539, modify Self.outputbuffer = Stringio.stringio () to self.outputbuffer= io. Stringio ()

Line No. 631, change print >> sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime) to print (Sys.stderr, ' \ ntimeelapsed:%s '% (self.stoptime-self.starttime))

Line No. 642, if not Rmap.has_key (CLS): modified to If not CLS in Rmap:

Line No. 766, change uo = O.decode (' latin-1 ') to UO = E

Line NO. 772, change UE = E.decode (' latin-1 ') to UE = E


Second, generate HTML test report

Take Baidu Search as an example to generate an HTML test report

1. Code implementation

#-*-coding:utf-8-*- fromSeleniumImportWebdriver fromHtmltestrunnerImportHtmltestrunnerImportUnittest,timeclassbaiduidetest (unittest. TestCase):defsetUp (self): Self.driver=Webdriver. Firefox () self.driver.implicitly_wait (30) Self.base_url="https://www.baidu.com/"        deftest_baidu_ide (self): driver=self.driver driver.get (self.base_url) driver.find_element_by_id ("kw"). Clear () driver.find_element_by_id ("kw"). Send_keys ("Htmltestrunner") driver.find_element_by_id ("su"). Click () time.sleep (5) self.assertequal (U"htmltestrunner_ Baidu Search", Driver.title)defTearDown (self): Self.driver.quit ()if __name__=="__main__":    #Constructing test SuitesTestsuit =UnitTest. TestSuite () testsuit.addtest (Baiduidetest ("Test_baidu_ide"))    #defining test Report storage Pathsfp = open ('./result.html','WB')    #Defining test ReportsRunner = Htmltestrunner (stream=FP, title='Automated test reports', Description='use case execution:') Runner.run (testsuit)#Close test ReportFp.close ()

2. Code Analysis

1) Import the Htmltestrunner module by importing it in.

2) Open the RESULT.HEML in the current directory with the binary write mode (' WB ') via the Open () method, and if not, create it automatically.

3) Call the Htmltestrunner class under the Htmltestrunner module.

Stream specifies the test report file

Title defines the caption of the test report

Description defining the subtitle of a test report

4) Run the test case in the test suite via the Htmltestrunner Run () method

5) Close the test report file.

3. Code effect

4. Tips

The code executes when the mouse is placed on "if __name__ = =" __main__ ":" Right-click "Run ' Baidu_ide_text '" instead of "Run untitest in Baidu_ide_text", the test report cannot be generated

Iii. easier-to-read test reports

Enhance the scalability of your test reports with comments from the doc string type of Python.

Python code comments are divided into two types: Commen (General comment #) and Doc string (describing functions, classes, and methods in three quotation marks)

1. Code implementation

......classbaiduidetest (unittest. TestCase):" "Baidu Search Test" "    defsetUp (self): Self.driver=Webdriver. Firefox () self.driver.implicitly_wait (30) Self.base_url="https://www.baidu.com/"        deftest_baidu_ide (self):" "Search Keywords" "Driver=self.driver driver.get (self.base_url) driver.find_element_by_id ("kw"). Clear () driver.find_element_by_id ("kw"). Send_keys ("Htmltestrunner") driver.find_element_by_id ("su"). Click () time.sleep (5) self.assertequal (U"htmltestrunner_ Baidu Search", Driver.title) ... ..

2. Code effect

Iv. name of the test report

Using the time module of Python, add the recognition to the report name by adding the current Times Enhancement test report file.

1. Knowledge Preparation

Time.time (): Gets the current timestamp

Time.ctime (): String form of the current time

Time.location (): The Struct_time form of the current time

Time.strftime (): Gets the current time, you can format the time string

Common formatting symbols:

%Y: The year with the century, 2017

%y: The year without the century, 17

%m: Month

%d: Days

Hours of%h:24 hours

%M: Min

%s: Seconds

2. Code implementation

......if __name__=="__main__":    #Constructing test SuitesTestsuit =UnitTest. TestSuite () testsuit.addtest (Baiduidetest ("Test_baidu_ide"))        #get the current time in a certain formatnow = Time.strftime ("%y%m%d_%h%m%s")    #Add the current time to the report file namefilename ='./'+now+'result.html'    #defining test Report storage Pathsfp = open (filename,'WB')    #Defining test ReportsRunner = Htmltestrunner (stream=FP, title='Automated test reports', Description='use case execution:') Runner.run (testsuit)#Close test ReportFp.close ()

3. Code effect

V. Project Integration Test Report

Integrate the above content into the project's runtest.py file to make it work across the test project

1. Code implementation

#-*-coding:utf-8-*- fromSeleniumImportWebdriverImportUnitTest, timeclassyoudaoidetest (unittest. TestCase):" "Youdao Translation Test" "    defsetUp (self): Self.driver=Webdriver. Firefox () self.driver.implicitly_wait (30) Self.base_url="http://www.youdao.com/"        deftest_youdao_ide (self):" "Chinese-English translation test" "Driver=self.driver driver.get (self.base_url) driver.find_element_by_id ("translatecontent"). Clear () driver.find_element_by_id ("translatecontent"). Send_keys (U"Automated test reports") Driver.find_element_by_css_selector ("Button"). Click () time.sleep (5) self.assertequal (U""Automated test Report"", Driver.title)defTearDown (self): Self.driver.quit ()if __name__=="__main__": Unittest.main ()

2. Code effect

Selenium+python's HTML test report

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.