app自动化POM模型,yuml文件,数据驱动

文章目录

  • POM
  • POM一般分为4层(和po模型差不多)
    • basepage(封装公共的属性和行为)
  • yaml 文件读取
  • 登录QQ
    • demo:
  • 数据驱动

POM

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

POM一般分为4层(和po模型差不多)

第一层:basepage层:描述每个页面相同的属性及行为
第二层:pageobject层(每个的独有特征及独有的行为)
第三层:testcase层(用例层,描述项目业务流程)
第四层:testdata(数据层)
app自动化POM模型,yuml文件,数据驱动_第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, 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)

    # 显示等待
    def webDriver(self, *log):
        WebDriverWait(self.driver, 10, 0.5).until(lambda f: f.driver.find_element(*log))

yaml 文件读取

yaml文件:数据层次清晰,可以跨平台,支持多种语言使用(可以适用于别的app)
读yaml文件,需要导入pyYAML(pip install pyYAML)

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


os.path.dirname(file)当前文件的上一级目录
os.path.abspath(path)找到路径的绝对路径
app自动化POM模型,yuml文件,数据驱动_第2张图片

登录QQ

demo:

pageobject

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


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

    def click_login(self):
        self.click(MobileBy.ACCESSIBILITY_ID, '同意')
        self.webDriver(MobileBy.ID, "com.tencent.mobileqq:id/btn_login")
        self.click(MobileBy.ID, "com.tencent.mobileqq:id/btn_login")

    def input_user_pwd(self):
        self.webDriver(MobileBy.ACCESSIBILITY_ID, '请输入QQ号码或手机或邮箱')
        self.input('393939399', MobileBy.ACCESSIBILITY_ID, '请输入QQ号码或手机或邮箱')
        self.input('9939393939393939', MobileBy.ID, 'com.tencent.mobileqq:id/password')
        self.click(MobileBy.ID, 'com.tencent.mobileqq:id/login')

** 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, 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)

    # 显示
    def webDriver(self, *log):
        WebDriverWait(self.driver, 10, 0.5).until(lambda f: f.find_element(*log))

testcase

import pytest, os,time
from appium import webdriver
from testdata.read_yaml import readYaml
from pageobject.login_page import Login_QQ


class TestQQClass():
    @classmethod
    def setup_class(cls) -> None:
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'testdata/data.yaml')
        cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", readYaml(path).get("caps"))
        print(readYaml(path).get("caps"))

    def test_01(self):
        l = Login_QQ(self.driver)
        l.click_login()
        l.input_user_pwd()

    @classmethod  # 必须以@classmethod,测试用例之后执行,只执行一次
    def teardown_class(cls) -> None:
        time.sleep(10)
        cls.driver.quit()

if __name__ == '__main__':
    pytest.main(['test01.py'])

** testdata**

import yaml

def readYaml(path):
    with open(path, "r", encoding="utf-8") as file:
        data = yaml.load(stream=file, Loader=yaml.FullLoader)
    return data

yaml data

caps:
  platformName: Android
  deviceName: 2ad7a176
  appPackage: com.tencent.mobileqq
  appActivity: com.tencent.mobileqq.activity.SplashActivity

数据驱动

在pytest中使用@pytest.mark.parametrize()修饰器
app自动化POM模型,yuml文件,数据驱动_第3张图片

你可能感兴趣的:(自动化,python,运维)