POM一版分为四层
第一层:basepage层:描述每个页面相同的属性及行为
第二层:pageobject层(每个的独有特征及独有的行为)
第三层:testcase层(用例层,描述项目业务流程)
第四层:testdata(数据层)
非po模型
# 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
caps = {}
caps["platformName"] = "Android"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.android.settings"
caps["appActivity"] = "com.android.settings.Settings"
caps["platformVersion"] = "5.1.1"
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
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("3312507327")
el3 = driver.find_element_by_accessibility_id("密码 安全")
el3.send_keys("lwj1314520")
el4 = driver.find_element_by_accessibility_id("登 录")
el4.click()
driver.quit()
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)
daohang_page.py(导航模块)
from basepage.basepages import BasePages
from appium.webdriver.common.mobileby import MobileBy
class DaHangpage(BasePages):
def __init__(self,driver):
BasePages.__init__(self,driver)
def click_login(self):
self.click(MobileBy.ID,"com.tencent.mobileqq:id/btn_login")
输入账号密码模块
from basepage.basepages import BasePages
from appium.webdriver.common.mobileby import MobileBy
class LoginPage(BasePages):
def send_zh(self,test):
self.input(test,MobileBy.ACCESSIBILITY_ID,"请输入QQ号码或手机或邮箱")
def send_mm(self,test):
self.input(test,MobileBy.ACCESSIBILITY_ID,"密码 安全")
def click_login_button(self):
self.click(MobileBy.ACCESSIBILITY_ID,"登 录")
单元测试模块
import unittest
from appium import webdriver
from pageobject.page import DaHangpage
from pageobject.pagedl 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"
caps["platformVersion"] = "5.1.1"
cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
cls.driver.implicitly_wait(20)
def test_01(self):
dh = DaHangpage(self.driver)
dh.click_login()
def test_02(self):
lg = LoginPage(self.driver)
lg.send_zh("3312507327")
lg.send_mm("lwj1314520")
lg.click_login_button()
@classmethod
def tearDownClass(cls) -> None:
cls.driver.quit()
if __name__ == '__main__':
unittest.main()
引入yuml文件
caps:
platformName: Android
deviceName: 127.0.0.1:62001
appPackage: com.tencent.mobileqq
appActivity: com.tencent.mobileqq.activity.LoginActivity
调用yaml文件,需要导入pip install pyYAML
def yeml_method(path):
with open(path, "r", encoding="utf-8") as file:
data = yaml.load(stream=file, Loader=yaml.FullLoader)
return data
# print(os.path.dirname(__file__))
# print(os.path.dirname(os.path.dirname(__file__)))
# print(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
# print(os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))),"testdata/yeml_read"))
print(yeml_method(os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))),"testdata/yemlread")))
数据驱动
在pytest中使用@pytest.mark.parametrize()修饰器