Python&Selenium借助html-testRunner生成自动化测试报告

时间:2022-02-16 06:02:07

一、摘要

本博文将介绍Python和Selenium进行自动化测试时,借助html-testRunner 生成自动化测试报告

安装命令:pip install html-testRunner

二、测试代码

# encoding = utf-8
'''
@Time    : 2018/8/14 20:41
@Author  : davieyang
@File    : test_AjaxElement.py
@Software: PyCharm
'''
import traceback

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import unittest
import time
import HtmlTestRunner


class TestAjaxElement(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()

    def test_AjaxDivOptionByKeys(self):
        """simulate keyboadr keydonw and enter and by circulating"""
        url = 'http://www.sogou.com'
        self.driver.get(url)
        searchBox = self.driver.find_element_by_id('query')
        searchBox.send_keys("selenium")
        time.sleep(3)
        for i in range(3):
            searchBox.send_keys(Keys.DOWN)
            time.sleep(3)
        searchBox.send_keys(Keys.ENTER)
        time.sleep(3)

    def test_AjaxDivOptionByWords(self):
        """using xpath to fuzzy match"""
        url = 'http://www.sogou.com'
        self.driver.get(url)
        try:
            searchBox = self.driver.find_element_by_id('query')
            searchBox.send_keys(u'光荣之路')
            time.sleep(3)
            suggetion_option = self.driver.find_element_by_xpath("//ul/li[contains(.,'@#')]")
            suggetion_option.click()
            time.sleep(3)
        except NoSuchElementException as e:
            print(traceback.print_exc())

    def test_error(self):
        """ This test should be marked as error one. """
        raise ValueError

    def test_fail(self):
        """ This test should fail. """
        self.assertEqual(1, 2)

    @unittest.skip("This is a skipped test.")
    def test_AjaxDivOptionByIndex(self):
        """using xpath to locate"""
        url = 'http://www.sogou.com'
        self.driver.get(url)
        try:
            searchBox = self.driver.find_element_by_id('query')
            searchBox.send_keys(u'光荣之路')
            time.sleep(3)
            suggetion_option = self.driver.find_element_by_xpath("//*[@id='vl']/div[1]/ul/li[3]")
            suggetion_option.click()
            time.sleep(3)
        except NoSuchElementException as e:
            print(traceback.print_exc())

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


if __name__ == '__main__':
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='D:\\Programs\\Python\\PythonUnittest\\Reports\\'))

三、报告样式

Python&Selenium借助html-testRunner生成自动化测试报告