日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

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

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

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

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

2.POM一般分為四層

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

3.代碼實(shí)現(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)# 點(diǎn)擊def click(self, *loc):self.locator(*loc).click()# 滑動(dòng)(上下左右滑動(dòng))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號(hào)碼或手機(jī)或郵箱")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ù)層次清晰,可以跨平臺(tái),支持多種語言使用(可以適用于別的app)

優(yōu)化代碼:提取basepage中的配置客戶端數(shù)據(jù)(將配置的數(shù)據(jù)放在yaml中)!創(chuàng)建一個(gè)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__)當(dāng)前文件的上一級(jí)目錄 # 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ū)動(dòng)

在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)容還不錯(cuò),歡迎將生活随笔推薦給好友。