Java junit.framework.AssertionFailedError:Back按钮可以';别着急
我这样做是为了使用Android UIAutomator退出应用程序Java junit.framework.AssertionFailedError:Back按钮可以';别着急,java,android,ui-automation,android-uiautomator,Java,Android,Ui Automation,Android Uiautomator,我这样做是为了使用Android UIAutomator退出应用程序 assertTrue("Back button can't be pressed", getUiDevice().pressBack()); assertTrue("Back button can't be pressed", getUiDevice().pressBack()); // This line will giving exception. 当我们按下两次后退按钮时,我的应用程序将退出,这就是为什么我要调用
assertTrue("Back button can't be pressed", getUiDevice().pressBack());
assertTrue("Back button can't be pressed", getUiDevice().pressBack()); // This line will giving exception.
当我们按下两次后退按钮时,我的应用程序将退出,这就是为什么我要调用它两次
但是
然后它会给我一个例外
但是,当我手动返回两次时,它将非常有效
例外情况是:
junit.framework.AssertionFailedError: Back button can't be pressed
at com.android.jdsu.automation.youtube.YoutubeUiTest.exitApplicaion(Yout
ubeUiTest.java:487)
at com.android.jdsu.automation.youtube.YoutubeUiTest.testYouTube(Youtube
UiTest.java:208)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAuto
matorTestRunner.java:160)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.run(UiAutoma
torTestRunner.java:96)
at com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.ja
va:91)
at com.android.commands.uiautomator.Launcher.main(Launcher.java:83)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
at dalvik.system.NativeStart.main(Native Method)
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
INSTRUMENTATION_STATUS: test=testYouTube
INSTRUMENTATION_STATUS: class=com.android.jdsu.automation.youtube.YoutubeUiTest
INSTRUMENTATION_STATUS: stack=junit.framework.AssertionFailedError: Back button
can't be pressed
at com.android.jdsu.automation.youtube.YoutubeUiTest.exitApplicaion(Yout
ubeUiTest.java:487)
at com.android.jdsu.automation.youtube.YoutubeUiTest.testYouTube(Youtube
UiTest.java:208)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAuto
matorTestRunner.java:160)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.run(UiAutoma
torTestRunner.java:96)
at com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.ja
va:91)
at com.android.commands.uiautomator.Launcher.main(Launcher.java:83)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
at dalvik.system.NativeStart.main(Native Method)
有趣的是,您是否尝试在两次呼叫之间添加等待时间。您的应用程序可能需要一些时间来重新加载上一个活动。增加至少500毫秒或1秒的等待时间 是的,我也经历过同样的问题,但不明白为什么会发生这种情况。 我使用以下方法使其工作:
UiDevice device = UiDevice.getInstance();
device.pressMenu();
Runtime.getRuntime().exec("/system/bin/input keyevent 82");
这对我来说很好。
否则,您也可以按如下方式使用keyevent
:
UiDevice device = UiDevice.getInstance();
device.pressMenu();
Runtime.getRuntime().exec("/system/bin/input keyevent 82");
assertTrue(“无法按下后退按钮”,getUiDevice().pressBack())代码>
如果用户界面在pressBack()
之后未更改,则返回false;如果当前视图在主视图中,则在getUiDevice().pressBack()之后返回false代码>这是一个有趣的问题:
基于此,我们不能信任UIDevice.pressBack()
,至少如果我们信任Appium
但令人好奇的是,它在大多数情况下都有效,而且实际上返回的是真的。根据我的经验,它可以与Intent Chooser、Google Drive、Google Play Store配合使用,但不能与API 19上的设备设置配合使用。到目前为止,我有一个比Appium的解决方案更严格的解决方案:
@RequiresApi(VERSION_CODES.JELLY_BEAN)
public static void pressBackExternal() {
UiDevice device = UiDevice.getInstance(getInstrumentation());
if (VERSION.SDK_INT == VERSION_CODES.KITKAT
&& "com.android.settings".equals(device.getCurrentPackageName())) {
// fails because pressBack sometimes returns false even though the Settings is closed.
device.pressBack();
} else {
assertTrue("expected to press Back button", device.pressBack());
}
}
是的,我也使用了线程。在两次调用之间使用sleep(3000)
,但仍然会给出相同的异常。Shivam,你会将睡眠放在哪里?问题是pressBack()
返回false。把它放在前面不会有任何效果,因为后退按钮还没有按下,所以没有什么可以等待的。把它放在后面太晚了,因为按钮已经按下并处理了;已确定返回值为false。martin,我可以看到pressBack()
的效果,顶部的活动已关闭,另一个活动已激活。我仍然得到false
作为结果。顺便说一句,“back”(如OP中所述)是/system/bin/input keyevent 4
(android.view.keyevent#KEYCODE_back
中的常量)