Python+Appium+POM实现APP端自动化测试

x33g5p2x  于2021-10-31 转载在 Python  
字(4.8k)|赞(0)|评价(0)|浏览(191)

1.POM及POM设计原理

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

2.POM一般分为四层

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

3.代码实现

1.组织代码

2.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)

3.pageobject(导航模块和登录模块)

1.导航模块

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

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

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

2.登录模块

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

class LoginPage(BasePages):
    
    def send_user(self,text):
        self.input(text,MobileBy.ACCESSIBILITY_ID,"请输入QQ号码或手机或邮箱")
    def send_password(self,text):
        self.input(text,MobileBy.ACCESSIBILITY_ID,"密码 安全")
    def click_login_qq(self):
        self.click(MobileBy.ACCESSIBILITY_ID,"登 录")

4.testcase(执行测试用例)

from POM.pageobject.daohang_page import DaohangPage
from POM.pageobject.login_page import LoginPage
from POM.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(30)
    def test_01(self):
        daohang=DaohangPage(self.driver)
        daohang.click_login()
    def test_02(self,user,password):
        login=LoginPage(self.driver)
        login.send_user("xxx")
        login.send_password("xxx")
        login.click_login_qq()
    @classmethod
    def teardown_class(cls) -> None:
        cls.driver.quit()

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

4.引入yaml文件(代码优化)

yaml文件:数据层次清晰,可以跨平台,支持多种语言使用(可以适用于别的app)

优化代码:提取basepage中的配置客户端数据(将配置的数据放在yaml中)!创建一个yaml.yaml文件

caps:
  platformName: Android
  deviceName: 127.0.0.1:62001
  appPackage: com.tencent.mobileqq
  appActivity: com.tencent.mobileqq.activity.LoginActivity

读取yaml文件,需要导入pip install pyYAML

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')

优化单元测试模块代码

from POM.pageobject.daohang_page import DaohangPage
from POM.pageobject.login_page import LoginPage
from POM.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_login()
    def test_02(self,user,password):
        login=LoginPage(self.driver)
        login.send_user("xxx")
        login.send_password("xxx")
        login.click_login_qq()
    @classmethod
    def teardown_class(cls) -> None:
        cls.driver.quit()

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

5.添加数据驱动

在pytest中使用@pytest.mark.parametrize()修饰器

from POM.pageobject.daohang_page import DaohangPage
from POM.pageobject.login_page import LoginPage
from POM.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_login()

    @pytest.mark.parametrize("user,password", [("xxx", "zzz")])
    def test_02(self,user,password):
        login=LoginPage(self.driver)
        login.send_user(user)
        login.send_password(password)
        login.click_login_qq()
    @classmethod
    def teardown_class(cls) -> None:
        cls.driver.quit()

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

相关文章