Do you really know how to automate? Web automated testing-PO mode practice, a thorough article...


Preface

PO mode

The Page Object (PO for short) pattern is the most popular in Selenium practice, and is the most familiar and respected design pattern in automated testing. When designing automated tests, abstract the page elements and their operation methods according to the page, separate them into certain objects, and then organize them.

One of the most troublesome problems in web automation is that the page changes. If the PO design pattern is not used, a change in the page means that the previous element positioning and even the element operation method cannot be used and need to be revised.

You need to find out the element positioning methods and element operation methods that need to be modified one by one from the test script, and then modify them one by one. Such automated scripts are not only cumbersome, but also extremely costly to maintain.

The page object mode can solve this problem very well. Advantages:
reduce code redundancy;
separate business and implementation;
reduce maintenance costs;

So what is the Page Object mode? As the name implies, it is the page object. In actual automated testing, scripts are generally divided into three layers: Object layer: used to
store page elements and positioning
logic layer: used to store some packaged Functional use case module
Business layer: used to store the operational part of our real test cases

In addition to the above three layers, there is also a basic layer. The basic layer is mainly for some common methods of selenium. It is re-encapsulated according to actual business needs, such as clicking, input and other operations, adding some waiting, log input, screenshots and other operations to facilitate the future. Check the running status of the script and troubleshoot problems.

base layer

The base layer class name is generally named BasePage. Subsequent object layer operations on elements inherit this base class. The following takes click and input as an example:

# basepage.py
import os
import time
import datetime
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.logging import log
from common.constant import IMG_DIR


class BasePage:

    def __init__(self, driver: WebDriver):
        self.driver = driver

    def wait_ele_visible(self, loc, img_desc, timeout=20, frequency=0.5):
        """等待元素可见"""
        try:
            WebDriverWait(self.driver, timeout, frequency).until(EC.visibility_of_element_located(loc))
            log.info("等待:{} - 元素{}可见成功。".format(img_desc, loc))
        except:
            log.exception("等待:{} - 元素{}可见失败!".format(img_desc, loc))
            self.save_img(img_desc)
            raise

    def get_element(self, loc, img_desc):
        """查找元素"""
        try:
            ele = self.driver.find_element(*loc)
        except:
            log.exception("查找:{} - 元素{}失败!".format(img_desc, loc))
            self.save_img(img_desc)
            raise
        else:
            log.info("查找:{} - 元素{}成功".format(img_desc, loc))
            return ele

    def click_element(self, loc, img_desc, timeout=20, frequency=0.5):
        """点击元素"""
        self.wait_ele_visible(loc, img_desc, timeout, frequency)
        ele = self.get_element(loc, img_desc)
        try:
            ele.click()
            log.info("点击:{} - 元素{}成功".format(img_desc, loc))
        except:
            log.exception("点击:{} - 元素{}失败!".format(img_desc, loc))
            self.save_img(img_desc)
            raise

    def input_text(self, loc, value, img_desc, timeout=20, frequency=0.5):
        """在元素中输入文本"""
        self.wait_ele_visible(loc, img_desc, timeout, frequency)
        ele = self.get_element(loc, img_desc)
        try:
            ele.send_keys(value)
            log.info("输入:在{} - 元素{}输入文本值({})成功".format(img_desc, loc, value))
        except:
            log.exception("输入:在{} - 元素{}输入文本值({})失败!".format(img_desc, loc, value))
            self.save_img(img_desc)
            raise

    def save_img(self, img_description):
        """保存异常截图"""
        now = time.strftime("%Y-%m-%d %H-%M-%S ", time.localtime())
        img_path = os.path.join(IMG_DIR, now + img_description + '.png')
        try:
            self.driver.save_screenshot(img_path)
        except:
            log.exception("异常截图失败!")
        else:
            log.info("异常截图成功,截图存放在{}".format(img_path))

Take click_element() as an example. Here, the waiting operation, log input, and exception screenshots are added to the secondary encapsulation. When you click on the element later, you can directly call click_element() to get it right in one step. There is no need to consider waiting, logs, and exceptions. , everything has been taken care of here.

Although writing basic pages will be time-consuming in the early stages, as long as the foundation is laid, subsequent maintenance work will be much easier. The above is just an example and can be optimized according to your actual needs.

Object layer and logic layer

The object layer stores page element positioning, and the logic layer stores element operation methods (page functions). Element positioning can be maintained in a separate module according to actual needs, or can be stored in excel for centralized management;

The following demonstrates that element positioning and element operation methods are stored in a module, one module per page. If subsequent page elements change, you only need to modify the corresponding positioning expression or operation method in this module.

The demonstration takes Baidu homepage as an example:

# baidu_page.py

from selenium.webdriver.common.by import By
from common.basepage import BasePage


class LoginPage(BasePage):

    login_btn = (By.XPATH, '//div[@id="u1"]//a[@name="tj_login"]')  # 登录按钮
    username_login_btn = (By.ID, 'TANGRAM__PSP_11__footerULoginBtn')    # 用户名登录按钮
    user_input = (By.ID, 'TANGRAM__PSP_11__userName')  # 用户信息输入框
    pwd_input = (By.ID, 'TANGRAM__PSP_11__password')  # 密码输入框
    login_submit = (By.ID, 'TANGRAM__PSP_11__submit')   # 登录提交按钮

    def login(self, user, pwd):
        """
        百度用户名登录
        :param user: 手机/邮箱/用户名
        :param pwd: 密码
        :return:
        """
        self.click_element(self.login_btn, '百度-登录')
        self.click_element(self.username_login_btn, '百度登录-用户名登录')
        self.input_text(self.user_input, user, '用户名登录-手机/邮箱/用户名')
        self.input_text(self.pwd_input, pwd, '用户名登录-密码')
        self.click_element(self.login_submit, '用户名登录-登录')

Business Layer

Used to store real test case operations. Element positioning and page functions will not appear here. All operations are directly called to the logic layer.

Test case = function of the test object + test data. The following takes Baidu login as an example (for demonstration, abbreviated):

import unittest
import pytest
import ddt
from selenium import webdriver
from PageObjects.baidu_login_page import LoginPage
from testdatas import common_datas as com_d
from testdatas import login_data as lo_d
from common.logging import log


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

    def setUp(self):
        log.info("-------用例前置工作:打开浏览器--------")
        self.driver = webdriver.Chrome()
        self.driver.get(com_d.baidu_url)
        self.driver.maximize_window()

    def tearDown(self):
        self.driver.quit()
        log.info("-------用例后置工作:关闭浏览器--------")

    @pytest.mark.smoke
    def test_login_success(self):
        # 用例:登录页的登录功能
        # 步骤
        LoginPage(self.driver).login(lo_d.success_data['user'], lo_d.success_data['pwd'])
        # 断言.....

operation result:

Testing started at 11:50 ...
C:\software\python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.3\helpers\pycharm\_jb_unittest_runner.py" --path D:/learn/test/testcases/test_baidu_login.py
Launching unittests with arguments python -m unittest D:/learn/test/testcases/test_baidu_login.py in D:\learn\test\testcases

Process finished with exit code 0
2021-03-14 11:50:47,238-【test_baidu_login.py-->line:27】-INFO:-------用例前置工作:打开浏览器--------
2021-03-14 11:50:51,327-【basepage.py-->line:38】-INFO:等待:百度-登录 - 元素('xpath', '//div[@id="u1"]//a[@name="tj_login"]')可见成功,耗时0:00:00.056843秒
2021-03-14 11:50:51,339-【basepage.py-->line:77】-INFO:查找:百度-登录 - 元素('xpath', '//div[@id="u1"]//a[@name="tj_login"]')成功
2021-03-14 11:50:51,414-【basepage.py-->line:86】-INFO:点击:百度-登录 - 元素('xpath', '//div[@id="u1"]//a[@name="tj_login"]')成功
2021-03-14 11:50:53,463-【basepage.py-->line:38】-INFO:等待:百度登录-用户名登录 - 元素('id', 'TANGRAM__PSP_11__footerULoginBtn')可见成功,耗时0:00:02.048293秒
2021-03-14 11:50:53,474-【basepage.py-->line:77】-INFO:查找:百度登录-用户名登录 - 元素('id', 'TANGRAM__PSP_11__footerULoginBtn')成功
2021-03-14 11:50:53,535-【basepage.py-->line:86】-INFO:点击:百度登录-用户名登录 - 元素('id', 'TANGRAM__PSP_11__footerULoginBtn')成功
2021-03-14 11:50:53,576-【basepage.py-->line:38】-INFO:等待:用户名登录-手机/邮箱/用户名 - 元素('id', 'TANGRAM__PSP_11__userName')可见成功,耗时0:00:00.040890秒
2021-03-14 11:50:53,584-【basepage.py-->line:77】-INFO:查找:用户名登录-手机/邮箱/用户名 - 元素('id', 'TANGRAM__PSP_11__userName')成功
2021-03-14 11:50:53,714-【basepage.py-->line:98】-INFO:输入:在用户名登录-手机/邮箱/用户名 - 元素('id', 'TANGRAM__PSP_11__userName')输入文本值(15692004245)成功
2021-03-14 11:50:53,759-【basepage.py-->line:38】-INFO:等待:用户名登录-密码 - 元素('id', 'TANGRAM__PSP_11__password')可见成功,耗时0:00:00.043882秒
2021-03-14 11:50:53,771-【basepage.py-->line:77】-INFO:查找:用户名登录-密码 - 元素('id', 'TANGRAM__PSP_11__password')成功
2021-03-14 11:50:53,925-【basepage.py-->line:98】-INFO:输入:在用户名登录-密码 - 元素('id', 'TANGRAM__PSP_11__password')输入文本值(phang0209)成功
2021-03-14 11:50:53,958-【basepage.py-->line:38】-INFO:等待:用户名登录-登录 - 元素('id', 'TANGRAM__PSP_11__submit')可见成功,耗时0:00:00.031914秒
2021-03-14 11:50:53,969-【basepage.py-->line:77】-INFO:查找:用户名登录-登录 - 元素('id', 'TANGRAM__PSP_11__submit')成功
2021-03-14 11:50:54,051-【basepage.py-->line:86】-INFO:点击:用户名登录-登录 - 元素('id', 'TANGRAM__PSP_11__submit')成功
2021-03-14 11:50:56,426-【test_baidu_login.py-->line:35】-INFO:-------用例后置工作:关闭浏览器--------


Ran 1 test in 9.191s

OK

Judging from the output log, every step of the operation is clearly visible, and problems can be quickly located. These can be optimized according to actual needs.

The following is the most comprehensive software testing engineer learning knowledge architecture system diagram in 2023 that I compiled.

1. Python programming from entry to proficiency

Please add image description

2. Practical implementation of interface automation projects

Please add image description

3. Web automation project actual combat

Please add image description

4. Practical implementation of App automation project

Please add image description

5. Resumes of first-tier manufacturers

Please add image description

6. Test and develop DevOps system

Please add image description

7. Commonly used automated testing tools

Please add image description

8. JMeter performance test

Please add image description

9. Summary (little surprise at the end)

Embrace difficulties, chase dreams, struggle is the key to achievement; perseverance, transcending yourself, success is forged by continuous hard work. Believe in your own abilities, move forward bravely, use sweat and wisdom to write a glorious chapter of life, and bloom your own light and glory.

Sharpening the will, breaking through the shackles, and struggling are the only ways to create glory; persist in dreams and transcend the ordinary, and achievement depends on burning inner enthusiasm. Believe in your own strength, pursue excellence, use sweat and wisdom to draw a magnificent picture of life, and compose your own immortal legend.

Burn the passion of the soul, let go of the wings of dreams, struggle is the rhythm of creating miracles; persist in faith and move forward bravely, achievements belong to perseverance. Believe in your own talents and pursue excellence.

Guess you like

Origin blog.csdn.net/m0_60054525/article/details/132043777