目录
1.安装依赖以及项目的基本目录
# 安装依赖
pip install pytest
pip install appium-python-client
pip install openpyxl # excel文件处理
pip install pytest-html # 测试报告
2.pom解析
pom 设计的核心思想 就是将不同的页面单独进行维护,在做自动化的过程中,如果前端页面进行更改,原来写自动化代码就可能不再适用,因为前端页面更改了之后,元素定位已经不再适合,自动化用例执行失败。就要重新更改代码,比较麻烦。
pom 将页面与测试用例单独封装,页面上的每个操作都单独封装起来,测试用例只需要调用封装好的方法即可。如果页面有改动。只需要改页面中封装的操作即可。
下面以登录场景为例,编写自动化:
conftest.py
from appium import webdriver
import pytest
import os
chromedriver= os.path.join(os.path.dirname(os.path.abspath(__file__)),'drivers/chrome/75.0.3770.140/chromedriver.exe')
@pytest.fixture(scope='session')
def driver():
desired_caps = {
'platformName': 'Android', # 测试Android系统
'platformVersion': '7.1.2', # Android版本 可以在手机的设置中关于手机查看
'deviceName': '127.0.0.1:62001', # adb devices 命令查看 设置为自己的设备
'automationName': 'UiAutomator2', # 自动化引擎
'noReset': False, # 不要重置app的状态
'fullReset': False, # 不要清理app的缓存数据
'chromedriverExecutable': chromedriver, # chromedriver 对应的绝对路径
'appPackage': "org.cnodejs.android.md", # 应用的包名
'appActivity': ".ui.activity.LaunchActivity" # 应用的活动页名称(appium会启动app的加载页)
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_capabilities=desired_caps)
driver.implicitly_wait(5) # 全局的隐式等待时间
yield driver # 将driver 传递出来
driver.quit()
pom/basePage.py
"""
所有的页面都继承这个类,获得driver
"""
import time
from appium.webdriver.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException
class BasePage:
def __init__(self,driver:WebDriver):
self.driver = driver
# 获取toast的文本值
@property
def result_text(self):
try:
toast = self.driver.find_element_by_xpath('//android.widget.Toast')
return toast.text
except NoSuchElementException:
return "找不到这个元素,请检查自己的自动化代码"
pom/loginPage.py
"""
登陆页面
"""
import time
from appium.webdriver.webdriver import WebDriver
from pom.basePage import BasePage
class LoginPage(BasePage):
# 初始化类的时候,打开登陆页面
def __init__(self,driver:WebDriver):
super(LoginPage,self).__init__(driver)
# 判断是否是登陆页面
current_activity = self.driver.current_activity
if ".ui.activity.LoginActivity" in current_activity:
pass
else:
# 不是登陆页面,则调用方法,打开登陆页面
self.__go_login