Appium 使用 uiautomator 2 檢測 toast, 同樣的程式碼在 Pixel 2XL (android 10) 上就可以正常執行;在 Pixel 2 (android 9) 上就報錯,搞了一段時間沒搞定,請各位出出招,謝謝

tancaidexiaoming發表於2020-08-10

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) 上就報錯,搞了一段時間沒搞定,請各位出出招,謝謝!

相關文章