Appium自动化测试(五)——PO模式(一):短信案例

PO模式:Page Object,PO模式是自动化测试项目开发实践的最佳设计模式之一。

核心思想:通过对界面元素的封装减少冗余代码,同时在后期维护中,若元素位置发生变化,只需要调整页面封装的代码,提高测试用例的可维护性、可读性。

优点: 

  • 减少了冗余代码
  • 业务代码和测试代码被分开,降低耦合性
  • 维护成本低

缺点:

  • 结构复杂:基于流程做了模块化的拆分

案例一:自动发送短信

方法:Appium+PO模式+Pytest框架数据参数化

模块分布

项目模块
项目模块
  •  base模块:前置代码和基本操作,base_driver.py对应打开driver,base_action.py对应元素定位、点击按钮和输入。
  • page模块:对应操作页面,考虑手指测试的过程需要用到多少个页面,就在page模块中创建多少个文件。page.py统一入口,有多少个页面,就写多少个函数,并创建对应的对象。
  • scripts模块:测试脚本。
  • pytest.ini:配置文件。

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_*

Readme

短信案例

需求:
在《短信》应用中,进入发送短信页面,在姓名和内容栏中,输入对应的数据,并点击发送。
包名界面名:com.android.mms/.ui.ConversationList
发送短信页面标识:resource-id,com.android.mms:id/action_compose_new
接收者标识:resource-id,com.android.mms:id/recipients_editor
内容标识:resource-id,com.android.mms:id/embedded_text_editor
发送按钮标识:content-desc,发送

数据:
[18588888888, HELLO]
[18577778888, 您好!]

运行test_message.py

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值