原文:http://blog.csdn.net/songer_xing/article/details/71272566html
經查詢appium1.6.3以上才能識別toast,以前安裝appium版本是1.4.X,因而重整了測試環境,這篇後半部分有記錄appium環境搭建(Android)java
官方文檔是這麼說的node
Firstly you should install appium server. Appium getting started. The version 1.6.3 or greater is recommended.android
Since version 5.x there many features based on Java 8. So we recommend to install JDK SE 8 and provide that source compatibility.shell
也就是更新appium 到1.6.3以上,java-client版本最好是5.x,jdk要用1.八、selenium要用3.x,其餘版本不必定兼容,要本身親測是否可用npm
根據要求重整環境以下:windows
appium:1.6.3babel
UiAutomator2 Drvier: 0.2.3
session
java-client :5.0.0-BETA6app
selenium:3.3.1
jdk:1.8
sdk:4.4.2
想識別toast,官網是這麼說
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.APPIUM);
You have to define this automation type to be able to use Android UIAutomator2 for new Android versions
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);
我手機系統是6.0,sdk版本UIAutomator2,加上這句
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);
運行後手機會裝以下兩個apk
io.appium.uiautomator2.server
io.appium.uiautomator2.server.test
啓動driver代碼以下:
- protected AndroidDriver<WebElement> driver;
-
- @Parameters({ "serverIP","port", "platformName","udid",
- "appPackage", "appActivity" })
- @BeforeTest
- public void setUp(String serverIP,String port,String platformName,String udid,
- String appPackage,String appActivity) throws Exception {
- DesiredCapabilities capabilities = new DesiredCapabilities();
- capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
- capabilities.setCapability("platformName", platformName);
- capabilities.setCapability("deviceName", udid);
- capabilities.setCapability("udid", udid);
- capabilities.setCapability("platformVersion", "6.0");
- capabilities.setCapability("unicodeKeyboard", true);
- capabilities.setCapability("resetKeyboard", true);
- capabilities.setCapability("noReset", true);
- capabilities.setCapability("noSign", true);
- capabilities.setCapability("appPackage", appPackage);
- capabilities.setCapability("appActivity", appActivity);
- capabilities.setCapability("automationName","uiautomator2");
-
- driver = new AndroidDriver(new URL("http://" + serverIP + ":" + port + "/wd/hub"),
- capabilities);
- }
識別toast代碼:
- final WebDriverWait wait = new WebDriverWait(driver,3);
- Assert.assertNotNull(wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[contains(@text,'"+ toast + "')]"))));
- log.info("查找toast成功!");
- return true;
- } catch (Exception e) {
- throw new AssertionError("找不到"+toast);
運行後driver初始化失敗,也沒有裝如上所說的兩個apk,去掉這句
- [debug] [ADB] Running 'D:\andriod\android-sdk-windows\platform-tools\adb.exe' wi
- th args: ["-P",5037,"-s","WTKDU16707010313","shell","pm","list","packages","-3",
- "io.appium.uiautomator2.server.test"]
- [debug] [ADB] App is not installed
- [debug] [ADB] Checking app cert for C:\Users\Administrator\AppData\Roaming\npm\n
- ode_modules\appium\node_modules\appium-uiautomator2-driver\uiautomator2\appium-u
- iautomator2-server-v0.0.8.apk.
- [debug] [ADB] App already signed.
- [debug] [ADB] Zip-aligning 'C:\Users\Administrator\AppData\Roaming\npm\node_modu
- les\appium\node_modules\appium-uiautomator2-driver\uiautomator2\appium-uiautomat
- or2-server-v0.0.8.apk'
- [ADB] Checking whether zipalign is present
- [debug] [ADB] App not signed with debug cert.
- [debug] [ADB] Resigning apk.
- [debug] [ADB] Zip-aligning 'C:\Users\Administrator\AppData\Roaming\npm\node_modu
- les\appium\node_modules\appium-uiautomator2-driver\uiautomator2\appium-uiautomat
- or2-server-v0.0.8.apk'
- [ADB] Checking whether zipalign is present
- [debug] [UiAutomator2] Deleting UiAutomator2 session
- [debug] [UiAutomator2] Deleting UiAutomator2 server session
- [UiAutomator2] Did not get confirmation UiAutomator2 deleteSession worked; Error
- was: Error: Trying to proxy a session command without session id
- [debug] [ADB] Getting connected devices...
- [debug] [ADB] 1 device(s) connected
- [debug] [ADB] Running 'D:\andriod\android-sdk-windows\platform-tools\adb.exe' wi
- th args: ["-P",5037,"-s","WTKDU16707010313","shell","am","force-stop","com.fxicr
- azy.sjml"]
- [debug] [Logcat] Stopping logcat capture
- [debug] [ADB] Removing forwarded port socket connection: 8200
- [debug] [ADB] Running 'D:\andriod\android-sdk-windows\platform-tools\adb.exe' wi
- th args: ["-P",5037,"-s","WTKDU16707010313","forward","--remove","tcp:8200"]
- [MJSONWP] Encountered internal error running command: Error: Could not find zipa
- lign.exe in tools, platform-tools, or supported build-tools under D:\andriod\and
- roid-sdk-windows do you have the Android SDK installed at this location?
- at ADB.callee$0$0$ (../../../lib/tools/system-calls.js:66:11)
- at tryCatch (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\
- node_modules\babel-runtime\regenerator\runtime.js:67:40)
- at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\Administrator\Ap
- pData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\run
- time.js:315:22)
- at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (C:\U
- sers\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules\babel-ru
- ntime\regenerator\runtime.js:100:21)
- at GeneratorFunctionPrototype.invoke (C:\Users\Administrator\AppData\Roaming
- \npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:3
- 7)
- [HTTP] <-- POST /wd/hub/session 500 8932 ms - 310
- [HTTP] --> POST /wd/hub/session {"capabilities":[{"desiredCapabilities":{"appPac
- kage":"com.fxicrazy.sjml","appActivity":".ui.welcome.WelcomeActivity","noSign":t
- rue,"platformVersion":"6.0","automationName":"UIAutomator2","platformName":"Andr
- oid","deviceName":"Android Emulator"}},{"requiredCapabilities":{}}]}
- [debug] [MJSONWP] Bad parameters: BadParametersError: Parameters were incorrect.
- We wanted {"required":["desiredCapabilities"],"optional":["requiredCapabilities
- ","capabilities","sessionId","id","sessionId","id","sessionId","id"]} and you se
- nt ["capabilities"]
看這個報錯信息有點蒙,服務日誌一句一句跟下來,發現這句
- [MJSONWP] Encountered internal error running command: Error: Could not find zipa
- lign.exe in tools, platform-tools, or supported build-tools under D:\andriod\and
- roid-sdk-windows do you have the Android SDK installed at this location?
額,原來是sdk沒這個工具,沒辦法進行 Zip-aligning apk,而後安裝。因此報錯,非常欣喜,而後用360手機助手手動安裝了這兩個apk到手機,再次運行,一直在等待uiautomator2,最後超時報錯,說明uiautomator2 server沒起來
- [UiAutomator2] Waiting for UiAutomator2 to be online...
- [debug] [JSONWP Proxy] Proxying [GET /status] to [GET http://localhost:8200/wd/h
- ub/status] with no body
- [debug] [JSONWP Proxy] Proxying [GET /status] to [GET http://localhost:8200/wd/h
- ub/status] with no body
- [debug] [JSONWP Proxy] Proxying [GET /status] to [GET http://localhost:8200/wd/h
- ub/status] with no body
- [debug] [JSONWP Proxy] Proxying [GET /status] to [GET http://localhost:8200/wd/h
- ub/status] with no body
百度相關信息特別少,只能查官網,按官網所說個人配置,代碼都沒問題。查單元測試源碼,也證實這樣作是對的,後來懷疑,appium版本與java-client,selenium版本不兼容,前先後後換了好幾個版本,整了好幾天,仍是不行,最後也排除了這幾個版本兼容性,最後更新了sdk,最高支持> POST /wd/hub/session/be19a6ec-33b4-49c2-a209-b6e7fd8ad9ce/elements {"
- using":"xpath","value":".//*[contains(@text,'當前小區暫無門禁設備')]"}
- [debug] [MJSONWP] Calling AppiumDriver.findElements() with args: ["xpath",".//*[
- contains(@text,'當前小區暫無門禁設備')]","be19a6ec-33b4-49c2-a209-b6e7fd8ad9ce"]
-
- [debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class
- name, accessibility id, -android uiautomator
- [debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class
- name, accessibility id, -android uiautomator
- [debug] [BaseDriver] Waiting up to 15000 ms for condition
- [debug] [JSONWP Proxy] Proxying [POST /elements] to [POST http://localhost:8200/
- wd/hub/session/8b26bb2b-fc12-446d-8158-68b75ee0d44a/elements] with body: {"strat
- egy":"xpath","selector":".//*[contains(@text,'當前小區暫無門禁設備')]","context"
- :"","multiple":true}
- [debug] [JSONWP Proxy] Got response with status 200: {"sessionId":"8b26bb2b-fc12
- -446d-8158-68b75ee0d44a","status":0,"value":[{"ELEMENT":"d794bb72-e404-49ac-b196
- -d61b420a8291"}]}
- [debug] [MJSONWP] Responding to client with driver.findElements() result: [{"ELE
- MENT":"d794bb72-e404-49ac-b196-d61b420a8291"}]
- [HTTP] <-- POST /wd/hub/session/be19a6ec-33b4-49c2-a209-b6e7fd8ad9ce/elements 20
- 0 982 ms - 124
最後針對此次問題的解決過程,又作了從新梳理,最後得出結論是io.appium.uiautomator2.server、io.appium.uiautomator2.server.test這兩個apk,必定要進行Zip-aligning apk,而後再安裝,才能正常使用,手動安裝不行,跟sdk版本是否更新到6.0無直接關係,中間踩了好幾個坑,費了好幾天時間。你們能夠借鑑一下,有什麼問題,也能夠留言,你們一塊兒討論。