Appium appium 脚本运行到一半,应用就会自动退出。

hoohyou · 2015年04月25日 · 最后由 GirlAlice 回复于 2020年06月06日 · 3095 次阅读

大家好,我现在碰到一个问题。在测试 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 时,会对这种登录的情况做一些限制,导致我的脚本运行就直接退出?

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

共收到 6 条回复 时间 点赞

从日志上看,是第 7 行没定位到。

driver.findElementById("android:id/action_bar_title").click();

然后,android:id 是什么鬼,已经不是你们应用的控件了吧?不是你们应用的控件,appium 是找不到的。

应用闪退查 logcat 啊,看 appium log 不够的。

#1 楼 @anikikun 是因为在运行第四行代码后,应用就自动退出了,所以才会定位不到这个控件。

#2 楼 @chenhengjie123 恩,已经拿去给开发了~待会和他讨论下这个登录的情况

#2 楼 @chenhengjie123 忘记来回复了 0.0 原因找到了。是因为我之前 capabilities.setCapability("appActivity","com.xxx.xxx");这个 activity 在登录后就会被关闭。所以应用表现为登录后就自动退出~~另外我想问下恒洁,这个 activity 是什么作用?是 appium 对于应用的操作都要基于这个 activity 吗?所以一旦 activity 被销毁,所有的动作就不再进行。

在设置自动化相关参数容器里加上:cap.setCapability("newCommandTimeout",30000);时间可以自己设定哦!

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册