日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發(fā)布時間:2025/3/20 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python+Appium+POM实现APP端自动化测试 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.POM及POM設(shè)計原理

POM(page object model)頁面對象模型,主要應(yīng)用于UI自動化測試框架的搭建,主流設(shè)計模式之 一,頁面對象模型:結(jié)合面向?qū)ο缶幊趟悸?#xff1a;把項目的每個頁面當做一個對象進行編程

2.POM一般分為四層

第一層:basepage層:描述每個頁面相同的屬性及行為
第二層:pageobject層(每個的獨有特征及獨有的行為)
第三層:testcase層(用例層,描述項目業(yè)務(wù)流程)
第四層:testdata(數(shù)據(jù)層)

3.代碼實現(xiàn)

1.組織代碼

2.basepage(封裝公共的屬性和行為)

from selenium.webdriver.support.wait import WebDriverWaitclass 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(導(dǎo)航模塊和登錄模塊)

1.導(dǎo)航模塊

from POM.basepage.base_page import BasePages from appium.webdriver.common.mobileby import MobileByclass 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 MobileByclass 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(執(zhí)行測試用例)

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,osclass TestClass:@classmethoddef 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()@classmethoddef teardown_class(cls) -> None:cls.driver.quit()if __name__ == '__main__':pytest.main(["test_case.py"])

4.引入yaml文件(代碼優(yōu)化)

yaml文件:數(shù)據(jù)層次清晰,可以跨平臺,支持多種語言使用(可以適用于別的app)

優(yōu)化代碼:提取basepage中的配置客戶端數(shù)據(jù)(將配置的數(shù)據(jù)放在yaml中)!創(chuàng)建一個yaml.yaml文件

caps:platformName: AndroiddeviceName: 127.0.0.1:62001appPackage: com.tencent.mobileqqappActivity: com.tencent.mobileqq.activity.LoginActivity

讀取yaml文件,需要導(dǎo)入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')

優(yōu)化單元測試模塊代碼

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,osclass TestClass:@classmethoddef 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()@classmethoddef teardown_class(cls) -> None:cls.driver.quit()if __name__ == '__main__':pytest.main(["test_case.py"])

5.添加數(shù)據(jù)驅(qū)動

在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,osclass TestClass:@classmethoddef 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()@classmethoddef teardown_class(cls) -> None:cls.driver.quit()if __name__ == '__main__':pytest.main(["test_case.py"])

總結(jié)

以上是生活随笔為你收集整理的Python+Appium+POM实现APP端自动化测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。