APP automated testing, Appium+PO mode+Pytest framework actual combat - project case


foreword

PO mode: Page Object, PO mode is one of the best design patterns for automated test project development practices.
Core idea: Reduce redundant codes by encapsulating interface elements. At the same time, in later maintenance, if the position of elements changes, you only need to adjust the code encapsulated on the page to improve the maintainability and readability of test cases.

Advantages:
Redundant code is reduced;
business code and test code are separated to reduce coupling;
maintenance cost is low;

Disadvantages:
complex structure: modular split based on process

Example: Send SMS automatically

Method: Appium+PO mode+Pytest framework data parameterization
base module: pre-code and basic operations, base_driver.py corresponds to opening the driver, base_action.py corresponds to element positioning, button click and input.

page module: Corresponding to the operation page, consider how many pages are needed in the finger test process, and create as many files in the page module. page.py has a unified entry, write as many functions as there are pages, and create corresponding objects.

scripts module: test scripts.
pytest.ini: configuration file.

base_action.py:

from selenium.webdriver.support.wait import WebDriverWait
 
 
class BaseAction:
 
    def __init__(self, driver):
        self.driver = driver
 
    def find_element(self, location, timeout=10, poll=1):
        """
        :param location: 元素位置
        :param timeout: 设置10秒
        :param poll: 多少秒找一次
        :return:
        """
        location_by, location_value = location
        wait = WebDriverWait(self.driver, timeout, poll)
        return wait.until(lambda x: x.find_element(location_by, location_value))
 
    def find_elements(self, location, timeout=10, poll=1):
        location_by, location_value = location
        wait = WebDriverWait(self.driver, timeout, poll)
        return wait.until(lambda x: x.find_elements(location_by, location_value))
 
    def click(self, location):
        self.find_element(location).click()
 
    def input(self, location, text):
        self.find_element(location).send_keys(text)

base_driver.py:

from appium import webdriver
 
 
def init_driver():
    desired_caps = dict()
    # 设备信息
    desired_caps["platformName"] = "Android"
    desired_caps["platformVersion"] = "5.1"
    desired_caps["deviceName"] = "192.168.56.101:5555"
    # app信息
    desired_caps["appPackage"] = "com.android.mms"
    desired_caps["appActivity"] = ".ui.ConversationList"
 
    return webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)

message_list_page.py:

from selenium.webdriver.common.by import By
 
from base.base_action import BaseAction
 
 
class MessageListPage(BaseAction):
    # 新建短信按钮
    new_message_button = By.ID, "com.android.mms:id/action_compose_new"
 
    def click_new_message(self):
        self.click(self.new_message_button)

new_message_page.py:

from selenium.webdriver.common.by import By
 
from base.base_action import BaseAction
 
 
class NewMessagePage(BaseAction):
    # 接受者特征
    recipients_edit_text = By.ID, "com.android.mms:id/recipients_editor"
    # 内容特征
    content_edit_text = By.ID, "com.android.mms:id/embedded_text_editor"
    # 发送按钮
    send_button = By.XPATH, "//*[@content-desc='发送']"
 
    def input_recipients(self, text):
        self.input(self.recipients_edit_text, text)
 
    def input_content(self, text):
        self.input(self.content_edit_text, text)
 
    def click_send(self):
        self.click(self.send_button)

page.py:

from page.message_list_page import MessageListPage
from page.new_message_page import NewMessagePage
 
 
class Page:
 
    def __init__(self, driver):
        self.driver = driver
 
    @property
    def message_list(self):
        return MessageListPage(self.driver)
 
    @property
    def new_message(self):
        return NewMessagePage(self.driver)

test_message.py:

import time
import pytest
from base.base_driver import init_driver
from page.page import Page
 
 
class TestMessage:
 
    def setup(self):
        self.driver = init_driver()
        self.page = Page(self.driver)
 
    def teardown(self):
        time.sleep(3)
        self.driver.quit()
 
    @pytest.mark.parametrize(('phone', 'content'), [('18588888888', "HELLO"),('18577778888', "您好!")])
    def test_send_message(self, phone, content):
        # 主页-点击短信,新建短信
        self.page.message_list.click_new_message()
        # 新建短信-输入 接收人
        self.page.new_message.input_recipients(phone)
        # 新建短信-输入 内容
        self.page.new_message.input_content(content)
        # 新建短信-点击发送
        self.page.new_message.click_send()
 
 
if __name__ == '__main__':
    pytest.main([])

pytest.ini:

[pytest]
# 添加命令行参数
addopts = -vs --html=report/report.html --reruns 0
# 文件搜索路径
testpaths = ./scripts
# 文件名称
python_files = test_*.py
# 类名称
python_classes = Test*
# 方法名称
python_functions = test_*
The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

As long as you are willing to work hard and don't give up on pursuing your dreams, you will be able to achieve success. Even if the road is bumpy, don't be discouraged, because this is an opportunity to grow and improve. Believe in yourself, go forward bravely, you will create your own brilliance!

Only by constantly working hard can we surpass ourselves and break through the limit. Every step forward is a step closer to success. Persevere in pursuing your dreams, and your efforts will always pay off. No matter how many setbacks you encounter, please always remain brave and firm in your beliefs!

Only by doing our best can we not leave regrets; only by surpassing ourselves can we achieve breakthroughs; only by persevering can we achieve success. No matter what challenges you face, you must move forward bravely and never give up. I believe you can achieve your own brilliance!

Guess you like

Origin blog.csdn.net/x2waiwai/article/details/131154193