简介
Page Object Model: 页面对象模型, 是 Selenium 中的一种测试设计模式, 一个页面对象代表用户界面交互测试的一个区域.
使用 page object 来实现: 测试, 逻辑, 数据, 驱动相互分离.
使用页面对象模式的好处:
创建可重用的代码, 可以跨多个测试用例共享
减少重复代码的数量
如果用户界面更改, 只需要修改一个地方
定位器
定位器将定位的字符串和使用它们的地方分隔开来, 将来开发修改了定位的方式和字符串只用来改该文件
locators.py 如下:
- from selenium.webdriver.common.by import By
- class MainPageLocators(object):
- """主页的定位器们"""
- GO_BUTTON = (By.ID, 'submit')
- class SearchResultsPageLocators(object):
- """搜索结果页定位器们"""
- pass
- 基类
- 每个页面类继承. 该类封装了下级 page 类的基本功能和公共的功能.
- from selenium.webdriver.support.ui import WebDriverWait
- class BasePageElement(object):
- """Base page class that is initialized on every page object class."""
- def __set__(self, obj, value):
- """Sets the text to the value supplied"""
- driver = obj.driver
- WebDriverWait(driver, 100).until(
- lambda driver: driver.find_element_by_name(self.locator))
- driver.find_element_by_name(self.locator).send_keys(value)
- def __get__(self, obj, owner):
- """Gets the text of the specified object"""
- driver = obj.driver
- WebDriverWait(driver, 100).until(
- lambda driver: driver.find_element_by_name(self.locator))
- element = driver.find_element_by_name(self.locator)
- return element.get_attribute("value")
- 各页面对象
- 页面对象模式将为每个 web 页面创建一个对象. 封装一些各页面的方法.
- page.py 如下:
- from element import BasePageElement
- from locators import MainPageLocators
- class SearchTextElement(BasePageElement):
- """这个类从指定的定位器中获得搜索文本"""
- #The locator for search box where search string is entered
- locator = 'q'
- class BasePage(object):
- """Base class to initialize the base page that will be called from all pages"""
- def __init__(self, driver):
- ? ?"""该 driver 是测试用例使用时传过来的"""
- self.driver = driver
- class MainPage(BasePage):
- """Home page action methods come here. I.e. Python.org"""
- #Declares a variable that will contain the retrieved text
- search_text_element = SearchTextElement()
- def is_title_matches(self):
- """验证"Python"字符串在 title 中"""
- return "Python" in self.driver.title
- def click_go_button(self):
- """触发搜索"""
- element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
- element.click()
- class SearchResultsPage(BasePage):
- """Search results page action methods come here"""
- def is_results_found(self):
- # Probably should search for this text in the specific page
- # element, but as for now it works fine
- return "No results found." not in self.driver.page_source
- 测试基础类
- 封装 setup 和 teardown 的类. 这里用 appium 的例子.
- ```python
- class BaseTest(unittest.TestCase):
- def setUp(self):
- path = lambda p: os.path.abspath(
- os.path.join(os.path.dirname(file), p)
- )
- desired_caps = {
- 'platformName': 'Android',
- 'platformVersion': '5.0.1',
- 'deviceName': 'MX5',
- 'appPackage': 'net.xxx.debug',
- 'appActivity': 'net.xxx.MainActivity',
- 'unicodeKeyboard': 'True',
- #'resetKeyboard': 'True'
- 'app': path('./apps/debug.10.apk')
- }
- self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
- def tearDown(self):
- self.driver.quit()
- ```
- 测试代码
- 具体的测试用例写在这里.
- import unittest
- from selenium import webdriver
- import page
- class PythonOrgSearch(BaseTest):
- def setUp(self):
- self.driver = webdriver.Firefox()
- self.driver.get("http://www.python.org")
- def test_search_in_python_org(self):
- """验证查找的元素存在"""
- #加载 main page. 这里使用 Python.org.
- main_page = page.MainPage(self.driver)
- #检验 "python" 在 title 中
- assert main_page.is_title_matches(), "python.org title doesn't match."# 设置搜索模式"pycon"main_page.search_text_element ="pycon"
- main_page.click_go_button()
- search_results_page = page.SearchResultsPage(self.driver)
- #验证搜索结果页不为空
- assert search_results_page.is_results_found(), "No results found."
- def tearDown(self):
- self.driver.close()
- if __name__ == "__main__":
- unittest.main()
来源: http://www.bubuko.com/infodetail-2639964.html