本章是介绍页面对象设计模式的教程。一个页面对象代表了web应用用户接口的一片区域,你的测试代码将与之交互的。
使用页面对象模式的好处:
可以创建在多个测试样例中都可使用的可重用代码
减少重复性代码
如果用户接口发生改变,只需要字一个地方做出改动即可
6.1 测试样例
下面是一个测试样例,用于测试Pytohn.org网站的搜索功能,搜索一个单词,并确保能得到一些结果。
import unittest
from selenium import webdriver
import page
class PythonOrgSearch(unittest.TestCase):
"""A sample test class to show how page object works"""
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("http://www.python.org")
def test_search_in_python_org(self):
"""
Tests python.org search feature. Searches for the word "pycon" then verified that some results show up.
Note that it does not look for any particular text in search results page. This test verifies that
the results were not empty.
"""
#Load the main page. In this case the home page of Python.org.
main_page = page.MainPage(self.driver)
#Checks if the word "Python" is in title
assert main_page.is_title_matches(), "python.org title doesn't match."
#Sets the text of search textbox to "pycon"
main_page.search_text_element = "pycon"
main_page.click_go_button()
search_results_page = page.SearchResultsPage(self.driver)
#Verifies that the results page is not empty
assert search_results_page.is_results_found(), "No results found."
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
6.2.页面对象类
页面对象模式会为每一个web页面创建一个对象。按照这种技术就可以实现测试代码和技术实现的分层。
page.py代码就像下面这样:
from element import BasePageElement
from locators import MainPageLocators
class SearchTextElement(BasePageElement):
"""This class gets the search text from the specified locator"""
#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):
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):
"""Verifies that the hardcoded text "Python" appears in page title"""
return "Python" in self.driver.title
def click_go_button(self):
"""Triggers the search"""
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
6.3.页面元素
element.py代码如下:
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")
6.4.定位器
一个实用的习惯是将定位器字符串从使用它的地方分离出来。在本例中,相同页面的定位器从属于相同的类。
locators.py代码如下:
from selenium.webdriver.common.by import By
class MainPageLocators(object):
"""A class for main page locators. All main page locators should come here"""
GO_BUTTON = (By.ID, 'submit')
class SearchResultsPageLocators(object):
"""A class for search results locators. All search results locators should come here"""
pass
[译]Selenium Python文档:目录
作者:Baiju Muthukadan 协议:本文档采用知识共享署名 - 共享4.0国际许可. 原英文网址:http://selenium-python.readthedocs.io/index.ht ...
[译]Selenium Python文档:八、附录:FAQ常见问题
另外一个FAQ:https://github.com/SeleniumHQ/selenium/wiki/Frequently-Asked-Questions 8.1.怎样使用ChromeDriver ...
[译]Selenium Python文档:一、安装
1.1.简介 Selenium Python为使用Selenium WebDriver来编写功能/验证测试提供了一个简单的API接口.通过Selenium Python API,你可以以一种非常直观的 ...
[译]Selenium Python文档:四、元素定位
要定位一个页面中的元素有多中策略和方法.你可以根据实际情况选择其中最为合适的.Selenium为定位页面元素提供了下面的这些方法: find_element_by_id(使用id) find_elem ...
[译]Selenium Python文档:二、初步开始
2.1.简单使用 如果已经安装好了Selenium Python,你就可以像下面这样编写Python代码来使用它了: from selenium import webdriver from selen ...
[译]Selenium Python文档:五、Waits等待
大多数现代web应用都使用了AJAX技术.当浏览器加载一个页面的时候,该页面内的元素可能在不用的时间间隔内进行加载.这使得元素定位变得比较困难:如果一个元素还没有出现在DOM中,定位函数将会抛出一个E ...
[译]Selenium Python文档:七、WebDriver API接口
由于API文档格式不太适合cnblog博客,暂且翻译一部分,且暂未校对 注意:这不是官方文档,官方 API文档在这里. 本章包含Selenium WebDriver的所有接口 推荐import风格 本 ...
[译]Selenium Python文档:三、导航控制
你使用WebDriver要做的第一件事就是访问一个链接.一般通过调用get方法来实现: driver.get("http://www.baidu.com") 在将控制权返给你的脚本 ...
XML解析之sax解析案例(二)使用sax解析把 xml文档封装成对象
Demo1类: import java.io.File; import java.util.List; import javax.xml.parsers.SAXParser; import javax ...
随机推荐
quartz学习
quartz是一个作业调度框架,用于指定工作(作业)在指定时间执行——定时工作. quartz的核心接口有: Scheduler接口:Scheduler是job的执行对象,用于工作的执行. Job接口 ...
JS产生随机一注彩票
Centos 7.4下 部署openstack Queens 计算节点qemu高版本问题
sed -i 's/$contentdir/centos/g' /etc/yum.repos.d/CentOS-QEMU-EV.repo 这样既可正常安装compute服务
【BZOJ-3672】购票 树分治 + 斜率优化DP
3672: [Noi2014]购票 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1177 Solved: 562[Submit][Status][ ...
Linux——用户管理简单学习笔记(三)
用户组管理命令: groupadd -g 888 webadmin 创建用户组webadmin,其GID为888 删除用户组: groupdel 组名 修改用户组信息 groupmod groupmo ...
ORA-10997:another startup/shutdown operation of this instance in progress解决方法
SQL> startup ORA-10997: another startup/shutdown operation of this instance inprogress ORA-09967: ...