原文:http://blog.csdn.net/songer_xing/article/details/71272566
经查询appium1.6.3以上才能识别toast,之前安装appium版本是1.4.X,于是重整了测试环境,这篇后半部分有记录appium环境搭建(Android)
官方文档是这么说的
Firstly you should install appium server. Appium getting started. The version 1.6.3 or greater is recommended.
Since version 5.x there many features based on Java 8. So we recommend to install JDK SE 8 and provide that source compatibility.
也就是更新appium 到1.6.3以上,java-client版本最好是5.x,jdk要用1.8、selenium要用3.x,其他版本不一定兼容,要自己亲测是否可用
根据要求重整环境如下:
appium:1.6.3
UiAutomator2 Drvier: 0.2.3
java-client :5.0.0-BETA6
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");//调用uiautomator2,获取toast
- 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:andriodandroid-sdk-windowsplatform-toolsadb.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:UsersAdministratorAppDataRoaming pm
- ode_modulesappium ode_modulesappium-uiautomator2-driveruiautomator2appium-u
- iautomator2-server-v0.0.8.apk.
- [debug] [ADB] App already signed.
- [debug] [ADB] Zip-aligning 'C:UsersAdministratorAppDataRoaming pm ode_modu
- lesappium ode_modulesappium-uiautomator2-driveruiautomator2appium-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:UsersAdministratorAppDataRoaming pm ode_modu
- lesappium ode_modulesappium-uiautomator2-driveruiautomator2appium-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:andriodandroid-sdk-windowsplatform-toolsadb.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:andriodandroid-sdk-windowsplatform-toolsadb.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:andriodand
- 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:UsersAdministratorAppDataRoaming pm ode_modulesappium
- node_modulesabel-runtime egenerator untime.js:67:40)
- at GeneratorFunctionPrototype.invoke [as _invoke] (C:UsersAdministratorAp
- pDataRoaming pm ode_modulesappium ode_modulesabel-runtime egenerator un
- time.js:315:22)
- at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (C:U
- sersAdministratorAppDataRoaming pm ode_modulesappium ode_modulesabel-ru
- ntime egenerator untime.js:100:21)
- at GeneratorFunctionPrototype.invoke (C:UsersAdministratorAppDataRoaming
- pm ode_modulesappium ode_modulesabel-runtime egenerator untime.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:andriodand
- 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无直接关系,中间踩了好几个坑,费了好几天时间。大家可以借鉴一下,有什么问题,也可以留言,大家一起讨论。