app端自动化PO模型

该博客介绍了Page Object Model(POM)模型在Appium自动化测试中的实现,包括四个层次:BasePage、PageObject、Testcase和TestData。通过示例代码展示了如何使用POM模型进行页面元素定位、输入、点击等操作,以及如何编写单元测试。此外,还提到了数据驱动的概念,并展示了如何读取YAML文件。整个示例涵盖了Android平台上QQ应用的登录流程。
摘要由CSDN通过智能技术生成

POM一版分为四层

第一层:basepage层:描述每个页面相同的属性及行为

第二层:pageobject层(每个的独有特征及独有的行为)

第三层:testcase层(用例层,描述项目业务流程)

第四层:testdata(数据层) 

 非po模型 

# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
 
from appium import webdriver
 
caps = {}
caps["platformName"] = "Android"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.android.settings"
caps["appActivity"] = "com.android.settings.Settings"
caps["platformVersion"] = "5.1.1"
 
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
 
el1 = driver.find_element_by_id("com.tencent.mobileqq:id/btn_login")
el1.click()
el2 = driver.find_element_by_accessibility_id("请输入QQ号码或手机或邮箱")
el2.send_keys("3312507327")
el3 = driver.find_element_by_accessibility_id("密码 安全")
el3.send_keys("lwj1314520")
el4 = driver.find_element_by_accessibility_id("登 录")
el4.click()
 
driver.quit()

po模型操作

 basepage(封装公共的属性和行为)

class BasePages:
    def __init__(self,driver):
        self.driver = driver
#元素定位
    def locator(self,*loc):
        return self.driver.find_element(*loc)
#清空
    def clear(self,*loc):
        self.locator(*loc).clear()
#输入
    def input(self,test,*loc):
        self.locator(*loc).send_keys(test)
#点击
    def click(self,*loc):
        self.locator(*loc).click()
#滑动(上下左右滑动)
    def swipe(self,start_x,start_y,end_x,end_y,duration=0):
#获取屏幕的尺寸
        window_size = self.driver.get_window_size()
        x = window_size["width"]
        y = window_size["height"]
        self.driver.swipe(start_x=x*start_x,
                            start_y=y*start_y,
                            end_x= x*end_x,
                            end_y=y*end_y,
                            duration=duration)

 daohang_page.py(导航模块)

from basepage.basepages import BasePages
from appium.webdriver.common.mobileby import MobileBy
class DaHangpage(BasePages):
    def __init__(self,driver):
        BasePages.__init__(self,driver)
    def click_login(self):
        self.click(MobileBy.ID,"com.tencent.mobileqq:id/btn_login")

输入账号密码模块

from basepage.basepages import BasePages
from appium.webdriver.common.mobileby import MobileBy
class LoginPage(BasePages):
    def send_zh(self,test):
        self.input(test,MobileBy.ACCESSIBILITY_ID,"请输入QQ号码或手机或邮箱")
    def send_mm(self,test):
        self.input(test,MobileBy.ACCESSIBILITY_ID,"密码 安全")
    def click_login_button(self):
        self.click(MobileBy.ACCESSIBILITY_ID,"登 录")

单元测试模块

import unittest
from appium import webdriver
from pageobject.page import DaHangpage
from pageobject.pagedl import LoginPage
class TestClass(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        caps = {}
        caps["platformName"] = "Android"
        caps["deviceName"] = "127.0.0.1:62001"
        caps["appPackage"] = "com.tencent.mobileqq"
        caps["appActivity"] = "com.tencent.mobileqq.activity.LoginActivity"
        caps["platformVersion"] = "5.1.1"
 
        cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        cls.driver.implicitly_wait(20)
    def test_01(self):
        dh = DaHangpage(self.driver)
        dh.click_login()
    def test_02(self):
        lg = LoginPage(self.driver)
        lg.send_zh("3312507327")
        lg.send_mm("lwj1314520")
        lg.click_login_button()
    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.quit()
if __name__ == '__main__':
    unittest.main()

引入yuml文件

caps:
  platformName: Android
  deviceName: 127.0.0.1:62001
  appPackage: com.tencent.mobileqq
  appActivity: com.tencent.mobileqq.activity.LoginActivity

调用yaml文件,需要导入pip install pyYAML

def yeml_method(path):
    with open(path, "r", encoding="utf-8") as file:
        data = yaml.load(stream=file, Loader=yaml.FullLoader)
        return data
# print(os.path.dirname(__file__))
# print(os.path.dirname(os.path.dirname(__file__)))
# print(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
# print(os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))),"testdata/yeml_read"))
print(yeml_method(os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))),"testdata/yemlread")))

 

 数据驱动

在pytest中使用@pytest.mark.parametrize()修饰器

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值