ppium+python+pytest自动化APP po模式脚本分离终(终极版):
1、跟前置代码一样base包,base_astion.py文件,前置代码:base_driver.py文件:
base_astion.py配置文件如下:
#导入WebDriverWait显示等待模块
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
class BaseAction:
def __init__(self,driver):
self.driver = driver
#点击操作
def click(self,loc):
self.find_element(loc).click()
#输入操作
def input_search_texts(self,loc,text):
self.find_element(loc).send_keys(text)
#定义个BY.ID ,属性元素 的方法,单元素定位
#如: driver.find_element_by_xpath()
def find_element(self, loc):
by = loc[0]
value = loc[1]
if by==By.XPATH:
#XPAYH处理方法
value = self.make_xpath_with_feature(value)
#下面这个是加显示等待
return WebDriverWait(self.driver,5,1).until(lambda x:x.find_element(by,value))
#多元素定位如:driver.find_elements_by_xpath()
def find_elements(self, loc):
by = loc[0]
value = loc[1]
if by == By.XPATH:
value = self.make_xpath_with_feature(value)
# 下面这个是加显示等待
return WebDriverWait(self.driver, 5, 1).until(lambda x: x.find_elements(by, value))
#以下是PXATH工具类简化
def make_xpath_with_unit_feature(self,loc):
'''
拼接xpath中间部分
:param loc:
:return:
'''
key_index = 0 # 下标0
value_index = 1 # 下标1
option_index = 2 # 下标2
args = loc.split(",")
featre = ""
if len(args) == 2:
featre = "@" + args[key_index] + "='" + args[value_index] + "'" + "and"
elif len(args) == 3:
if args[option_index] == "0":
featre = "@" + args[key_index] + "='" + args[value_index] + "'" + "and"
elif args[option_index] == "1":
featre = "contains(@" + args[key_index] + ",'" + args[value_index] + "')" + "and"
return featre
# xpath调用简单用法
def make_xpath_with_feature(self,loc):
feature_start = "//*["
feature_end = "]"
featre = ""
# 判断是不是字符串
if isinstance(loc, str):
# 如果是正常的xpath
if loc.startswith("//"):
return loc
featre = self.make_xpath_with_unit_feature(loc) # loc字符串拼接
else:
# loc列表拼接
for i in loc:
featre += self.make_xpath_with_unit_feature(i)
# 删掉尾巴and
featre = featre.rstrip("and")
loc = feature_start + featre + feature_end
return loc
'''
使用方法与描述
//*[contains(@text,'设置')] #包含查询
//*[@text='设置'] #精确查找
//*[comtains(@text,'设置')and@index='30'and@自动='d9999']#列表and且
以后输入以下就可以了
"text,设置"
"text,设置,0"
"text,设置,1"
["text,设置","text,设置,1","text,设置,0"]
'''
2前置代码:base_driver.py文件如下:
from appium import webdriver
def init_driver():
desired_caps = {}
# 设备信息
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '127.0.0.1:62001'
# app的信息启动名和包名
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'
# 中文输入允许
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
# 声明我们的driver对象
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
return driver
3在page包里面,display_page.py文件脚本操作文件(专门点击操作等)
from selenium.webdriver.common.by import By #导入By模块
import os,sys
sys.path.append(os.getcwd())
from ..base.base_action import BaseAction
class DisplayPage(BaseAction):
#点击显示按钮
display_button = By.XPATH,"//*[@text='显示']"
#点击放大镜
search_button = By.ID,"com.android.settings:id/search"
#输入文字搜索框
input_text_view = By.ID,"android:id/search_src_text"
#点击返回按钮
back_button = By.CLASS_NAME,"android.widget.ImageButton"
def __init__(self,driver):
# self.driver = driver
BaseAction.__init__(self,driver)
def click_display(self):
# 点击显示
#调用find_elemnt()方法传入变量display_button
# self.find_element(self.display_button).click()
self.click(self.display_button)
def click_search(self):
# 点击放大镜
# self.find_element(self.search_button).click()
self.click(self.search_button)
def input_text(self,text):
# #输入文字
# self.find_element(self.input_text_view).send_keys(text)
self.input_search_texts(self.input_text_view,text)
# #点击返回
def click_back(self):
# self.find_element(self.back_button).click()
self.click(self.back_button)
4在scripts包里面有test_display.py文件:(专门控制流程的)
#pytest,用自己模块必须导入OS,sy模块
import os,sys
sys.path.append(os.getcwd())
from appium import webdriver
from ..base.base_driver import init_driver
from ..page.display_page import DisplayPage
#进入设置操作
class TestDisplay:
def setup(self):
self.driver = init_driver()
self.display_page = DisplayPage(self.driver)
#定义流程执行函数
def test_modile_display_input(self):
#点击显示
self.display_page.click_display()
#点击放大镜
self.display_page.click_search()
# #输入文字
self.display_page.input_text("hello")
# #点击返回
self.display_page.click_back()
5、PO模式总体如下:
PO
dase
page
scripts
pytest.ini配置
report报告