Web自动化测试项目(二)BasePage实现
2021-04-24 05:29
标签:不同 selector 变量 rop https cas 单例模式 tcl const base_page.py constants.py se_utils.py base_page.py login_page.py home_page.py Web自动化测试项目(二)BasePage实现 标签:不同 selector 变量 rop https cas 单例模式 tcl const 原文地址:https://www.cnblogs.com/snailrunning/p/12236298.html一、BasePage介绍
二、Selenium Api 二次封装
├── pages
│?? ├── __init__.py
│?? └── base_page.py
└── utils
└── __init__.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
├── pages
│?? ├── __init__.py
│?? └── base_page.py
├── run_case
│?? └── __init__.py
└── utils
├── __init__.py
├── constants.py
└── se_utils.py
DOMAIN = 'https://www.tapd.cn/'
LOGIN_URL = '/cloud_logins/login'
TIMEOUT = 10
POLL_FREQUENCY = 0.5
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
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
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()
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实现
文章链接:http://soscw.com/index.php/essay/78814.html