ppium+python+pytest自动化APP po模式脚本分离终(终极版):
1跟前置代码一样base包,base_astion.py文件,前置代码:base_driver.py文件:
base_astion.py文件如下:
#导入WebDriverWait显示等待模块
from selenium.webdriver.support.wait import WebDriverWait
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 ,属性元素 的方法
def find_element(self, loc):
by = loc[0]
value = loc[1]
# return self.driver.find_element(by,value)#这个是没有显示等待
#下面这个是加显示等待
return WebDriverWait(self.driver,5,1).until(lambda x:x.find_element(by,value))
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报告