Appium独立测试自动化初始化脚本

server/2024/9/24 13:01:28/

1、查看环境初始化参数

确保appium已经开起来了,设置ip ,并点击启动

打开夜神模拟器,点击工具--设置

最下面的版本说明,双击进去

版本号这里再去单击。

直到进入到开发者模式。

可能我们不是开发者模式打开的状态,所以软件访问模拟器时,它有可能不让我们连。

要重启一下模拟器

重启模拟器之后,开发者模式才能生效。

此时再用命令行查看,可以看到设备号。

caps={'platformName':'Android',               #设置platformName:手机系统名称Android'platformVersion':'7.1.2',              # #设置platformVersion:手机系统版本'deviceName':'127.0.0.1:52001' ,        #设置deviceName:设备名称'appPackage':'uni.UNI765428A',          #设置appPackage:被测程序包名'appActivity':'io.dcloud.PandoraEntry'  #设置appActivity:被测程序活动名
}

手机端参数查看命令有那些。每个人设备及端口号不一样,所以需要单独看。

adb  devices

查看包名

adb shell dumpsys activity activities |findstr mFocusedActivity

打开appium Inspector ,开始定位元素。

比如这个id,先点1混合定位模式,再点2开始定位,点3要定位的元素,最后拷贝它的id或者xpath

定位命令和web没什么不同

driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()

2、test_loginV1.py  跑通流程

先导入必备的包及写上版本说明

#*****************************************
#v1.0:app独立自动化测试 脚本--初始化登录
#*****************************************
#导入类库
import timefrom appium.webdriver.webdriver import WebDriver
from appium.webdriver.webdriver import By
#手机参数初始化
#查询程序包名的命令:adb shell dumpsys activity activities| findstr mFocusedActivity#设置appPackage:被测程序包名

定义程序包名的参数


caps={'platformName':'Android',               #设置platformName:手机系统名称Android'platformVersion':'7.1.2',              # #设置platformVersion:手机系统版本'deviceName':'127.0.0.1:52001' ,        #设置deviceName:设备名称'appPackage':'uni.UNI765428A',          #设置appPackage:被测程序包名'appActivity':'io.dcloud.PandoraEntry'  #设置appActivity:被测程序活动名
}
#启动appium
driver=WebDriver('http://127.0.0.1:4723/wd/hub',caps)

点击允许按钮

#进行元素定位
#点击允许按钮
time.sleep(3)
driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()

#允许电话管理

time.sleep(2)
driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()
#输入后台服务器地址

time.sleep(5)
xpath_service='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.widget.EditText'
servicepath='https://lefeiwisdom-3pt-2t6a7-www.vip.51env.net'
driver.find_element(By.XPATH,xpath_service).clear()
time.sleep(1)
driver.find_element(By.XPATH,xpath_service).send_keys(servicepath)
#点击确定按钮
time.sleep(3)
xpath_ok="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]"
driver.find_element(By.XPATH,xpath_ok).click()
#点击验证码登录按钮

xpath_check='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[4]'
driver.find_element(By.XPATH,xpath_check).click()
time.sleep(2)
#输入手机号码
xpath_phone='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[5]/android.view.View/android.widget.EditText'
# id_phone='c52abd0b-3c7b-4b6f-a2a0-f386d56bebd8'
driver.find_element(By.XPATH,xpath_phone).send_keys('13800138001')
time.sleep(2)
#输入验证码
xpath_checkcode='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[6]/android.view.View/android.widget.EditText'
driver.find_element(By.XPATH,xpath_checkcode).send_keys('111111')
#接受协议
xpath_allow='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[9]'
time.sleep(2)
driver.find_element(By.XPATH,xpath_allow).click()
#登录按钮

time.sleep(2)
xpath_login='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[8]'
driver.find_element(By.XPATH,xpath_login).click()

完成登录

3、test_loginV2.py 面向过程的封装

#方法0:手机驱动参数输出化设置
#方法1:两个允许按钮+服务器地址+验证码登录
#方法2:普通用户登录

传递参数driver

#*****************************************
#v2.0:app独立自动化测试 脚本--初始化登录
#优化:面向过程的封装
#方法0:手机驱动参数输出化设置
#方法1:两个允许按钮+服务器地址+验证码登录
#方法2:普通用户登录
#****************************************
#导入类库
import time
from appium.webdriver.webdriver import WebDriver
from appium.webdriver.webdriver import By
#方法0:手机驱动参数初始化
def test_cpas_init():# 手机参数初始化# ******************************************************************************************# 查询程序包名的命令:adb shell dumpsys activity activities| findstr mFocusedActivity#设置appPackage:被测程序包名caps = {'platformName': 'Android',  # 设置platformName:手机系统名称Android'platformVersion': '7.1.2',  # #设置platformVersion:手机系统版本'deviceName': '127.0.0.1:52001',  # 设置deviceName:设备名称'appPackage': 'uni.UNI765428A',  # 设置appPackage:被测程序包名'appActivity': 'io.dcloud.PandoraEntry'  # 设置appActivity:被测程序活动名}# 启动appiumdriver = WebDriver('http://127.0.0.1:4723/wd/hub', caps)# **********************************************************************************return driver#方法1:两个允许按钮+服务器地址+验证码登录
def test_login_init(driver):#进行元素定位#点击允许按钮time.sleep(2)driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()#允许电话管理time.sleep(2)driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()#*****************************************************************#输入后台服务器地址time.sleep(5)xpath_service='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.widget.EditText'servicepath='https://lefeiwisdom-3pt-2t6a7-www.vip.51env.net'driver.find_element(By.XPATH,xpath_service).clear()time.sleep(1)driver.find_element(By.XPATH,xpath_service).send_keys(servicepath)#点击确定按钮time.sleep(3)#***********************************************************************xpath_ok="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]"driver.find_element(By.XPATH,xpath_ok).click()#***********************************************************************#点击验证码登录按钮time.sleep(4)xpath_check='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[4]'driver.find_element(By.XPATH,xpath_check).click()time.sleep(2)#********************************************************************************#方法2:普通用户登录
def user_login_login(driver):#输入手机号码xpath_phone='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[5]/android.view.View/android.widget.EditText'# id_phone='c52abd0b-3c7b-4b6f-a2a0-f386d56bebd8'driver.find_element(By.XPATH,xpath_phone).send_keys('13800138001')time.sleep(2)#输入验证码xpath_checkcode='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[6]/android.view.View/android.widget.EditText'driver.find_element(By.XPATH,xpath_checkcode).send_keys('111111')#接受协议xpath_allow='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[9]'time.sleep(2)driver.find_element(By.XPATH,xpath_allow).click()#登录按钮time.sleep(2)xpath_login='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[8]'driver.find_element(By.XPATH,xpath_login).click()if __name__ == '__main__':driver=test_cpas_init()test_login_init(driver)user_login_login(driver)

4、test_loginV3.py    作者登录

#v3.0:app独立自动化测试 脚本--初始化登录
#优化:面向过程的封装
#方法0:手机驱动参数输出化设置
#方法1:两个允许按钮+服务器地址+验证码登录
#方法3:作者登录
#*****************************************
#v3.0:app独立自动化测试 脚本--初始化登录
#优化:面向过程的封装
#方法0:手机驱动参数输出化设置
#方法1:两个允许按钮+服务器地址+验证码登录
#方法3:作者登录
#****************************************
#导入类库
import time
from appium.webdriver.webdriver import WebDriver
from appium.webdriver.webdriver import By
#方法0:手机驱动参数初始化
def test_cpas_init():# 手机参数初始化# ******************************************************************************************# 查询程序包名的命令:adb shell dumpsys activity activities| findstr mFocusedActivity#设置appPackage:被测程序包名caps = {'platformName': 'Android',  # 设置platformName:手机系统名称Android'platformVersion': '7.1.2',  # #设置platformVersion:手机系统版本'deviceName': '127.0.0.1:52001',  # 设置deviceName:设备名称'appPackage': 'uni.UNI765428A',  # 设置appPackage:被测程序包名'appActivity': 'io.dcloud.PandoraEntry'  # 设置appActivity:被测程序活动名}# 启动appiumdriver = WebDriver('http://127.0.0.1:4723/wd/hub', caps)# **********************************************************************************return driver#方法1:两个允许按钮+服务器地址+验证码登录
def test_login_init(driver):#进行元素定位#点击允许按钮time.sleep(2)driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()#允许电话管理time.sleep(2)driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()#*****************************************************************#输入后台服务器地址time.sleep(5)xpath_service='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.widget.EditText'servicepath='https://lefeiwisdom-3pt-2t6a7-www.vip.51env.net'driver.find_element(By.XPATH,xpath_service).clear()time.sleep(1)driver.find_element(By.XPATH,xpath_service).send_keys(servicepath)#点击确定按钮time.sleep(3)#***********************************************************************xpath_ok="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]"driver.find_element(By.XPATH,xpath_ok).click()#***********************************************************************#点击验证码登录按钮time.sleep(4)xpath_check='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[4]'driver.find_element(By.XPATH,xpath_check).click()time.sleep(2)#********************************************************************************#方法3:作者登录
def test_author_login(driver):time.sleep(5)#切换到作者登录标签xpath_author='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[3]'driver.find_element(By.XPATH,xpath_author).click()time.sleep(2)#输入手机号码xpath_phone='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[5]/android.view.View/android.widget.EditText'# id_phone='c52abd0b-3c7b-4b6f-a2a0-f386d56bebd8'driver.find_element(By.XPATH,xpath_phone).send_keys('13900139001')time.sleep(2)#输入验证码xpath_checkcode='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[6]/android.view.View/android.widget.EditText'driver.find_element(By.XPATH,xpath_checkcode).send_keys('111111')#接受协议xpath_allow='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[9]'time.sleep(2)driver.find_element(By.XPATH,xpath_allow).click()#登录按钮time.sleep(2)xpath_login='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[8]'driver.find_element(By.XPATH,xpath_login).click()if __name__ == '__main__':driver=test_cpas_init()test_login_init(driver)test_author_login(driver)

5、test_loginV4.py #优化:面向过程的封装,可选哪种方法的登录

#*****************************************
#v4.0:app独立自动化测试 脚本--初始化登录
#优化:面向过程的封装,可选哪种方法的登录
#方法0:手机驱动参数输出化设置
#方法1:两个允许按钮+服务器地址+验证码登录
#方法2:普通用户登录/作者登录
#****************************************
#*****************************************
#v4.0:app独立自动化测试 脚本--初始化登录
#优化:面向过程的封装,可选哪种方法的登录
#方法0:手机驱动参数输出化设置
#方法1:两个允许按钮+服务器地址+验证码登录
#方法2:普通用户登录/作者登录
#****************************************
#导入类库
import time
from appium.webdriver.webdriver import WebDriver
from appium.webdriver.webdriver import By
#方法0:手机驱动参数初始化
def test_cpas_init():# 手机参数初始化# ******************************************************************************************# 查询程序包名的命令:adb shell dumpsys activity activities| findstr mFocusedActivity#设置appPackage:被测程序包名caps = {'platformName': 'Android',  # 设置platformName:手机系统名称Android'platformVersion': '7.1.2',  # #设置platformVersion:手机系统版本'deviceName': '127.0.0.1:52001',  # 设置deviceName:设备名称'appPackage': 'uni.UNI765428A',  # 设置appPackage:被测程序包名'appActivity': 'io.dcloud.PandoraEntry'  # 设置appActivity:被测程序活动名}# 启动appiumdriver = WebDriver('http://127.0.0.1:4723/wd/hub', caps)# **********************************************************************************return driver#方法1:两个允许按钮+服务器地址+验证码登录
def test_login_init(driver):#进行元素定位#点击允许按钮time.sleep(2)driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()#允许电话管理time.sleep(2)driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()#*****************************************************************#输入后台服务器地址time.sleep(5)xpath_service='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.widget.EditText'servicepath='https://lefeiwisdom-3pt-2t6a7-www.vip.51env.net'driver.find_element(By.XPATH,xpath_service).clear()time.sleep(1)driver.find_element(By.XPATH,xpath_service).send_keys(servicepath)#点击确定按钮time.sleep(3)#***********************************************************************xpath_ok="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]"driver.find_element(By.XPATH,xpath_ok).click()#***********************************************************************#点击验证码登录按钮time.sleep(4)xpath_check='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[4]'driver.find_element(By.XPATH,xpath_check).click()time.sleep(2)#********************************************************************************#方法2:作者登录
def test_author_login(driver,usertype):time.sleep(5)if usertype==1:#切换到作者登录标签xpath_author='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[3]'driver.find_element(By.XPATH,xpath_author).click()userphone='13900139001'else:userphone='13800138001'time.sleep(2)#输入手机号码xpath_phone='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[5]/android.view.View/android.widget.EditText'# id_phone='c52abd0b-3c7b-4b6f-a2a0-f386d56bebd8'driver.find_element(By.XPATH, xpath_phone).send_keys(userphone)time.sleep(2)#输入验证码xpath_checkcode='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[6]/android.view.View/android.widget.EditText'driver.find_element(By.XPATH,xpath_checkcode).send_keys('111111')#接受协议xpath_allow='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[9]'time.sleep(2)driver.find_element(By.XPATH,xpath_allow).click()#登录按钮time.sleep(2)xpath_login='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[8]'driver.find_element(By.XPATH,xpath_login).click()if __name__ == '__main__':#进行哪种方法的登录#usertype=0 表示普通用户登录,=1为作者登录usertype=int(input("请输入数字,0 表示普通用户登录,1为作者登录"))driver=test_cpas_init()test_login_init(driver)test_author_login(driver,usertype)

6、test_loginV5.py #优化:面向对象的封装,可选哪种方法的登录

#v5.0:app独立自动化测试 脚本--初始化登录
#优化:面向对象的封装,可选哪种方法的登录
#方法0:手机驱动参数输出化设置
#方法1:两个允许按钮+服务器地址+验证码登录
#方法2:普通用户登录/作者登录
#将以上方法封装到测试类中
#*****************************************
#v5.0:app独立自动化测试 脚本--初始化登录
#优化:面向对象的封装,可选哪种方法的登录
#方法0:手机驱动参数输出化设置
#方法1:两个允许按钮+服务器地址+验证码登录
#方法2:普通用户登录/作者登录
#将以上方法封装到测试类中
#****************************************
#导入类库
import time
from appium.webdriver.webdriver import WebDriver
from appium.webdriver.webdriver import By
class Test_login():#方法0:手机驱动参数初始化def test_cpas_init(self):# 手机参数初始化# ******************************************************************************************# 查询程序包名的命令:adb shell dumpsys activity activities| findstr mFocusedActivity#设置appPackage:被测程序包名caps = {'platformName': 'Android',  # 设置platformName:手机系统名称Android'platformVersion': '7.1.2',  # #设置platformVersion:手机系统版本'deviceName': '127.0.0.1:52001',  # 设置deviceName:设备名称'appPackage': 'uni.UNI765428A',  # 设置appPackage:被测程序包名'appActivity': 'io.dcloud.PandoraEntry'  # 设置appActivity:被测程序活动名}# 启动appiumdriver = WebDriver('http://127.0.0.1:4723/wd/hub', caps)# **********************************************************************************return driver#方法1:两个允许按钮+服务器地址+验证码登录def test_login_init(self,driver):#进行元素定位#点击允许按钮time.sleep(2)driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()#允许电话管理time.sleep(2)driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()#*****************************************************************#输入后台服务器地址time.sleep(5)xpath_service='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.widget.EditText'servicepath='https://lefeiwisdom-3pt-2t6a7-www.vip.51env.net'driver.find_element(By.XPATH,xpath_service).clear()time.sleep(1)driver.find_element(By.XPATH,xpath_service).send_keys(servicepath)#点击确定按钮time.sleep(3)#***********************************************************************xpath_ok="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]"driver.find_element(By.XPATH,xpath_ok).click()#***********************************************************************#点击验证码登录按钮time.sleep(4)xpath_check='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[4]'driver.find_element(By.XPATH,xpath_check).click()time.sleep(2)#********************************************************************************#方法2:普通用户登录/作者登录def test_author_login(self,driver,usertype):time.sleep(5)if usertype==1:#切换到作者登录标签xpath_author='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[3]'driver.find_element(By.XPATH,xpath_author).click()userphone='13900139001'else:userphone='13800138001'time.sleep(2)#输入手机号码xpath_phone='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[5]/android.view.View/android.widget.EditText'# id_phone='c52abd0b-3c7b-4b6f-a2a0-f386d56bebd8'driver.find_element(By.XPATH, xpath_phone).send_keys(userphone)time.sleep(2)#输入验证码xpath_checkcode='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[6]/android.view.View/android.widget.EditText'driver.find_element(By.XPATH,xpath_checkcode).send_keys('111111')#接受协议xpath_allow='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[9]'time.sleep(2)driver.find_element(By.XPATH,xpath_allow).click()#登录按钮time.sleep(2)xpath_login='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[8]'driver.find_element(By.XPATH,xpath_login).click()if __name__ == '__main__':#进行哪种方法的登录#usertype=0 表示普通用户登录,=1为作者登录usertype=int(input("请输入数字,0 表示普通用户登录,1为作者登录"))#实例化测试类对象obj=Test_login()driver=obj.test_cpas_init()obj.test_login_init(driver)obj.test_author_login(driver,usertype)

7、test_loginV6.py  将以上方法封装到测试类中,将参数变为属性

#*****************************************
#v6.0:app独立自动化测试 脚本--初始化登录
#优化:面向对象的封装,可选哪种方法的登录
#方法0:手机驱动参数输出化设置
#方法1:两个允许按钮+服务器地址+验证码登录
#方法2:普通用户登录/作者登录
#将以上方法封装到测试类中,将参数变为属性
#****************************************
#*****************************************
#v6.0:app独立自动化测试 脚本--初始化登录
#优化:面向对象的封装,可选哪种方法的登录
#方法0:手机驱动参数输出化设置
#方法1:两个允许按钮+服务器地址+验证码登录
#方法2:普通用户登录/作者登录
#将以上方法封装到测试类中,将参数变为属性
#****************************************
#导入类库
import time
from appium.webdriver.webdriver import WebDriver
from appium.webdriver.webdriver import By
class Test_login():#方法0:手机驱动参数初始化def test_cpas_init(self):# 手机参数初始化# ******************************************************************************************# 查询程序包名的命令:adb shell dumpsys activity activities| findstr mFocusedActivity#设置appPackage:被测程序包名caps = {'platformName': 'Android',  # 设置platformName:手机系统名称Android'platformVersion': '7.1.2',  # #设置platformVersion:手机系统版本'deviceName': '127.0.0.1:52001',  # 设置deviceName:设备名称'appPackage': 'uni.UNI765428A',  # 设置appPackage:被测程序包名'appActivity': 'io.dcloud.PandoraEntry'  # 设置appActivity:被测程序活动名}# 启动appiumself.driver = WebDriver('http://127.0.0.1:4723/wd/hub', caps)# **********************************************************************************#方法1:两个允许按钮+服务器地址+验证码登录def test_login_init(self):#进行元素定位#点击允许按钮time.sleep(2)self.driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()#允许电话管理time.sleep(2)self.driver.find_element(By.ID,'com.android.packageinstaller:id/permission_allow_button').click()#*****************************************************************#输入后台服务器地址time.sleep(5)xpath_service='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.widget.EditText'servicepath='https://lefeiwisdom-3pt-2t6a7-www.vip.51env.net'self.driver.find_element(By.XPATH,xpath_service).clear()time.sleep(1)self.driver.find_element(By.XPATH,xpath_service).send_keys(servicepath)#点击确定按钮time.sleep(3)#***********************************************************************xpath_ok="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]"self.driver.find_element(By.XPATH,xpath_ok).click()#***********************************************************************#点击验证码登录按钮time.sleep(4)xpath_check='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[4]'self.driver.find_element(By.XPATH,xpath_check).click()time.sleep(2)#********************************************************************************#方法2:普通用户登录/作者登录def test_author_login(self,usertype):time.sleep(5)if usertype==1:#切换到作者登录标签xpath_author='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[3]'self.driver.find_element(By.XPATH,xpath_author).click()userphone='13900139001'else:userphone='13800138001'time.sleep(2)#输入手机号码xpath_phone='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[5]/android.view.View/android.widget.EditText'# id_phone='c52abd0b-3c7b-4b6f-a2a0-f386d56bebd8'self.driver.find_element(By.XPATH, xpath_phone).send_keys(userphone)time.sleep(2)#输入验证码xpath_checkcode='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[6]/android.view.View/android.widget.EditText'self.driver.find_element(By.XPATH,xpath_checkcode).send_keys('111111')#接受协议xpath_allow='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[9]'time.sleep(2)self.driver.find_element(By.XPATH,xpath_allow).click()#登录按钮time.sleep(2)xpath_login='/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[8]'self.driver.find_element(By.XPATH,xpath_login).click()if __name__ == '__main__':#进行哪种方法的登录#usertype=0 表示普通用户登录,=1为作者登录usertype=int(input("请输入数字,0 表示普通用户登录,1为作者登录"))#实例化测试类对象obj=Test_login()obj.test_cpas_init()obj.test_login_init()obj.test_author_login(usertype)


http://www.ppmy.cn/server/121358.html

相关文章

验收测试:从需求到交付的全程把控!

在软件开发过程中,验收测试是一个至关重要的环节。它不仅是对软件质量的把关,也是对整个项目周期的全程把控。从需求分析到最终的软件交付,验收测试都需要严格进行,以确保软件能够符合预期的质量和性能要求。 一、需求分析阶段 在…

express的Router,配置 post 请求方法

在Express中,使用Router对象配置POST请求方法与在主应用上配置POST请求方法非常相似。你首先需要从express模块中引入Router,然后创建一个Router实例。接下来,你可以在这个Router实例上使用.post()方法来定义POST请求的路由处理器。 下面是一…

对c语言中的指针进行深入全面的解析

1.普通的指针: 实际上指针就是存放地址的变量,eg: int a10; int *p&a; 拆分一下int *中的*说明p是一个指针,int是它所指向的类型; 2.字符串指针和字符串数组 char*str1"abcd"; 先看这一个,这个就是一个字符串…

数模方法论-蒙特卡洛法

一、基本概念 蒙特卡洛法是一种基于随机抽样的数值计算方法,主要用于估计复杂系统的数值解。其基本原理是通过生成大量随机样本,来模拟系统的行为或求解特定的数学问题,比如积分、概率和优化等。 在应用上,蒙特卡洛法可以用于金融…

Mac 上哪个剪切板增强工具比较好用? 好用剪切板工具推荐

在日常文字编辑中,我们经常需要重复使用复制的内容。然而,新内容一旦复制,旧内容就会被覆盖。因此,选择一款易用高效的剪贴板工具成为了许多人的需求。本文整理了一些适用于 macOS 系统的优秀剪贴板增强工具,欢迎大家下…

【ShuQiHere】 探索数据挖掘的世界:从概念到应用

🌐 【ShuQiHere】 数据挖掘(Data Mining, DM) 是一种从大型数据集中提取有用信息的技术,无论是在商业分析、金融预测,还是医学研究中,数据挖掘都扮演着至关重要的角色。本文将带您深入了解数据挖掘的核心概…

可编辑PPT | 能源企业数字化框架、数字化运营及数字化平台建设方案

项目背景及需求理解 首先提出了全球能源互联网的概念,强调了清洁能源和电能替代的重要性,并介绍了德国工业4.0战略以及泛在电力物联网的创新。文档探讨了信息化与工业化的深度融合,以及云计算、大数据、物联网和移动应用等新技术在能源行业的…

Linux——创建编写并编译一个C程序

一、使用vim编辑器 在Linux系统下,使用vim编辑器创建、编写并编译一个C程序是一个常见的做法。以下是一个详细的步骤指南,我们将创建一个简单的C程序,该程序的功能是输出“Hello, World!”到终端。 步骤 1: 打开vim编辑器并创建C程序文件 …