selenium自动化PageObject应用实例unittest+ PageObject+html测试报告

版权声明:QQ群:796245415 个人技术交流,禁止用作商业活动 https://blog.csdn.net/chen498858336/article/details/83834576
basic_info.py
# coding =utf-8
import time


class PageObject(object):
    def __init__(self, driver):
        self.driver = driver

    def test(self):
        ele = self.driver.find_element_by_id("menu-user")
        return ele

    def login(self):
        self.driver.find_element_by_id("userAccount").send_keys("admin")
        self.driver.find_element_by_id("loginPwd").send_keys(123456)
        time.sleep(2)
        self.driver.find_element_by_id("loginBtn").click()



TestMyUnittest.py

# coding=utf-8
import unittest
from selenium import webdriver
from basic_info import PageObject
from time import sleep


class Test(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(5)
        cls.driver.maximize_window()
        cls.name = "zhangsan"
        cls.url="http://192.168.229.128:8080/cms/manage/login.do"
        cls.driver.get(cls.url)
        # PageObject(cls.driver).login()
        print" i am class method ,do i before all method start"

    @classmethod
    def tearDownClass(cls):
        sleep(5)
        cls.driver.close()
        print "i am class method ,do i after all method finished"

    def test1_login(self):
        u"用户登录"
        PageObject(self.driver).login()

    def test2_action(self):
        u"用户点击"
        PageObject(self.driver).test().click()
        print "success"

    def test3_class(self):
        u"调试"
        print "class.attribute:{},class.dui_xiang:{}".format(self.name, self)

if __name__ == "__main__":
    unittest.main(verbosity=2)


简单定义了一个类管理套件和报告方法:
# coding =utf-8
import unittest
import os
from HTMLTestRunner import HTMLTestRunner
import time


class TestSuiteRun(object):

    def add_suites(self):
        cur_path =os.getcwd()
        suites = unittest.defaultTestLoader.discover(cur_path, pattern='test*.py', top_level_dir=None)
        return suites
        # unit = unittest.TestSuite()
        # for suite in suites:
        #     print suite
        #     for case in suite:
        #         print case
        #         unit.addTests(case)
        # print unit
        # return unit

    def run(self, report_path):
            with open(report_path, 'wb') as f:
                runner = HTMLTestRunner(stream=f, title="interface report",
                                        description="results like following:", verbosity=2)
                runner.run(self.add_suites())
            f.close()
if __name__ ==  "__main__":
    job = time.strftime("%Y%m%M%H%M%S", time.localtime(time.time()))
    report_path = r"C:\Users\Administrator\PycharmProjects\py3project\wrapperkargs\{}_report.html".format(job)
    testsuiterun = TestSuiteRun()
    testsuiterun.run(report_path)

结果如下:
C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/py3project/wrapperkargs/run_selenium.py
 i am class method ,do i before all method start
ok test1_login (test_page.Test)
ok test2_action (test_page.Test)
ok test3_class (test_page.Test)
i am class method ,do i after all method finished

Time Elapsed: 0:00:15.564000

Process finished with exit code 0


在这里插入图片描述


在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/chen498858336/article/details/83834576