赞
踩
PO模式是page object的缩写,是一种设计模式
举个栗子来讲:用户注册使用PO模式后,操作步骤如下:
PO模式分层机制3
说到PO模式的有点必须要说到非PO模式进行对比可能会更加明显
Exp:用户注册为例进行编写
实现BasePage类:
#BasePage.py from selenium import webdriver class BasePage(object): #初始化driver def __init__(self, driver): self.driver = driver # 访问页面 def visit(self,url): self.driver.get(url) # 元素定位 def locator(self, locator): return self.driver.find_element(*locator) # 关闭浏览器 def quit_driver(self): self.driver.quit()
实现注册类:
#Register.py from basepage.base_page import BasePage from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep class Register(BasePage): # 页面元素发生变化时,维护好当前元素路径即可 input_username = (By.ID, 'kw') input_pwd = (By.ID, 'passwd') click_id = (By.ID, 'su') # 输入元素 def input_text(self, input_text): self.locator(self.input_id).send_keys(input_text) # 点击注册按钮 def click_element(self): self.locator(self.click_id).click() # 测试 def check(self, url, input_text): self.visit(url) self.input_text(input_text) self.click_element() if __name__ == '__main__': url = 'https://www.baidu.com' driver = webdriver.Chrome() sp = SearchPage(driver) sp.check(url, '软件测试') sp.quit_driver()
测试用例实现:
#test_register.py from basepage.base_page import BasePage from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep class RegisPage(BasePage): url = 'https://www.test.com' link_id = (By.LINK_TEXT,'注册') # 用户名 username = (By.ID,'pwd') # 密码 pwd = (By.ID,'pwd') # 注册按钮 btn = (By.XPATH,'//*[@id="TANGRAM__PSP_11__submit"]') # 弹出登录框 def click_link(self): self.locator(self.link_id).click() # 点击用户名密码注册 def click_user(self): self.locator(self.uname_pwd).click() # 输入用户名 def input_username(self,input_username): self.locator(self.username).send_keys(input_username) # 输入密码 def input_pwd(self,input_pwd): self.locator(self.pwd).send_keys(input_pwd) # 点击注册 def click_register(self): self.locator(self.btn).click() if __name__ == '__main__': driver = webdriver.Chrome() sp = LoginPage(driver) sleep(3) sp.check('username','pwd') sleep(8) sp.quit_driver()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。