Selenium (Python) generates HTML test reports

Source: Internet
Author: User

Since Python3 has not supported Htmltestrunner anymore,

Neither Pycharm nor PIP can be successfully installed,

So can only go to https://pypi.python.org/pypi/HTMLTestRunner manual download,

Put the htmltestrunner.py file into the Lib folder under the Python3 installation directory;

There's one more place to be aware of,

Python3 already does not support the file method,

should use open!.

A lot of pits ...

New testcase.py:

 import UnitTest 
from time import sleep
from selenium import webdriver


Class Searchtestcase ( UnitTest. TestCase):

def setUp (self):
Self.driver = Webdriver. Firefox ()
Self.driver.maximize_window ()
Self.driver.get ("https://www.baidu.com/")
Sleep (2

def Test_searchchina (self):
"" "Baidu searches China for test case" ""
Self.driver.find_element_by_xpath (".//*[@ id= ' kw '). Send_keys ("China")
Self.driver.find_element_by_xpath (".//*[@id = ' su ']"). Click ()
Sleep (2)
result = Self.driver.find_element_by_xpath (".//*[@id = ' 1 ']/h3/a"). Text

Self.assertequal (Result, "China _ Hundred ")

def tearDown (self):
Self.driver.close ()
Self.driver.quit ()

If __name__ = = ' _ _main__ ':
Unittest.main ()


and then new htmlreport.py:

Import Htmltestrunner
Import UnitTest
From time import strftime, localtime, time

From TestCase import searchtestcase

Suite = UnitTest. TestSuite ()
# Get the instance object of Testsuite
Suite.addtest (Searchtestcase ("Test_searchchina"))
# Add test Cases to the test container
now = strftime ("%y-%m-%m-%h_%m_%s", LocalTime (Time ()))
# Get current time
filename = now + "test.html"
# file name
fp = open (filename, "WB")
# Open the file in binary mode and write the result
Runner = Htmltestrunner.htmltestrunner (
STREAM=FP,
verbosity=2,
Title= "The title of the test Report",
description= "Details of the test report")
Runner.run (Suite)
Fp.close ()


Run a hand,
The HTML file is generated at the root of the current project,
Open it:

Selenium (Python) generates HTML test reports

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.