Selenium Python Tutorial Chapter 6: Working with Page Objects

insert image description here

6. Page Objects page objects

6.1 What is the Page Object Model (POM)?

Page Objects Model (POM) is a set of classes designed to represent one or more web pages. One class is used to store all the elements on one web page. Similar web pages can reuse this class.
A website usually has multiple pages, and multiple page objects can be used to represent each page. The benefits are:

  • The structure of the web application testing program or crawler program is clearer and easier to understand.
  • For multiple web pages with similar structure, the amount of repeated code can be reduced
  • If the web page elements change, you only need to modify one

6.2 Project structure with page objects

The usual structure of a project using page objects is something like

 |-- pages
    |--- locators.py
    |--- elements.py
    |--- pages.py
|-- tests
    |--- test_contact_page.py

Description of each file:

  • pages.py defines page elements and the operation method for each element
  • locators.py separates locators. Usually, locators on the same page belong to the same class
  • elements.py usually defines a base class of page elements, providing set(), get() methods
  • test_*.py test case class test case class file

6.2 Source code files and instructions

6.2.1 Test case class source code

In this example test_contact_page.py is used to fill in the Contact Me form on the colibri-software.com website to determine whether the result is successful

import unittest
from selenium import webdriver
import pages

class TestColibriSoftwareContactMe(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get("https://www.colibri-software.com")

    def test_submit_contact_me_form(self):
        """
         "Contact me" 测试表单
        填写各字段,提交表单,验证提交是否成功
        """

        # 加载主页,本例为 colibri-software.com主页
        contact_page = pages.ContactPage(self.driver)

        # Checks if the word "Contact" is in title
        assert contact_page.is_title_matches()

        # 向表单各字段填写内容
        contact_page.name_input = 'John Doe'
        contact_page.company_name_input = 'John Doe\'s paper company'
        contact_page.email_input = '[email protected]'
        contact_page.additional_info_input = 'I need a website to sell paper online'

        # 提前表单
        contact_page.click_submit_form_button()

        # 验结果是否成功
        assert contact_page.success_message_is_displayed()

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

6.2.2 Page object class source code

pages.py describes how to write page object classes

from elements import BasePageElement
from locators import ContactPageLocators

class NameElement(BasePageElement):
    """
    This class gets the search text from the specified locator
    """

    # The locator for text box where name is entered
    locator = 'wpforms-236-field_0'

# Similar classes for other text fields    
class CompanyNameElement(BasePageElement):

    locator = 'wpforms-236-field_4'

class EmailElement(BasePageElement):

    locator = 'wpforms-236-field_1'

class AdditionalInfoElement(BasePageElement):

    locator = 'wpforms-236-field_0'

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 ContactPage(BasePage):
    """
    Contact page action methods come here
    """

    # Declares text input fields
    name_input = NameElement()
    company_name_input = CompanyNameElement()
    email_input = EmailElement()
    additional_info_input = AdditionalInfoElement()

    def is_title_matches(self):
        """
        Verifies that the text "Contact" appears in page title
        """
        return 'Contact' in self.driver.title

    def click_submit_form_button(self):
        """
        Submits the form
        """
        element = self.driver.find_element(*ContactPageLocators.SUBMIT_FORM_BUTTON)
        element.click()

    def success_message_is_displayed(self):
        success_message = 'Thanks for contacting us! We will be in touch with you shortly.'
        return success_message in self.driver.page_source

6.2.3 Page element base class source code

elements.py defines a base class of page elements, providing set(), get() methods

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

6.2.4 The locators class
A good programming practice is to separate the locator characters. In this example, the locators for the same page belong to the same class.

from selenium.webdriver.common.by import By

class ContactPageLocators(object):
    """
    A class for all Contact page locators.
    """
    SUBMIT_FORM_BUTTON = (By.CSS_SELECTOR, 'button[type="submit"]')

class SearchResultsPageLocators(object):
    """A class for search results locators. All search results locators should come here"""
    pass

おすすめ

転載: blog.csdn.net/captain5339/article/details/131123472