标签:BMI 基本 https imp port use return sele The
pageObject设计模式是python技术栈测试中使用最广泛的一种设计模式,使用pageObject设计模式可以分离出页面对象层,操作层和业务层,不同的层关注只关注一个问题,便于管理,不同的层形成一种金字塔的结构,便于代码复用,减少代码重复率
1)创建一个基础类Page,在初始化方法__init__()中定义驱动(driver),定义寻找元素的基本方法,和一些基本的URL(base_url)和超时时间(timeout)等
from time import sleep class Page(): ‘‘‘页面基础类‘‘‘ # 初始化 def __init__(self, driver): self.driver = driver self.base_url = ‘https://xxx‘ self.timeout = 10 # 打开不同的子页面 def _open(self, url): url_ = self.base_url + url print("The url is %s" % url_) # self.driver.maximize_window() self.driver.get(url_) sleep(2) assert self.driver.current_url == url_, ‘Did not load on %s‘ % url_ def open(self): self._open(self.url) # 元素定位方法封装 def find_element(self, *loc): return self.driver.find_element(*loc)
2)再创建页面对象层的py
from po.BasePage import * from selenium.webdriver.common.by import By class LoginPageView(Page): url = "/" # 定位器 username_loc = (By.NAME, ‘username‘) password_loc = (By.NAME, ‘password‘) submit_loc = (By.NAME, ‘Submit‘) # 用户名输入框元素 def type_username(self, username): self.find_element(*self.username_loc).clear self.find_element(*self.username_loc).send_keys(username) # 密码输入框元素 def type_password(self, password): self.find_element(*self.password_loc).clear self.find_element(*self.password_loc).send_keys(password) # 登录按钮元素 def type_submit(self): self.find_element(*self.submit_loc).click
3)再创建操作层的py
from po.LoginPage import LoginPageView class caoz(LoginPageView) # 登录功能模块封装 def test_user_login(driver, username, password): ‘‘‘测试用户名密码是否可用‘‘‘ login_page = LoginPage(driver) login_page.open() login_page.type_username(username) login_page.type_password(password) login_page.type_submit()
4)再创建业务层的py
from po.LoginProcess import * from selenium import webdriver driver = webdriver.Chrome() username = ‘用户名‘ password = ‘密码‘ test_user_login(driver, username, password) sleep(3) driver.quit()
selenium的pageObject设计例子_byseyOrd
标签:BMI 基本 https imp port use return sele The
原文地址:https://www.cnblogs.com/seyOrd/p/12692208.html