app端自动化POM

一:POM及POM设计原理

POM(page object model)页面对象模型,主要应用于UI自动化测试框架的搭建,主流设计模式之 一,页面对象模型:结合面向对象编程思路:把项目的每个页面当做一个对象进行编程

python基础:什么对象?

python中对象= 属性+行为 通过类定义=具有相同属性+相同行为对象集合

二:POM一版分为四层

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

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

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

第四层:testdata(数据层)

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)

3.2.2:业务页代码

#page1

from basepage.basepage import BasePages

from appium.webdriver.common.touch_action import TouchAction
class OneClass(BasePages):
    def __init__(self,driver):
        BasePages.__init__(self,driver)

    def srk_click(self):
        TouchAction(self.driver).tap(x=790,y=390).perform()

#page2
from basepage.basepage import BasePages

class TwoClass(BasePages):
    def __init__(self,driver):
        BasePages.__init__(self,driver)

    def send_srk(self,text,*args):
        self.input(text,*args)

    def click_ss(self,*args):
        self.click(*args)
#测试代码
from page.pageone import OneClass
from page.twopage import TwoClass
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
import unittest,time
class TestClass(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        caps = {}
        caps["platformName"] = "Android"
        caps["deviceName"] = "127.0.0.1:62001"
        caps["appPackage"] = "com.taobao.taobao"
        caps["appActivity"] = "com.taobao.tao.TBMainActivity"
        cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        cls.driver.implicitly_wait(30)

    def test001(self):
        one = OneClass(self.driver)
        one.srk_click()
    def test002(self):
        two = TwoClass(self.driver)
        two.send_srk("手机",MobileBy.ID,"com.taobao.taobao:id/searchEdit")
        two.click_ss(MobileBy.ID,'com.taobao.taobao:id/searchbtn')

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

app端自动化POM_第1张图片

四:引入yuml文件 

yaml文件:数据层次清晰,可以跨平台,支持多种语言使用(可以适用于别的app) 优化代码:提取basepage中的配置客户端数据(将配置的数据放在yaml中) 创建config-- config.yaml

caps:

platformName: Android

deviceName: 127.0.0.1:62001

appPackage: com.tencent.mobileqq

appActivity: com.tencent.mobileqq.activity.LoginActivity

调用yaml文件,需要导入pip install pyYAML 创建common--readyaml.p

import yaml,os
def readYaml(path):
with open(path,"r",encoding="utf-8") as file:
data = yaml.load(stream=file,Loader=yaml.FullLoader)
return data
if __name__ == '__main__':
#os.path.dirname(__file__)当前文件的上一级目录
#os.path.abspath(path)找到路径的绝对路径
rootpath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
dd = os.path.join(rootpath , "config\config.yaml")
print(dd)
print(readYaml(dd))python

修改单元测试模块代码

import os
from day02.common.read_yaml import readYaml
from day02.pageobjects.daohang_page import DaoHangPage
from day02.pageobjects.login_page import LoginPage
from appium import webdriver
import pytest
import time
class TestClass():
@classmethod
def setup_class(cls) -> None:

rootpath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
dd = os.path.join(rootpath,"config\config.yaml")
data = readYaml(dd)
cls.driver = webdriver.Remote("http://localhost:4723/wd/hub",
data["caps"])
cls.driver.implicitly_wait(30)

    def test001(self):
        one = OneClass(self.driver)
        one.srk_click()
    def test002(self):
        two = TwoClass(self.driver)
        two.send_srk("手机",MobileBy.ID,"com.taobao.taobao:id/searchEdit")
        two.click_ss(MobileBy.ID,'com.taobao.taobao:id/searchbtn')

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

你可能感兴趣的