码迷,mamicode.com
首页 > Web开发 > 详细

Web自动化测试项目(二)BasePage实现

时间:2020-01-27 17:16:05      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:不同   selector   变量   rop   https   cas   单例模式   tcl   const   

一、BasePage介绍

  1. 创建一个BasePage类,对Selenium Api进行二次封装
  2. 为了快速创建项目并投产,用到的Selenium Api才进行封装,没用到的则不封装
  3. 优先封装最重要的几个方法:
    • 初始化webdriver
    • 元素定位(不封装元素操作方法,例如send_keys)
    • 浏览器加载页面

二、Selenium Api 二次封装


├── pages
│?? ├── __init__.py
│?? └── base_page.py
└── utils
    └── __init__.py

base_page.py

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

DOMAIN = 'https://www.tapd.cn/'
TIMEOUT = 10
POLL_FREQUENCY = 0.5


class BasePage():

    def __init__(self, driver=None, path=None):
        self.driver = driver if driver != None else webdriver.Chrome()
        self.get_url(path)

    def find_element(self, locator):
        WebDriverWait(driver=self.driver, timeout=TIMEOUT, poll_frequency=POLL_FREQUENCY).until(
            EC.visibility_of_element_located(locator))
        return self.driver.find_element(*locator)

    def get_url(self, path=None):
        if path != None:
            url = URL + path
        else:
            url = None

        if url != None:
            self.driver.get(url)

三、优化base_page

  1. DOMAIN,TIMEOUT,POLL_FREQUENCY这几个全局变量是随时可以改变的,我们称为魔法变量,会特意建一个文件存起来,命名为: constants.py ,本项目存放在utils目录 (所有的url也可放在单独的一个文件叫urls.py,区别于constants.py)。
  2. 编写一个dirver工具类(单例模式),实现只打开和关闭一次浏览器,存放在se_utils.py,用于管理启动不同的浏览器的开关。
├── pages
│?? ├── __init__.py
│?? └── base_page.py
├── run_case
│?? └── __init__.py
└── utils
    ├── __init__.py
    ├── constants.py
    └── se_utils.py

constants.py

DOMAIN = 'https://www.tapd.cn/'
LOGIN_URL = '/cloud_logins/login'
TIMEOUT = 10
POLL_FREQUENCY = 0.5

se_utils.py

from selenium import webdriver

class Driver():
    _driver = None

    @classmethod
    def get_driver(cls, browser_name='Chrome'):
        if cls._driver == None:
            if browser_name == 'Chrome':
                cls._driver = webdriver.Chrome()
            elif browser_name == 'Firefox':
                cls._driver = webdriver.Firefox()
            elif browser_name == 'Safari':
                cls._driver == webdriver.Safari()
            elif browser_name == 'Opera':
                cls._driver == webdriver.Opera()
            elif browser_name == 'edge':
                cls._driver == webdriver.Edge()
            elif browser_name == 'Ie':
                cls._driver == webdriver.Ie()
            else:
                raise NameError(
                    "Not found %s browser,You can enter 'Chrome', 'Firefox', 'Ie', 'Edge', 'Safari',Opera" % browser_name)
        return cls._driver

    @classmethod
    def quit_driver(cls):
        if cls._driver:
            cls._driver.quit()
            cls._driver = None

base_page.py

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from utils.constants import TIMEOUT, POLL_FREQUENCY, DOMAIN
from utils.se_utils import Driver

class BasePage():

    def __init__(self, path=None):
        self.driver = Driver.get_driver()
        self.driver.maximize_window()
        self.get_url(path)

    def find_element(self, locator):
        WebDriverWait(driver=self.driver, timeout=TIMEOUT, poll_frequency=POLL_FREQUENCY).until(
            EC.visibility_of_element_located(locator))
        return self.driver.find_element(*locator)

    def get_url(self, path=None):
        if path != None:
            url = DOMAIN + path
        else:
            url = None

        if url != None:
            self.driver.get(url)

四、其他页面继承BasePage

  1. 编写两个页面类,登录页和登陆成功后的首页
  2. 页面类里包含元素定位和用例

login_page.py

from base_page import BasePage, By
from home_page import HomePageAction

class LoginPage(BasePage):
    username_locator = (By.CSS_SELECTOR, '#username')
    password_locator = (By.CSS_SELECTOR, '#password_input')
    login_button = (By.CSS_SELECTOR, '#tcloud_login_button')

    def username_input_box(self):
        return self.find_element(self.username_locator)

    def password_input_box(self):
        return self.find_element(self.password_locator)

    def login_btn(self):
        return self.find_element(self.login_button)


class LoginPageAction(LoginPage):

    def login(self, username, password):
        self.username_input_box().clear()
        self.username_input_box().send_keys(username)
        self.password_input_box().clear()
        self.password_input_box().send_keys(password)
        self.login_btn().click()
        return HomePageAction()

home_page.py

from base_page import BasePage, By

class HomePage(BasePage):
    user_title_locator = (By.CSS_SELECTOR, '.avatar-text-default.avatar-Q')

    def user_title(self):
        return self.find_element(self.user_title_locator)


class HomePageAction(HomePage):

    @property
    def get_user_title(self):
        return self.user_title().get_attribute('title')




Web自动化测试项目(二)BasePage实现

标签:不同   selector   变量   rop   https   cas   单例模式   tcl   const   

原文地址:https://www.cnblogs.com/snailrunning/p/12236298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!