每一个页面都有一个page class管理py文件,将定位元素等操作(管理定位方式+值+元素操作方法+业务关键字)封装到此py文件中
优化过程:
1.第一次优化:集成PO
将定位元素的id,name,xpath等,作为类属性定义到管理文件的类中:
class LoginPage:
username='accounts'
pwd = 'pwd'
loginButton='xpath地址‘
测试用例的参数取值中只需要调用类.类属性:
driver.fine_element_by_name(LoginPage.username).send_keys('用户名')
2,第二次优化,元素的定位方式放到PO统一管理
username=(By.NAME,'accounts')
pwd = (By.NAME,'pwd')
loginButton=(By.XPATH,'xpath值')
测试用例:
driver.find_element(*LoginPage.username).send_keys('用户名')
3.第三次优化,业务步骤
将每一个步骤封装到PO文件中
def username(self,value):
self.find_element(LoginPage.username).send_keys(value)
元素操作需要浏览器对象,所以新建一个浏览器方法页面(InitBrowser),定义一个浏览器基础类(class Browser():),将浏览器操作封装在这个类中,PO类需继承浏览器基础类:
def __init__(self,driver):
self.driver=driver
def find_element(self,loc):
return self.driver.find_element(*loc) 返回元素对象
测试用例:调用操作步骤方法(需继承封装类):
self.username('yinuo')
4.第四次优化:封装业务关键字(常用的绑定在一起)
例如把用户名,密码,点击登录按钮封装在一个方法中(PO页面)
def login(self,username,password):
self.username(username)
self.password(password)
self.login_button()
测试用例:
self.login('qingfeng','123456')
5.第五次优化:每次操作一个元素,自动加上显式等待时间,封装到浏览器基础类
def wait_element_visible(self,loc):
WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))
return ele
自动:
def click(self,loc):
self.wait_element_visible(loc).click()
def send_keys(self.loc,value):
self.wait_element_visible(loc).send_keys(value)
PO页面:
def search_input(self,value):
self.send_keys(HomePage.search_input,value)
def search_butthon(self):
self.click(HomePage.search_butthon)