栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

APP端自动化-POM

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

APP端自动化-POM

一、POM及POM设计原理

POM(page object model)页面对象模型,主要应用于UI自动化测试框架的搭建,主流设计模式之

页面对象模型:结合面向对象编程思路:把项目的每个页面当做一个对象进行编程 python基础:什么对象? python中对象= 属性+行为 通过类定义=具有相同属性+相同行为对象集合

二、POM一版分为四层

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

 

三、代码实现

 非PO模型   (夜神中QQ登录) 
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver

import time

caps = {}
caps["platformName"] = "Android"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.vphone.launcher"
caps["appActivity"] = "com.vphone.launcher.launcher3.Launcher"

driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)

time.sleep(5)

el1 = driver.find_element_by_accessibility_id("QQ")
el1.click()
el2 = driver.find_element_by_id("com.tencent.mobileqq:id/btn_login")
el2.click()
el3 = driver.find_element_by_accessibility_id("请输入QQ号码或手机或邮箱")
el3.send_keys("2648636025")
el4 = driver.find_element_by_accessibility_id("密码 安全")
el4.send_keys("liu0819..")
el5 = driver.find_element_by_accessibility_id("登 录")
el5.click()

driver.quit()

四、PO模型操作

1、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=600,
                          start_y=1000,
                          end_x=600,
                          end_y=1000,
                          duration=5000)

 

五、业务页代码

 2、daohang_page.py(导航模块)
from qqlogin.background.gonggong import basePages

from appium.webdriver.common.mobileby import 
    MobileBy  # 导航页面= base层属性赫行为+当前界面的特有的属性行为 


class DaoHangPage(basePages):

    def __init__(self, driver):
        basePages.__init__(self, driver)

    def click_login(self):
        self.click(MobileBy.ID, "com.tencent.mobileqq:id/btn_login")
3、login_page.py(登录模块)  
from qqlogin.background.gonggong import basePages

from appium.webdriver.common.mobileby 
import MobileBy

# 导航页面= base层属性赫行为+当前界面的特有的属性行为 
class DaoHangPage(basePages):
    def __init__(self, driver):
        basePages.__init__(self, driver)

    def click_login(self):
        self.click(MobileBy.ID, "com.tencent.mobileqq:id/btn_login")

 

六、单元测试模块

from qqlogin.background import DaoHangPage
from qqlogin.background import LoginPage
from appium import webdriver
import pytest
import time
class TestClass(): 
    @classmethod
def setup_class(cls) -> None: caps = {}
caps["platformName"] = "Android"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.tencent.mobileqq"
caps["appActivity"] = "com.tencent.mobileqq.activity.LoginActivity"
cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
cls.driver.implicitly_wait(30)

def test_01_daohang(self): 
    daohang = DaoHangPage(self.driver)
    daohang.click_login()
def test_02(self): 
    login = LoginPage(self.driver)
    login.clear_name()
    login.input_name("766603163")
    login.clear_pass()
    login.input_pass("lly19891024lly")
    login.click_dl_button() @ classmethod

def teardown_class(cls) -> None: 
    time.sleep(20)

cls.driver.quit()
if __name__ == '__main__': 
    pytest.main()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/355644.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号