Selenium-Webdriver-PageObject


阅读次数

设计说明

使用Selenium Webdriver 编写UI自动化测试脚本的设计方法非常多,但是大体都分为

  1. 元素定位
  2. 业务逻辑)

  3. 断言处理

这里对WebDriver Python 的Page Objects 设计模式就行下整理,这个模式采用面向对象的思想,把每个页面看作是一个的对象,页面的内的的所有的元素还有业务逻辑都被看作是是属性和方法

class page_name():

def action1():
....

def action2():
....

def action3():
....

设计结构

基本结构分为四层

  • BasePage (封装WebDriver 的基础方法)
  • PageElement (页面对象类)
  • PageLOctors (页面坐标路径)
  • TestCase (测试用例)

Demo

这里拿小米官网的登陆做一个例子

BasePage/BaseDriver.py

# -*- coding:utf-8 -*-

# webdriver page object 基础公用方法
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class BaseDriver(object):

def __init__(self, driver):
self.driver = driver

# 打开指定url
def open_url(self, url, pagetitle):
self.driver.get(self.url)
self.driver.maximize_window()
print self.driver.title
assert self.pagetitle in self.driver.title

# 根据传入坐标,返回元素
def get_element(self, *loc):
element = self.driver.find_element(*loc)
return element

# 根据传入坐标,返回所有匹配的元素,返回list
def get_elements(self, *loc):
elementlist = self.driver.find_elements(*loc)
return elementlist

# webdriver 调用selenium 的方法,判断元素是否存
def is_element_exist(self, *loc):
try:
self.driver.find_element(*loc)
self.driver.find_elements(*loc)
return True
except:
return False

# webdriver 调用selenium 的方法,判断元素是否存
def is_element_display(self, *loc):
try:
if self.driver.find_element(*loc).is_displayed() or self.driver.find_elements(*loc).is_displayed():
return True
else:
return False
except:
return False

# 判断元素是否显示
def wait_element_display(self, time, *loc):
try:
WebDriverWait(self.driver, time).until(
EC.presence_of_element_located(*loc))
return True
except:
return False

# 判断元素是否可视
def wait_element_visible(self, time, *loc):
try:
WebDriverWait(self.driver, time).until(
EC.visibility_of_element_located(*loc))
return True
except:
return False

# 执行js 脚本
def exec_js(self, script):
self.driver.execute_script(script)

# 刷新网页
def refresh_page(self):
self.driver.refresh()

PageElement/login_page.py

# -*- coding:utf-8 -*-

from BasePage.BaseDriver import BaseDriver
from PageLoctors.login_page_loc import LoginPageLoc

class LoginPage(BaseDriver):

def __init__(self,driver):
BaseDriver.__init__(self,driver)

def input_username(self, username):
print self.get_element(*LoginPageLoc.user)
self.get_element(*LoginPageLoc.user).send_keys(username)

def input_password(self, password):
self.get_element(*LoginPageLoc.password).send_keys(password)

def click_login_button(self):
self.get_element(*LoginPageLoc.loginbut).click()

PageLocotrs/Login_page_loc.py

# -*- coding:utf-8 -*-

from selenium.webdriver.common.by import By

class LoginPageLoc(object):

user = (By.ID, "username")
password = (By.ID, "pwd")
loginbut = (By.ID, "login-button")

TestCase/xiaomi_Test.py

# -*- coding:utf-8 -*-

import unittest

from selenium import webdriver
from PageElement.login_page import LoginPage

class XiaoMiTest(unittest.TestCase):

def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get("url......")
self.driver.maximize_window()
self.driver.implicitly_wait(30)

self.username = "xxxxxxxxx"
self.password = "xxxxxxxxx"

def test_login(self):
loginPage = LoginPage(self.driver)
loginPage.input_username(self.username)
loginPage.input_password(self.password)
loginPage.click_login_button()

def tearDown(self):
self.driver.quit()

if __name__ == '__main__':
unittest.main()

参考

官方PageObject 说明
github代码

">