大前端

前端学习之家-大前端

app自动化POM模型

POM

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 文件读取
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)找到路径的绝对路径

登录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()修饰器\

发表评论:

Copyright Your WebSite.Some Rights Reserved.