一、页面对象

该篇是介绍页面对象设计模式的方法。页面对象是Web应用程序用户界面的区域,测试时主要在此区域。

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

  • 创建多测试用例共用的可重复代码
  • 减少重复的代码
  • 如果用户界面发生改变,只需修改一次对应部分

1.测试用例

以下是一个测试python.org搜索字符串的用例,并且确保可以查到一些结果。

import unittest
from selenium import webdriver
import page

class PythonOrgSearch(unittest.TestCase):
    '''A sample teest 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. searchs for the word "pycon" then verified that some results show up.
        Note that it does not look for any particular test 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 desn'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()

2.Page 对象类

Page 对象模式是为每个页面创建一个对象。按照此方法,创建一个测试代码与技术实施之间的隔离层。 page.py可以为:

from element import BasePageElement
from locators import MainPageLocators

class SearchTextElement(BasePageElement):
    """ This class gets 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" appers 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

3.Page elements

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))
        element = driver.find_element_by_name(self.locator)
        return element.get_attribute("value")

4.Locators

一种方式为将locators字符串和使用的地方分离。下面的例子,同一页面的locators属于同一个类。 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 SearchResultPageLocators(object):
    """A class for search results locators. All search results locators should come here"""
        pass

Top^

上一篇Selenium主题5 下一篇Selenium主题7

by 李鹏