appium每次运行不初始化
阅读原文时间:2023年07月10日阅读:3

最近测试公司软件遇到一个问题,每次运行Appium测试代码,打开软件都是初始化,每次的新手教程以及无法记住密码一堆问题,导致效率很低,而且定位有时候出问题。

先看初始化的代码

1 from appium import webdriver
2 from time import sleep
3 from appium.webdriver.common.touch_action import TouchAction
4 desired_caps = {}
5 desired_caps['platformName'] = 'Android'
6 desired_caps['platformVersion'] = '9.0'
7 desired_caps['deviceName'] = 'b68548ed'
8 desired_caps['appPackage'] = 'com.kexin.iot'
9 desired_caps['appActivity'] = '.MainActivity'
10
11 driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
12 sleep(5)
13 el1 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View[1]/android.view.View/android.view.View[2]/android.view.View[2]/android.view.View[1]/android.view.View/android.view.View/android.widget.Button")
14 el1.click()#点击跳过
15 sleep(2)
16 el2 = driver.find_element_by_id('android:id/button1')
17 el2.click()#点击允许GIS定位
18 sleep(2)
19 e13 = driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View[1]/android.view.View/android.view.View[2]/android.view.View[2]/android.view.View/android.view.View[2]/android.view.View/android.view.View/android.view.View[2]/android.widget.EditText')
20 e13.send_keys('cc')
21 e14 = driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View[1]/android.view.View/android.view.View[2]/android.view.View[2]/android.view.View/android.view.View[3]/android.view.View/android.view.View/android.view.View[2]/android.widget.EditText')
22 e14.send_keys('a123456')
23 e15 = driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View[1]/android.view.View/android.view.View[2]/android.view.View[2]/android.view.View/android.widget.Button')
24 e15.click()#点击登录
25 sleep(3)

加上  desired_caps['noReset'] = 'True'

跳过初始化的代码:上面的新手教程的定位都可以省略了,效率就提升了

1 from appium import webdriver
2 from time import sleep
3 from appium.webdriver.common.touch_action import TouchAction
4 desired_caps = {}
5 desired_caps['platformName'] = 'Android'
6 desired_caps['platformVersion'] = '9.0'
7 desired_caps['deviceName'] = 'b68548ed'
8 desired_caps['appPackage'] = 'com.kexin.iot'
9 desired_caps['appActivity'] = '.MainActivity'
10 desired_caps['noReset'] = 'True'
11
12 driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
13 sleep(5)