自动化POM(app)

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

二、POM一版分为四层
第一层:basepage层:描述每个页面相同的属性及行为
第二层:pageobject层(每个的独有特征及独有的行为)
第三层:testcase层(用例层,描述项目业务流程)
第四层:testdata(数据层)

三:代码实现
1.非po模型(夜神中qq登录)
from appium import webdriver
caps = {}
caps["platformName"] = "Android"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.tencent.mobileqq"
caps["appActivity"] = "com.tencent.mobileqq.activity.LoginActivity"
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
driver.implicitly_wait(30)

导航界面

el2 = driver.find_element_by_id("com.tencent.mobileqq:id/btn_login")
el2.click()

登录界面

el3 = driver.find_element_by_accessibility_id("请输入QQ号码或手机或邮箱")
el3.clear()
el3.send_keys("766603163")
el4 = driver.find_element_by_accessibility_id("密码 安全")
el4.clear()
el4.send_keys("lly19891024lly")
el5 = driver.find_element_by_accessibility_id("登 录")
el5.click(
2.po模型操作
1). basepage(封装公共的属性和行为)

from selenium.webdriver.support.wait import WebDriverWait

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, text, loc):
self.locator(
loc).send_keys(text)
# 点击
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)
2).pageobject(导航模块和登录模块)

a.导航页面

from basepage.BasePages import BasePages
from appium.webdriver.common.mobileby import MobileBy

class DaoHangpage(BasePages):
def init(self,driver):
BasePages.init(self,driver)

def click_button(self):
    self.click(MobileBy.XPATH,"//*[contains(@text,'登录')]")

b.登录页面

from basepage.BasePages import BasePages
from appium.webdriver.common.mobileby import MobileBy

class LoginPage(BasePages):
def send_username(self, text):
self.input(text, MobileBy.ACCESSIBILITY_ID, "请输入QQ号码或手机或邮箱")

def send_password(self, text):
    self.input(text, MobileBy.ACCESSIBILITY_ID, "密码 安全")

def click_login(self):
    self.click(MobileBy.ACCESSIBILITY_ID, "登 录")

3).testcase(执行测试用例)
from pageobject.daohang_page import DaoHangpage
from pageobject.login_page import LoginPage
from testdata.readyaml import readyaml
from appium import webdriver
import pytest,time,os

class TestClass():
@classmethod
def setup_class(cls) -> None:
cap={}
cap["platformName"]= "Android"
cap["deviceName"]="127.0.0.1:62001"
cap["appPackage"]= "com.tencent.mobileqq"
cap["appActivity"]="com.tencent.mobileqq.upgrade.activity.UpgradeActivity"
cls.driver=webdriver.Remote("http://localhost:4723/wd/hub",cap)
cls.driver.implicitly_wait(200)
def test_01(self):
daohang=DaoHangpage(self.driver)
daohang.click_button()
def test_02(self):
login=LoginPage(self.driver)
login.send_username("*********")
login.send_password("*********")
login.click_login()
@classmethod
def teardown_class(cls) -> None:
time.sleep(10)
cls.driver.quit()

if name == 'main':
pytest.main(["test_001.py"])

3.引入yaml文件
yaml 文件:数据层次清晰,可以跨平台,支持多种语言使用 ( 可以适用于别的 app)
优化代码:提取 basepage 中的配置客户端数据(将配置的数据放在 yaml 中) 创建 config--
config.yaml
1).调用 yaml 文件,需要导入 pip install pyYAML 创建 common--read-yaml.py

import yaml,os
def readyaml(path):
with open(path,'r',encoding='utf-8') as f:
data=yaml.load(stream=f,Loader=yaml.FullLoader)
return data

os.path.dirname(file)当前文件的上一级目录

os.path.abspath(path)找到路径的绝对路径

rpath=os.path.abspath(os.path.dirname(os.path.dirname(file)))
ph=os.path.join(rpath,'testdata/yaml.yaml')
2).修改单元测试模块代码

from pageobject.daohang_page import DaoHangpage
from pageobject.login_page import LoginPage
from testdata.readyaml import readyaml
from appium import webdriver
import pytest,time,os

class TestClass:
@classmethod
def setup_class(cls) -> None:
rpath = os.path.abspath(os.path.dirname(os.path.dirname(file)))
ph = os.path.join(rpath, 'testdata/yaml.yaml')
data=readyaml(ph)
cls.driver=webdriver.Remote("http://localhost:4723/wd/hub",data['caps'])
cls.driver.implicitly_wait(30)
def test_01(self):
daohang=DaoHangpage(self.driver)
daohang.click_button()
def test_02(self,user,password):
login=LoginPage(self.driver)
login.send_username("*********")
login.send_password("*********")
login.click_login()
@classmethod
def teardown_class(cls) -> None:
cls.driver.quit()

if name == 'main':
pytest.main(["test_001"])
4.添加数据驱动
from pageobject.daohang_page import DaoHangpage
from pageobject.login_page import LoginPage
from testdata.readyaml import readyaml
from appium import webdriver
import pytest,time,os

class TestClass:
@classmethod
def setup_class(cls) -> None:
rpath = os.path.abspath(os.path.dirname(os.path.dirname(file)))
ph = os.path.join(rpath, 'testdata/yaml.yaml')
data=readyaml(ph)
cls.driver=webdriver.Remote("http://localhost:4723/wd/hub",data['caps'])
cls.driver.implicitly_wait(30)
def test_01(self):
daohang=DaoHangpage(self.driver)
daohang.click_button()
@pytest.mark.parametrize("username,password", [("*********", "*********")])
def test_02(self,user,password):
login=LoginPage(self.driver)
login.send_username(username)
login.send_password(password)
login.click_login()
@classmethod
def teardown_class(cls) -> None:
cls.driver.quit()

if name == 'main':
pytest.main(["test_001"])

你可能感兴趣的:(自动化POM(app))