博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[译]Selenium Python文档:六、页面对象
阅读量:6761 次
发布时间:2019-06-26

本文共 3927 字,大约阅读时间需要 13 分钟。

本章是介绍页面对象设计模式的教程。一个页面对象代表了web应用用户接口的一片区域,你的测试代码将与之交互的。

使用页面对象模式的好处:

  • 可以创建在多个测试样例中都可使用的可重用代码
  • 减少重复性代码
  • 如果用户接口发生改变,只需要字一个地方做出改动即可

6.1 测试样例

下面是一个测试样例,用于测试Pytohn.org网站的搜索功能,搜索一个单词,并确保能得到一些结果。

import unittestfrom selenium import webdriverimport pageclass 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 BasePageElementfrom locators import MainPageLocatorsclass 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 = driverclass 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 WebDriverWaitclass 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 Byclass 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

转载地址:http://ekbeo.baihongyu.com/

你可能感兴趣的文章
问到的问题
查看>>
iOS网络模块优化(失败重发、缓存请求有网发送)
查看>>
经典SQL语句大全(绝对的经典)
查看>>
中小研发团队架构实践之总体架构设计
查看>>
PDO中获取结果集
查看>>
实用主义性能测试
查看>>
oozie开发注意事项
查看>>
【Tomcat】linux下实时查看tomcat运行日志
查看>>
HDU 5212 Code
查看>>
yarn使用
查看>>
Hadoop之 MapReducer工作过程
查看>>
CPU监控
查看>>
MongoDB中的explain和hint提的使用
查看>>
redis集群部署及踩过的坑
查看>>
为什么许多人宁愿死,也不愿思考
查看>>
从内核源代码配置文件预測泛泰新品(A920 ?)
查看>>
Disconf 学习系列之Disconf是什么?
查看>>
[UWP小白日记-11]在UWP中使用Entity Framework Core(Entity Framework 7)操作SQLite数据库(一)...
查看>>
【翻译】Apache Shiro10分钟教程
查看>>
Ooui.Wasm:浏览器中的.NET
查看>>