Android 误报:junit.framework.AssertionFailedError:未找到EditText

Android 误报:junit.framework.AssertionFailedError:未找到EditText,android,android-emulator,robotium,travis-ci,Android,Android Emulator,Robotium,Travis Ci,我在设置Robotium测试以在Travis上运行时没有随机的假阳性时遇到问题 我得到的每两个版本 pl.mg6.agrtt.TestActivityTests > testCanEnterTextAndPressButton[test(AVD) - 4.4.2] FAILED junit.framework.AssertionFailedError: EditText is not found! at com.robotium.solo.Waiter.waitForAnd

我在设置Robotium测试以在Travis上运行时没有随机的假阳性时遇到问题

我得到的每两个版本

pl.mg6.agrtt.TestActivityTests > testCanEnterTextAndPressButton[test(AVD) - 4.4.2] FAILED
    junit.framework.AssertionFailedError: EditText is not found!
    at com.robotium.solo.Waiter.waitForAndGetView(Waiter.java:540)
在我所有的测试中

我已创建以显示问题。
您可以看到它是如何构建的。注意:修改无关文件后,构建#7失败

我怀疑这是由模拟器被锁定或其屏幕变暗引起的。我可以在本地机器上通过关闭连接设备的屏幕然后运行来重现这个问题

./gradlew connectedAndroidTest

在modyfing测试之后,我得到了一条不同的错误消息,这是一条信息量更大的消息,所以我添加它只是为了防止有人试图找到解决方案:

pl.mg6.agrtt.TestActivityTests > testCanFindViewsEnterTextAndPressButton[test(AVD) - 4.4.2] FAILED
    junit.framework.AssertionFailedError: Click at (160.0, 264.0) can not be completed! (java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission)
    at com.robotium.solo.Clicker.clickOnScreen(Clicker.java:106)

你的猜测可能是对的。确保这一点的一种方法是捕获抛出的异常并调用它

solo.takeScreenshot("screenshotFileName");
然后查看保存在手机SD卡中的屏幕截图,以查看发生错误时手机正在执行的操作


我通过打开设备的“保持清醒”设置解决了这个问题,这样它在充电时就不会睡觉。

Robotium在使用
enterText(int,String)
时会丢弃不可见的视图。使用
Solo
getView(int)
enterText(view,String)
中使用结果视图

像这样:

public void testCanEnterTextAndPressButton() {
    solo.enterText(((EditText) solo.getView(R.id.editText1)), "my login");
    solo.enterText(((EditText) solo.getView(R.id.editText2)), "my password");
    solo.clickOnView(solo.getView(R.id.button));
}
如果设备屏幕被锁定,Robotium将无法运行您给出的指令。您可能希望禁用屏幕锁定


通过上面的代码,我的测试通过了。

虽然我仍然不知道这个问题的根本原因,但经过一些调查,在Robotium作者的帮助下,我可以确认我最初怀疑emulator确实锁定了自己

我现在使用的一种解决方法是将此代码放入
setUp
方法:

getInstrumentation().runOnMainSync(new Runnable() {
    @Override
    public void run() {
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    }
});

谢谢你的建议。我将尝试找出如何从Travis上的模拟器中获取屏幕截图。遗憾的是,无法使用
takeScreenshot
,因为如果(decorView==null)返回,它会
在它的内部,看起来它就是找不到装饰视图,就像它找不到my
EditText
一样。请查看这些
http://stackoverflow.com/questions/22392654/getting-started-with-robotium-edittext-not-found
http://stackoverflow.com/questions/16683702/junit-framework-assertionfailederror-while-testing-back-button-on-android
。这可能会对您有所帮助。@Stephen感谢您指出这些案例,但它们是关于测试代码中的错误,这些错误会导致测试总是失败。如果你看看travis,你会发现我的“测试”总是通过,但可能会随机失败(build#7)。当你关闭屏幕时,Android不会破坏
视图。只有在调用
onDestroy
后,它们才会被销毁。你有没有解释为什么只有一个单一版本在Travis上失败,而没有使用“保持清醒”功能?你知道在这个设置中禁用屏幕锁定的方法吗?你可以使用“设置”应用程序来做到这一点。设置>安全性>屏幕锁定>无。或者您可以通过以下代码执行此操作:
mKeyGuardManager=(KeyguardManager)getSystemService(keyguardservice);mLock=mKeyGuardManager.newKeyguardLock(“活动类名称”);mLock.disableKeyguard()在我的物理设备(Nexus5和KitKat)上也适用于我。谢谢插曲。在活动测试中,要解锁设备,您需要调用
KeyguardManager.KeyguardLock.disableKeyguard()
。此方法需要
DISABLE_KEYGUARD
权限。看来你的是个更好的选择。你知道为什么你的替代品没有在文档中使用吗?@niekharman在
KeyguardLock
中也提到了不推荐使用的内容。我想他们只是没有足够的人手来更新整个文档。这方面的例子很多。