Selenium页面对象模型

发表于:2016-12-19 10:57

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:积跬步    来源:51Testing软件测试网采编

  Page Object Model:页面对象模型,是Selenium中的一种测试设计模式,一个页面对象代表用户界面交互测试的一个区域。
  使用页面对象模式的好处:
  · 创建可重用的代码,可以跨多个测试用例共享
  · 减少重复代码的数量
  · 如果用户界面更改,只需要修改一个地方
  测试代码
importunittest
fromseleniumimportwebdriver
importpage
classPythonOrgSearch(unittest.TestCase):
defsetUp(self):
self.driver = webdriver.Firefox()
self.driver.get("http://www.python.org")
deftest_search_in_python_org(self):
"""
验证查找的元素存在
"""
#加载main page.这里使用Python.org.
main_page = page.MainPage(self.driver)
#检验“python”在title中
assertmain_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)
#验证搜索结果页不为空
assertsearch_results_page.is_results_found(),"No results found."
deftearDown(self):
self.driver.close()
if__name__ =="__main__":
unittest.main()
  页面对象
  页面对象模式将为每个web页面创建一个对象。page.py如下。
fromelementimportBasePageElement
fromlocatorsimportMainPageLocators
classSearchTextElement(BasePageElement):
"""这个类从指定的定位器中获得搜索文本"""
#The locator for search box where search string is entered
locator = 'q'
classBasePage(object):
"""Base class to initialize the base page that will be called from all pages"""
def__init__(self, driver):
self.driver = driver
classMainPage(BasePage):
"""Home page action methods come here. I.e. Python.org"""
#Declares a variable that will contain the retrieved text
search_text_element = SearchTextElement()
defis_title_matches(self):
"""验证"Python"字符串在title中"""
return"Python"inself.driver.title
defclick_go_button(self):
"""触发搜索"""
element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
element.click()
classSearchResultsPage(BasePage):
"""Search results page action methods come here"""
defis_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."notinself.driver.page_source
  页面元素
  element.py如下:
fromselenium.webdriver.support.uiimportWebDriverWait
classBasePageElement(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(
lambdadriver: 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(
lambdadriver: driver.find_element_by_name(self.locator))
element = driver.find_element_by_name(self.locator)
returnelement.get_attribute("value")
  定位器
  将定位的字符串和使用它们的地方分隔开来,locators.py如下:
fromselenium.webdriver.common.byimportBy
classMainPageLocators(object):
"""主页的定位器们"""
GO_BUTTON = (By.ID, 'submit')
classSearchResultsPageLocators(object):
"""搜索结果页定位器们"""
pass
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号