POM(page object model)页面对象模型,主要应用于UI自动化测试框架的搭建,主流设计模式之一,页面对象模型:结合面向对象编程思路:把项目的每个页面当做一个对象进行编程。
第一层:basepage层:描述每个页面相同的属性及行为
第二层:pageobject层(每个的独有特征及独有的行为)
第三层:testcase层(用例层,描述项目业务流程)
第四层:testdata(数据层)
yaml文件:数据层次清晰,可以跨平台,支持多种语言使用(可以适用于别的app)
优化代码:提取basepage中的配置客户端数据(将配置的数据放在yaml中) 创建config--config.yam
yaml文件展示(主要存放用于连接的参数,注意这样缩进打印结果是字典套字典)
- caps:
- platformName: Android
- deviceName: emulator-5554
- appPackage: com.vphone.launcher
- appActivity: com.vphone.launcher.Launcher
读取模块
首先需要导入第三方包 :
pip install pyYAML
(打印结果是字典套字典)
- import yaml
- def readYaml(path):
- with open(path,"r",encoding="utf-8") as file:
- data = yaml.load(stream=file,Loader=yaml.FullLoader)
- return data
-
- print(readYaml(r'F:\Python_Project\app_auto_po\data\data.yaml'))
-
- '''
- {'caps': {'platformName': 'Android', 'deviceName': '127.0.0.1:62001', 'appPackage': 'com.vphone.launcher', 'appActivity': 'com.vphone.launcher.Launcher'}}
- '''
三、实例
basepage (存放公用方法)
- from appium.webdriver.common.touch_action import TouchAction
- 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_e(self,*loc):
- self.locator(*loc).click()
- #点击坐标轴
- def click_l(self,n,m):
- TouchAction(self.driver).tap(x =n,y = m).perform()
- #滑动(上下左右滑动)
- 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)
- data(存放数据)
pageone
- from basepage.base import BasePages
- from appium.webdriver.common.mobileby import MobileBy
- class PageOne(BasePages):
- def __init__(self,driver):
- BasePages.__init__(self,driver)
- #元素定位
- def danji(self):
- self.click(MobileBy.XPATH,"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.Button[1]")
pagetwo
- from basepage.base import BasePages
- from appium.webdriver.common.mobileby import MobileBy
- class PageTwo(BasePages):
- def __init__(self,driver):
- BasePages.__init__(self,driver)
- #点击搜索框
- def dianji(self):
- self.click(MobileBy.ID,"fengchedongman.apps.com:id/lx")
- #输入内容
- def shuru(self,text):
- self.input(text,MobileBy.ID,'fengchedongman.apps.com:id/a0o')
- #点击搜索
- def sousuo(self):
- self.click(MobileBy.ID,'fengchedongman.apps.com:id/a0j')
pagethree
- from basepage.base import BasePages
- from appium.webdriver.common.mobileby import MobileBy
- class PageThree(BasePages):
- def __init__(self,driver):
- BasePages.__init__(self,driver)
- #添加到书架
- def tianjia(self):
- self.click(MobileBy.ID,'fengchedongman.apps.com:id/gu')
po模型代码
- from appium import webdriver
- import unittest,time
- from pages.pageone import PageOne
- from pages.pagetwo import PageTwo
- from pages.pagethree import PageThree
- class TestClass(unittest.TestCase):
- @classmethod
- def setUpClass(cls) -> None:
- caps = {}
- caps["deviceName"] = "emulator-5554"
- caps["platformName"] = "Android"
- caps["appPackag"] = "fengchedongman.apps.com"
- caps["appActivity"] = "com.Stub.App"
- cls.driver=webdriver.Remote("http://localhost:4723/wd/hub", caps)
- cls.driver.implicitly_wait(30)
- def test_001(self):
- p1=PageOne(self.driver)
- p1.danji()
- def test_002(self):
- p2=PageTwo(self.driver)
- p2.dianji()
- p2.shuru("逆天邪神")
- p2.sousuo()
- def test_003(self):
- p3=PageThree(self.driver)
- p3.tianjia()
- @classmethod
- def tearDownClass(cls) -> None:
- time.sleep(15)
- if __name__ == '__main__':
- unittest.main()