大家好,我现在碰到一个问题。在测试 apk(暂命名为 A 应用)时,运行到第 4 行代码后应用就会自动退出了,然后在运行第 4 行代码后报错:定位不到元素。下面是我的 java 代码。
系统:win7.
appium 版本:1.3.4(之前 用 1.3.7 也这样)
java:1.7

public void testApp() throws Exception {
        1 driver.findElementById("com.wacai365:id/etAccount").sendKeys("hys1990");
        2 driver.findElementById("com.wacai365:id/password").sendKeys("123456");
        3 Thread.sleep(1000);
        4 driver.findElementById("com.wacai365:id/billInfo").click();
        5 Thread.sleep(1000);
        6 driver.findElementById("android:id/action_bar_title").click();
        7 Thread.sleep(1000);
}

log 信息:

info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: setText
info: [debug] [BOOTSTRAP] [debug] Attempting to clear using UiObject.clearText().
info: [debug] [BOOTSTRAP] [debug] Sending plain text to element: 123456
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
info: [debug] Responding to client with success: {"status":0,"value":true,"sessionId":"de479588-018b-454f-80ad-83d7c96bf45e"}
info: <-- POST /wd/hub/session/de479588-018b-454f-80ad-83d7c96bf45e/element/3/value 200 5267.636 ms - 76 {"status":0,"value":true,"sessionId":"de479588-018b-454f-80ad-83d7c96bf45e"}
info: --> POST /wd/hub/session/de479588-018b-454f-80ad-83d7c96bf45e/element {"using":"id","value":"com.wacai365:id/billInfo"}
info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"com.wacai365:id/billInfo","context":"","multiple":false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.wacai365:id/billInfo","context":"","multiple":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding com.wacai365:id/billInfo using ID with the contextId: multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.wacai365:id/billInfo]
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"ELEMENT":"4"},"status":0}
info: [debug] Responding to client with success: {"status":0,"value":{"ELEMENT":"4"},"sessionId":"de479588-018b-454f-80ad-83d7c96bf45e"}
info: <-- POST /wd/hub/session/de479588-018b-454f-80ad-83d7c96bf45e/element 200 27.666 ms - 87 {"status":0,"value":{"ELEMENT":"4"},"sessionId":"de479588-018b-454f-80ad-83d7c96bf45e"}
info: --> POST /wd/hub/session/de479588-018b-454f-80ad-83d7c96bf45e/element/4/click {"id":"4"}
info: [debug] Pushing command to appium work queue: ["element:click",{"elementId":"4"}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:click","params":{"elementId":"4"}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: click
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
info: [debug] Responding to client with success: {"status":0,"value":true,"sessionId":"de479588-018b-454f-80ad-83d7c96bf45e"}
info: <-- POST /wd/hub/session/de479588-018b-454f-80ad-83d7c96bf45e/element/4/click 200 278.324 ms - 76 {"status":0,"value":true,"sessionId":"de479588-018b-454f-80ad-83d7c96bf45e"}
info: --> POST /wd/hub/session/de479588-018b-454f-80ad-83d7c96bf45e/element {"using":"id","value":"android:id/action_bar_title"}
info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"android:id/action_bar_title","context":"","multiple":false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"android:id/action_bar_title","context":"","multiple":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding android:id/action_bar_title using ID with the contextId: multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=android:id/action_bar_title]
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"No element found","status":7}
info: [debug] Condition unmet after 538ms. Timing out.
info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"No element found"},"sessionId":"de479588-018b-454f-80ad-83d7c96bf45e"}
info: <-- POST /wd/hub/session/de479588-018b-454f-80ad-83d7c96bf45e/element 500 540.154 ms - 195
info: --> DELETE /wd/hub/session/de479588-018b-454f-80ad-83d7c96bf45e {}
info: Shutting down appium session
info: [debug] Pressing the HOME button
info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 192.168.56.101:5555 shell "input keyevent 3"
info: [debug] Resetting IME to 'com.android.inputmethod.latin/.LatinIME'
info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 192.168.56.101:5555 shell "ime set com.android.inputmethod.latin/.LatinIME"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"OK, shutting down","status":0}
info: [debug] [BOOTSTRAP] [debug] Closed client connection
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
info: [debug] [UIAUTOMATOR STDOUT] Time: 19.756
info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
info: [debug] UiAutomator shut down normally
info: [debug] Cleaning up android objects
info: [debug] Cleaning up appium session
info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"de479588-018b-454f-80ad-83d7c96bf45e"}
info: <-- DELETE /wd/hub/session/de479588-018b-454f-80ad-83d7c96bf45e 200 1349.537 ms - 76 {"status":0,"value":null,"sessionId":"de479588-018b-454f-80ad-83d7c96bf45e"}

1,在自带的模拟器和 genymotion 上都是如此。不过我安装到 genymotion 上时报错,将 x86 处理器换成 atm 后就安装成功了。这个是否有影响?
2,是否开发 apk 时,会对这种登录的情况做一些限制,导致我的脚本运行就直接退出?

不知道大家有没有碰到过这种运行到一半,应用自动退出的情况。


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