Appium 版本:1.13.0
SDK:

这些都已经安装好了

使用 uiautomator 2 检测 toast, 同样的代码在 Pixel 2XL (android 10) 上就可以正常运行;在 Pixel 2 (android 9) 上就报错:
(1)cmd:

E
======================================================================
ERROR: test_case (__main__.BtSwitchTset)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "Developer_Toast_2.py", line 36, in setUp
    self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
  File "C:\Users\cody.ming\AppData\Local\Programs\Python\Python37\lib\site-packages\appium\webdriver\webdriver.py", line 144, in __init__
    proxy
  File "C:\Users\cody.ming\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\cody.ming\AppData\Local\Programs\Python\Python37\lib\site-packages\appium\webdriver\webdriver.py", line 219, in start_session
    response = self.execute(RemoteCommand.NEW_SESSION, parameters)
  File "C:\Users\cody.ming\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\cody.ming\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Error executing adbExec. Original error: 'Command 'E\:\\Andr
oid_for_Appium\\android-sdk_r24.4.1-windows\\android-sdk-windows\\platform-tools\\adb.exe -P 5037 -s FA7971A04040 shell pm install /data/local/tmp/appium_cache/56abf1bf10f6562619a2dd8fb060718e27c4cb28
.apk' exited with code 1'; Stderr: ''; Code: '1'


----------------------------------------------------------------------
Ran 1 test in 17.730s

FAILED (errors=1)

(2)Appium:
看着就这四句黄叹号有些问题

另外,代码如下:

import subprocess
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest
import time, os
from selenium.webdriver.support import expected_conditions as EC
import uiautomator2 as u2
from selenium.webdriver.common.by import By
import warnings

class ToastTset(unittest.TestCase):

    def setUp(self):
        warnings.simplefilter("ignore", ResourceWarning)
        os.system("adb shell input keyevent 26")
        os.system("adb shell input keyevent 3")
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '9'   #Pixel 2
        desired_caps['deviceName'] = 'FA7971A04040'   #Pixel 2
        desired_caps['appPackage'] = 'com.android.settings'
        desired_caps['appActivity'] = 'com.android.settings.Settings'
        desired_caps ['automationName'] ='UiAutomator2'

        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    def test_case(self):

        driver = self.driver

        item = driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className("android.widget.TextView"), "系统", true)')
        item.click()
        driver.find_element_by_android_uiautomator('new UiSelector().text("关于手机")').click()
        item_ver = driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className("android.widget.TextView"), "版本号", true)')
        item_ver.click()
        try:
            #检测toast内容的方法
            toast_loc = ("xpath", ".//*[contains(@text,'您已处于')]")
            t = WebDriverWait(driver, 30, 0.01).until(EC.presence_of_element_located(toast_loc))
            print(t)  #若检测到toast,则会输出一句session信息
            print(driver.find_element_by_xpath(".//*[contains(@text,'您已处于')]").text) #若检测到,输出toast内容
        except:
            print("非开发者模式")
        time.sleep(5)

    def tearDown(self):
        self.driver.quit()
        os.system("adb shell input keyevent 26")

if __name__ == '__main__':
    unittest.main()

同样的代码在 Pixel 2XL (android 10) 上就可以正常运行;在 Pixel 2 (android 9) 上就报错,搞了一段时间没搞定,请各位出出招,谢谢!


↙↙↙阅读原文可查看相关链接,并与作者交流