POM(page object model)頁面物件模型,主要應用于UI自動化測驗框架的搭建,
主流設計模式之一,頁面物件模型:結合面向物件編程思路:把專案的每個頁面當做一個物件進行編程
第一層:basepage層:描述每個頁面相同的屬性及行為
第二層:pageobject層(每個的獨有特征及獨有的行為)
第三層:testcase層(用例層,描述專案業務流程)
第四層:testdata(資料層)
from appium import webdriver
caps = {}
caps["platformName"] = "Android"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.tencent.mobileqq"
caps["appActivity"] = "com.tencent.mobileqq.activity.LoginActivity"
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
driver.implicitly_wait(30)
el1 = driver.find_element_by_id("com.tencent.mobileqq:id/btn_login")
el1.click()
el2 = driver.find_element_by_accessibility_id("請輸入QQ號碼或手機或郵箱")
el2.send_keys("QQ賬號")#輸入自己的QQ賬號
el3 = driver.find_element_by_accessibility_id("密碼 安全")
el3.send_keys("QQ密碼")#輸入QQ密碼
el4 = driver.find_element_by_accessibility_id("登 錄")
el4.click()
el5 = driver.find_element_by_accessibility_id("同意")
el5.click()
el6 = driver.find_element_by_accessibility_id("登 錄")
el6.click()

po模型操作
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=x*start_x,
start_y=y*start_y,
end_x=x*end_x,
end_y=y*end_y,
duration=duration)
業務頁代碼
page_01.py(導航模塊)
from lzwifi.base.basepage import BasePages # 匯入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.ID, "com.tencent.mobileqq:id/btn_login")

page_02.py(登錄模塊)
from lzwifi.base.basepage import BasePages
from appium.webdriver.common.mobileby import MobileBy
class LoginPage(BasePages):
def __init__(self, driver):
BasePages.__init__(self, driver)
# 清空賬號框操作
def clear_zh(self):
self.clear(MobileBy.ACCESSIBILITY_ID, "請輸入QQ號碼或手機或郵箱")
# 輸入QQ賬號操作
def input_zh(self, test):
self.input(test, MobileBy.ACCESSIBILITY_ID, "請輸入QQ號碼或手機或郵箱")
# 清空密碼框操作
def clear_mm(self):
self.clear(MobileBy.ACCESSIBILITY_ID, "密碼 安全")
# 輸入QQ密碼操作
def input_mm(self, test):
self.input(test, MobileBy.ACCESSIBILITY_ID, "密碼 安全")
# 點擊登錄操作
def click_dl(self):
self.click(MobileBy.ACCESSIBILITY_ID, "登 錄")
# 同意條款
def click_ty(self):
self.click(MobileBy.ACCESSIBILITY_ID, "同意")
# 再次登錄
def click_rdl(self):
self.click(MobileBy.ACCESSIBILITY_ID, "登 錄")

單元測驗模塊
unittest實作
import unittest
import time
from appium import webdriver
from lzwifi.pages.page_01 import DaoHangPage
from lzwifi.pages.page_02 import LoginPage
class TestClass(unittest.TestCase):
@classmethod
def setUpClass(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(self):
dh = DaoHangPage(self.driver)
dh.click_login()
def test_02(self):
dl = LoginPage(self.driver)
dl.clear_zh()
dl.input_zh("QQ賬號") # 輸入自己的QQ賬號
dl.clear_mm()
dl.input_mm("QQ密碼") # 輸入自己的QQ密碼
dl.click_dl()
dl.click_ty()
dl.click_rdl()
@classmethod
def tearDownClass(cls) -> None:
time.sleep(20)
cls.driver.quit()
if __name__ == '__main__':
unittest.main()

pytest 實作 (需要匯入 pip install pytest)
import pytest
import time
from appium import webdriver
from lzwifi.pages.page_01 import DaoHangPage
from lzwifi.pages.page_02 import LoginPage
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(self):
dh = DaoHangPage(self.driver)
dh.click_login()
def test_02(self):
dl = LoginPage(self.driver)
dl.clear_zh()
dl.input_zh("QQ賬號")
dl.clear_mm()
dl.input_mm("QQ密碼")
dl.click_dl()
dl.click_ty()
dl.click_rdl()
@classmethod
def teardown_class(cls) -> None:
time.sleep(20)
cls.driver.quit()
if __name__ == '__main__':
pytest.main()

引入yaml檔案
yaml檔案:資料層次清晰,可以跨平臺,支持多種語言使用(可以適用于別的app)
優化代碼:提取basepage中的配置客戶端資料(將配置的資料放在yaml中)
創建config.yaml
caps:
platformName: Android
deviceName: 127.0.0.1:62001
appPackage: com.tencent.mobileqq
appActivity: com.tencent.mobileqq.activity.LoginActivity
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
if __name__ == '__main__':
# os.path.dirname(__file__)當前檔案的上一級目錄
# os.path.abspath(path)找到路徑的絕對路徑
rootpath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
dd = os.path.join(rootpath, "datademo\config.yaml")
print(dd)
print(readYaml(dd))

修改單元測驗模塊代碼
import pytest
import time,os
from lzwifi.common.read_yaml import readYaml
from appium import webdriver
from lzwifi.pages.page_01 import DaoHangPage
from lzwifi.pages.page_02 import LoginPage
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"
rootpath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
dd = os.path.join(rootpath, "datademo\config.yaml")
data = readYaml(dd)
cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", data["caps"])
cls.driver.implicitly_wait(30)
def test_01(self):
dh = DaoHangPage(self.driver)
dh.click_login()
def test_02(self):
dl = LoginPage(self.driver)
dl.clear_zh()
dl.input_zh("QQ賬號")
dl.clear_mm()
dl.input_mm("QQ密碼")
dl.click_dl()
dl.click_ty()
dl.click_rdl()
@classmethod
def teardown_class(cls) -> None:
time.sleep(20)
cls.driver.quit()
if __name__ == '__main__':
pytest.main()

資料驅動
@pytest.mark.parametrize("user,password", [("QQ賬號", "QQ密碼")])
def test_02(self, user, password):
dl = LoginPage(self.driver)
dl.clear_zh()
dl.input_zh(user)
dl.clear_mm()
dl.input_mm(password)
dl.click_dl()
dl.click_ty()
dl.click_rdl()

生成測驗報告
unittest實作(首先需要切換到unittest單元測驗模塊)
匯入HTMLTestRunner.py
testhtml.py
import unittest
from lzwifi.datademo.HTMLTestRunner import HTMLTestRunner # 匯入HTMLTestRunner
from lzwifi.test.test_qq import TestClass # 匯入單元測驗類
class HtmlClass():
def htmlMethod(self):
suite = unittest.TestSuite()
lists = ['test_01', 'test_02'] # 測驗用例
for i in lists:
suite.addTest(TestClass(i))
# 測驗報告生成路徑
with open("../unittesthtml.html", "wb") as f:
HTMLTestRunner(
stream=f,
title="測驗資料",
description="測驗一期",
verbosity=2
).run(suite)
h = HtmlClass()
h.htmlMethod()

pytest實作(首先需要切換到pytest單元測驗模塊)
需要匯入pip install pytest-html、pip install allure-pytest
修改單元測驗模塊代碼
import pytest
import time,os,allure
from lzwifi.common.read_yaml import readYaml
from appium import webdriver
from lzwifi.pages.page_01 import DaoHangPage
from lzwifi.pages.page_02 import LoginPage
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"
rootpath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
dd = os.path.join(rootpath, "datademo\config.yaml")
data = readYaml(dd)
cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", data["caps"])
cls.driver.implicitly_wait(30)
def test_01(self):
dh = DaoHangPage(self.driver)
dh.click_login()
@pytest.mark.parametrize("user,password", [("QQ賬號", "QQ密碼")])
def test_02(self, user, password):
dl = LoginPage(self.driver)
dl.clear_zh()
dl.input_zh(user)
dl.clear_mm()
dl.input_mm(password)
dl.click_dl()
dl.click_ty()
dl.click_rdl()
@classmethod
def teardown_class(cls) -> None:
time.sleep(20)
cls.driver.quit()
if __name__ == '__main__':
pytest.main(['--alluredir', 'report/result', 'test_qq.py'])
split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
os.system(split)
生成html測驗報告

最終框架展示
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/342243.html
標籤:其他
上一篇:查看Android 原始碼時,無法找到部分類如contextImpl
下一篇:Android studio 修改了 android:background=“@drawable/welcome“后無法啟動
