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

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 cases in suite:
        #         print cases
        #         for case in cases:
        #         		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


在这里插入图片描述


在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值