Selenium+python Automated 81-html Report Optimization (pie chart + failed re-run + compatible python2&3) "Reprint"

Source: Internet
Author: User

Optimize HTML reporting

In order to meet the needs of the small partners of the various perverts, in order to install forced to increase the force lattice, in order to make the report bigger, the test report made the following optimizations:

    • Test report Chinese display, optimize some assertion failure body garbled problem
    • Added errors and failures to display in HTML reports
    • Optimize click to enlarge not clear problem
    • Increase pie chart Statistics
    • Retry function after failure
    • Compatible with python2.x and 3.x
Report effect

1. The results of the test report generated, such as the default display of errors and exceptions, the use cases of failed retries will also be counted.

2. Click on the display, you can directly display the captured pictures, no need to save to the local

Table tables

1. Modify the table's TD back content, you can customize the table name

2.drawCircle This back is a pie chart feature

<Tr Id=' Header_row '><Td> test Group/test case</Td><Total td></Td><Td> through</Td><Td> failure</Td><Td> Error</Td><Td> View</Td><Td> Error</Td></tr>% (test_list) s<Tr Id=' Total_row '><Td> Statistics</Td><td>% (count) s</Td><td>% (Pass) s</Td><td>% (fail) s</Td><td>% (Error) s</Td><Td>&nbsp;</Td><Td>&nbsp; </td></tr></table><script> drawcircle( %(Pass) s, %(fail) s, %(error) s)</script>  
Abnormal

1. This is the use case after the failure, will automatically, the image is stored in the HTML report base64, do not need to save to the local

Driver.get_screenshot_as_base64 ()

    Def Adderror(Self, test, Err):Self.error_count+=1Self.status=1 Testresult.adderror (Self, test, err) _, _exc_str=self.errors[-1] Output=Self.complete_output ()Self.result.append ((2, test, output, _EXC_STR))Try:driver=getattr (test,  "driver") test.img = Driver.get_screenshot_as_base64 () except  AttributeError:test.img =  "if self.verbosity > 1:sys.stderr.write ( ' E ') sys.stderr.write (str (test)) Sys.stderr.write ( Span class= "St" > "\nelse:sys.stderr.write ( ' E ')             

2. Test cases must define driver parameters, such as:

Driver = Webdriver. Firefox ()

Failed retry

1. The parameters of the generated report add a parameter Retry=1, which indicates that the use case fails and will run again.

# Coding:utf-8Import htmltestrunner_jpgImport UnitTestIf__name__ ==  "__main__": Discover Span class= "OP" >= unittest.defaultTestLoader.discover ( "case",  "test*.py") print (Discover) Run = Htmltestrunner_jpg. Htmltestrunner (Title= "can be loaded test report", Description= "test results", Stream=open ( Span class= "St" > "result.html",  "WB", Verbosity=2, Retry=1) Run.run (Discover)  

2.verbosity=2 This parameter is the console display test result style, as follows:

ETEST_01 (Pject. test_jpg. TEST1)Retesting ... 1ETEST_01 (Pject. test_jpg. TEST1)FTest_02 (Pject. test_jpg. TEST1)Retesting ... 1FTest_02 (Pject.test_jpgok test_03 ( pject.test_jpg ok test_01 ( pject.test_xxx ok test_02 ( pject.test_xxx time elapsed:0 :00:17.892222      
Execute use case

1. This is the reference test case, I did not generate a test report in the use case, generate the test report using the above batch execution, a separate script execution

# Coding:utf-8From seleniumImport WebdriverImport UnitTestClass Test1(UnitTest. TestCase):U ' ' Blog Park test ' @classmethodDef SetupClass(CLS): Cls.driver= Webdriver. Firefox () @classmethodDef Teardownclass(CLS): Cls.driver.quit ()Def Test_01(Self):U "" "Position failure Case" ""Self.driver.get ("Https://www.baidu.com")SELF.DRIVER.FIND_ELEMENT_BY_ID (' xxxxx '). Send_keys (U ' Baidu a bit ')SELF.DRIVER.FIND_ELEMENT_BY_ID (' Su '). Click ()Self.asserttrue (True)Def Test_02(Self):U ' failure case 'Self.driver.get ("http://www.cnblogs.com/yoyoketang/") t=Self.driver.titleSelf.assertin (u "failure case", T) def test_03(self): u ' through use case ' self.driver.get ("http://www.cnblogs.com/yoyoketang/") Self.assertin (u "Shanghai",self.driver.title)if __ name__ = = "__main__": Unittest.main ()           
Refer to the Great god GitHub

Great God GitHub point here

Optimized after source

1. This is on the basis of the great God slightly made a little bit of picture display optimization, before the picture is too small, the display blurred, magnified the next

2. Then re-organized, in a way that uses the bulk execution case

3. Download the reference code from GitHub
https://github.com/yoyoketang/selenium_report/

Selenium+python Automated 81-html Report Optimization (pie chart + failed re-run + compatible python2&3) "Reprint"

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.