前言:最痛苦的事,不是失败,是我本可以!不让时间隐藏你的潜力。

一、selenium 分层设计
1、元素层

2、功能层

3、用例层

举例:存在login_test用例:

import unittest
from common.browser import Browser
from common.base_page import BasePage
from actions.login_action import LoginAction
from common.config_utils import local_config

class LoginTest(unittest.TestCase):
    def setUp(self) -> None:
        self.base_page = BasePage(Browser().get_driver())
        self.base_page.set_browser_max()
        self.base_page.implicitly_wait()
        self.base_page.open_url(local_config.url)

    def tearDown(self) -> None:
        self.base_page.close_tab()

    def test_login_success(self):
        login_action = LoginAction( self.base_page.driver )
        main_page = login_action.login_success('test01','newdream123')
        actual_result = main_page.get_username()
        self.assertEqual(actual_result,'测试人员1','test_login_success用例执行失败')

    def test_login_fail(self):
        login_action = LoginAction(self.base_page.driver)
        actual_result = login_action.login_fail('test01','newdream12')
        print('actual:%s'%actual_result)
        self.assertEqual(actual_result,'登录失败,请检查您的用户名或密码是否填写正确。')


if __name__ == '__main__':
    unittest.main()

  存在quit_test用例:

import unittest
from common.browser import Browser
from common.base_page import BasePage
from actions.login_action import LoginAction
from actions.quit_action import QuitAction
from common.config_utils import local_config

class QuitTest(unittest.TestCase):
    def setUp(self) -> None:
        self.base_page = BasePage(Browser().get_driver())
        self.base_page.set_browser_max()
        self.base_page.implicitly_wait()
        self.base_page.open_url(local_config.url)

    def tearDown(self) -> None:
        self.base_page.close_tab()

    def test_quit(self):
        login_action = LoginAction(self.base_page.driver)
        main_page = login_action.default_login()
        quit_action = QuitAction( main_page.driver )
        login_page = quit_action.quit()
        actual_result = login_page.get_title()
        self.assertEqual( actual_result.__contains__('用户登录'),True,'test_quit用例不通过' )

if __name__=='__main__':
    unittest.main()

  

其中用例层的每个模块(用例)存在共性:1)都需要写setup和teardown方法  2)均要先打开测试url

优化思路:使用selenium_base_page模块封装setup和teardown方法,在执行用例前先打开url。
代码如下:

import unittest
from common.base_page import BasePage
from common.browser import Browser
from common.config_utils import local_config
from common.log_utils import logger

class SeleniumBaseCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        logger.info('')
        logger.info('==============测试类开始执行=============')
        cls.url = local_config.url

    def setUp(self) -> None:
        logger.info('---------测试方法开始执行-----------')
        self.base_page = BasePage(Browser().get_driver())
        self.base_page.set_browser_max()
        self.base_page.implicitly_wait()
        self.base_page.open_url(self.url)

    def tearDown(self) -> None:
        # 测试用例失败截图
        # if len(self._outcome.errors)>=1:
        #     self.base_page.screenshot_as_file()
        errors = self._outcome.errors
        for test,exc_info in errors:
            if exc_info:
                self.base_page.wait()
                self.base_page.screenshot_as_file()
        self.base_page.close_tab()
        logger.info('---------测试方法执行完毕-----------')

    @classmethod
    def tearDownClass(cls) -> None:
        logger.info('==============测试类执行完毕=============')

 

posted on 2020-05-13 12:27  喵小超  阅读(457)  评论(0编辑  收藏  举报