二、POM一版分为四层POM(page object model)页面对象模型,主要应用于UI自动化测试框架的搭建,主流设计模式之
页面对象模型:结合面向对象编程思路:把项目的每个页面当做一个对象进行编程 python基础:什么对象? python中对象= 属性+行为 通过类定义=具有相同属性+相同行为对象集合
第一层: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()



