Do you know the super useful design pattern in UI automation testing?

What are POMs?

POM is the abbreviation of PageObjectModule (Page Object Model), and its purpose is to create an object library for Web UI testing.

In this mode, each page involved in the application should be defined as a separate class. The class should contain the page element objects on this page and the methods needed to process these element objects.

The pages associated with the process are used as objects, and the objects are connected in series to form different processes. POM is recognized as the best design pattern in the industry.

Advantages of POMs

(1) PO provides a mode of separating page element operations and business processes, which can make the code structure of the test clearer and more readable than before.

(2) It is more convenient to reuse objects and methods.

(3) The object library is a unified object library independent of test cases, and different test purposes can be achieved by integrating different tool classes. For example, integrated UnitTest can be used for unit testing, automation/functional testing, and JBehave/Cucumber can also be integrated for acceptance testing.

(4) Make the advantages of overall automated testing easier. If an element of a page needs to be changed, then you can directly change the encapsulated page element class without changing other test classes/codes that call it . In this way, the overall code maintenance cost will also be reduced.

The core of PO is the idea of ​​layering, which puts the elements belonging to the same page into a page class.

The above concepts and content are sourced from books.

Brief structure design

Logic code: the base class, which implements the encapsulation of all tool functions, similar to the keyword-driven design pattern.

Page object code: Based on the system page, the business operation is realized by calling the tool function, and the corresponding page object is generated.

Test code: based on test needs, component page objects, realize the automation of core processes, and execute test cases.

Test data: Provide the required test data for the execution of test cases.

POM complete framework

Base layer: Define the basic methods required by the project, especially some basic operations, such as element click operations, sendkeys operations, methods for calling JavaScript scripts, and other operations related to basic browsers.

Common layer: contains methods for processing Excel files, methods for obtaining information about project paths, test system URLs, and framework execution-related log functions.

Data layer: Store test data, where test data can be maintained. This storage is to make the project more maintainable and overall organized. Test data is sometimes the driving factor of automated testing, so the management and maintenance of Data is particularly important.

Logs layer: store the log files generated during the running of the project.

PageObject layer: This is the core layer of PO. This layer not only involves code technology, but also involves the analysis of project business, and then analyzes the project page.

Reports layer: store the test report files generated during project execution, and the test report is a summary of the test results.

TestCase layer: manage test cases and execute tests, which is equivalent to the general entrance of tests.

config.ini: Configuration items needed for the entire project.

Project combat

Take Baidu search as an example

We first create a simple project structure: base, data, page_object, cases, as shown in the following figure:

Create a new base_page.py file under the base package, create a new BasePage class under the base_page.py file as the base class, and provide various commonly used encapsulated functions, which are convenient for subsequent page object classes to call.

Commonly used functions in Selenium: element positioning, input, click, access URL, wait, close, etc. code show as below:

'''
基类:提供各个常用的已封装好的函数,便于后续的页面对象类进行调用。
selenium中常用的函数:元素定位、输入、点击、访问URL、等待、关闭
'''
from time import sleep
from selenium import webdriver

class BasePage:

    driver = webdriver.Chrome()

    # 访问URL
    def visit(self, url):
        self.driver.get(url)

    # 元素定位
    def locator(self, loc):
        return self.driver.find_element(*loc)

    # 输入
    def input(self, loc, txt):
        self.locator(loc).send_keys(txt)

    # 点击
    def click(self, loc):
        self.locator(loc).click()

    # 等待
    def wait(self, time):
        sleep(time)

    # 关闭
    def close(self):
        self.driver.quit()

Create a new search_page.py file under the page_object package, create a new SearchPage class under this file to inherit the BasePage class, call the methods in the base class to implement page operations, and generate corresponding page objects. code show as below:

'''
百度查询页面,搜索功能
'''
from selenium.webdriver.common.by import By
from base.base_page import BasePage
from selenium import webdriver

class SearchPage(BasePage):

    url = 'http://www.baidu.com'

    # 定位百度输入框
    search_input = (By.NAME, 'wd')
    # 定位百度一下按钮
    search_button = (By.ID, 'su')

    # 封装实现业务流程的函数
    def search(self, txt):
        self.visit(self.url)
        self.wait(2)
        self.input(self.search_input, txt)
        self.wait(2)
        self.click(self.search_button)

Create a new testcase.py file under the cases package as a test class to search for Selenium and Python respectively in Baidu. The code is as follows:​​​​​​​​

'''
测试类
'''
import unittest
from page_object.search_page import SearchPage
from selenium import webdriver

class Case(unittest.TestCase):

    def test_search1(self):
        driver = webdriver.Chrome()
        txt = 'selenium'
        SearchPage(driver).search(txt)
        
    def test_search2(self):
        driver = webdriver.Chrome()
        txt = 'python'
        SearchPage(driver).search(txt)

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

After running the above code, it is found that because the WebDriver is created twice, a Chrome browser will be opened every time the code is run. At the same time, there is redundancy in the code.

Optimization solution: @classmethod can be added as a precondition and postcondition, and the search conditions can also be completed in a data-driven manner to improve the readability and maintainability of the code.

Create a new searchTXT.yaml file under the data package, and store the search content as follows:

The code after optimization is as follows:​​​​​​​​

'''
测试类
'''
import unittest
from page_object.search_page import SearchPage
from selenium import webdriver
import ddt

@ddt.ddt()
class Case(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.driver = webdriver.Chrome()
        cls.se = SearchPage(cls.driver)

    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.quit()

    @ddt.file_data('../data/searchTXT.yaml')
    def test_search(self, txt):
        self.se.search(txt)

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

After running the above code, it was found that the test case was executed four times, and Selenium, Python, Java, and php were searched respectively, so that the data drive was realized, and the operation was also completed in a browser.

postscript

The above simply implements the POM design pattern, and completes the code writing of the base class, page object layer, data layer, and test layer. If readers are interested, they can complete the writing of the report layer, log layer, etc., and finally realize a complete framework. .

Finally: The complete software testing video tutorial below has been organized and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]

insert image description here

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

picture

Acquisition of complete set of information

Guess you like

Origin blog.csdn.net/weixin_50829653/article/details/131288519