我尝试过使用Espresso测试框架并编写了一个简单的测试.
没有什么花哨的,只有这个代码:
onView(withId(R.id.login_button_stub)).perform(click());
Run Code Online (Sandbox Code Playgroud)
测试在Nexus 5设备和Genymotion与Android 5.0上运行良好,但在Android Emulator 4.x,5.0,Genymotion 2.3,4.x和LG G2与Android 4.x上运行失败.
在所有这些设备/模拟器/模拟器上,单击按钮并执行其操作(在我的情况下转到另一个屏幕).问题是它阻塞了perform某些设备.这在60秒后以一个很好的例外结束:
android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: com.vielengames.mock:id/login_button_stub'.
...
Caused by: android.support.test.espresso.AppNotIdleException: Looped for 3544 iterations over 60 SECONDS. The following Idle Conditions failed .
...
Run Code Online (Sandbox Code Playgroud)
请注意,没有关于哪些条件失败的信息.查看Espresso源代码,应该有一个以逗号分隔的问题列表.
这是完整的代码:TestCase.java.
和完整的堆栈跟踪:
I/TestRunner? android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: com.vielengames.mock:id/login_button_stub'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:70)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:185)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.vielengames.ui.NotLoggedOnTestCase.testLoggedOnAfterLoginClick(NotLoggedOnTestCase.java:19)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at …Run Code Online (Sandbox Code Playgroud) 当我在运行一些espresso测试时显示的布局中有ProgressBar时,我会遇到:
Caused by: android.support.test.espresso.AppNotIdleException: Looped for 1670 iterations over 60 SECONDS. The following Idle Conditions failed .
Run Code Online (Sandbox Code Playgroud)
解决这个问题的好方法是什么?找到一些hackish东西,但寻找一个很好的方式
@Test
public void test3_PaySuccessful(){
init();
ViewInteraction amountEditText = onView(
allOf(withId(R.id.et_amount), isDisplayed()));
amountEditText.perform(replaceText("SGD 0.010"), closeSoftKeyboard());
//, withText("Proceed")
ViewInteraction appCompatButton = onView(
allOf(withId(R.id.btn_confirm), isDisplayed()));
appCompatButton.perform(click());
//, withText("Pay")
ViewInteraction appCompatButton2 = onView(
allOf(withId(R.id.btn_confirm), isDisplayed()));
appCompatButton2.perform(click());
//dialog
ViewInteraction appCompatButton3 = onView(
allOf(withId(R.id.confirm_button), withText("Confirm"), isDisplayed()));
appCompatButton3.perform(click());
//have to disable animation in order to pass this.
intended(CoreMatchers.allOf(hasComponent(PaymentSelectionActivity2.class.getName())));
}
Run Code Online (Sandbox Code Playgroud)
我在使用涉及动画的视图进行Espresso测试时遇到了一个问题,我知道Espresso无法处理动画,所以我在下面做了. - 禁用我的测试设备窗口动画,过渡动画和动画设计持续时间缩放都设置为OFF(这不起作用) - 然后我尝试在我的代码中添加一个标志,例如.espresso_testing = true.如果为true,我的代码将跳过调用所有startAnimation()函数调用.--->这是有效的.但是,在编写espresso测试用例时,我无法更改应用程序中的代码.包括上面的测试用例.
有没有其他方法可以做到这一点?提前致谢.
我有一个视图,其中一个元素在无限循环中使用以下动画进行动画处理:
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:duration="10000"
android:repeatCount="-1"
android:repeatMode="reverse"/>
Run Code Online (Sandbox Code Playgroud)
当Espresso打开我的活动时,它可以执行一些操作但很快冻结.我想Espresso正在等待UI线程变为空闲,在这种情况下永远不会发生.
是我测试此视图以实现禁用动画的机制的唯一方法吗?我可以有另一个类处理动画,这将被模拟为测试.或者建立时间条件.
编辑:我还没有创建一个示例项目来尝试重新创建该问题,但在此期间还有一些进一步的细节:
1)我正在使用Jake Wharton ActivityRule自动启动我的活动(https://gist.github.com/JakeWharton/1c2f2cadab2ddd97f9fb).
2)这是我的测试:
onView(withId(R.id.btn_yes)).perform(click());
Run Code Online (Sandbox Code Playgroud)
3)这是完整的堆栈跟踪.注意AppNotIdleException:
Running tests
Test running started
android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: com.myapp:id/btn_yes'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:70)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:185)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.myapp.espresso.MyActivityTest.yesButtonTest(MyActivityTest.java:53)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at com.myapp.espresso.ActivityRule$2.evaluate(ActivityRule.java:129)
at org.junit.rules.RunRules.evaluate(RunRules.java:18)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at …Run Code Online (Sandbox Code Playgroud) 我正在使用Junit4和Espresso进行测试.我正面临一个与espresso测试相关的奇怪问题 - 当我调用onView时,有时一切都按照它应该执行,但有时候我的测试会冻结,60秒后我得到类似这样的东西"android.support.test.espresso.AppNotIdleException:Looped超过60秒的63次迭代.以下空闲条件失败ASYNC_TASKS_HAVE_IDLED"例如:
onView(withId(R.id.zone_button_continue)).perform(click());
onView(withId(R.id.loginButton)).check(matches(isDisplayed()));
onView(withId(R.id.number)).check(matches(isDisplayed()));
onView(withId(R.id.password)).check(matches(isDisplayed()));
onView(withId(R.id.number)).perform(typeText(phoneNumber));
onView(withId(R.id.password)).perform(typeText(password));
onView(withId(R.id.loginButton)).perform(click());
Run Code Online (Sandbox Code Playgroud)
一切都得到了正确执行,除了最后一行 - 由于某种原因,它找不到loginButton,但它有100%的百分比.我可以
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.findViewById(R.id.loginButton).performClick();
}
});
Run Code Online (Sandbox Code Playgroud)
这很有效.在我的大多数活动中我都遇到过类似的问题 - 有时onView工作有时候没有.
什么可能导致这种不一致?我一直在与这个问题作斗争,这让我的测试非常令人沮丧和困难.我的结论到目前为止我的gradle依赖项出了问题,或者我的应用程序中存在某种阻止espresso正常工作的错误.但奇怪的是,在上面的例子中,onView的所有内容都在工作,除了最后一行,一切都发生在一个活动中,这让我怀疑这个bug是在我的应用程序中.这是我的gradle依赖项:
compile files('src/main/libs/guice-3.0-no_aop.jar')
compile files('src/main/libs/javax.inject-1.jar')
compile files('src/main/libs/roboguice-2.0.jar')
compile files('src/main/libs/junit-4.11.jar')
compile files('src/main/libs/hamcrest-core-1.3.jar')
compile files('src/main/libs/GeoLib.jar')
compile files('src/main/libs/GeoPolygons.jar')
compile files('src/main/libs/universal-image-loader-1.9.4.jar')
compile files('src/main/libs/javax.annotation-3.2-b06-sources.jar')
compile ('uk.co.chrisjenx:calligraphy:2.1.0'){
exclude group:'com.android.support'
}
compile 'com.squareup:otto:1.3.5'
compile ('com.google.android.gms:play-services:6.5.87'){
exclude group:'com.android.support'
}
compile 'com.android.support:support-annotations:22.2.1'
compile ('com.android.support:appcompat-v7:22.2.0'){
exclude group:'com.android.support'
}
compile ('com.android.support:support-v4:22.2.0'){
exclude group:'com.android.support', module:'support-annotations'
}
compile ('com.android.support:palette-v7:22.2.0'){
exclude group:'com.android.support'
}
compile 'com.google.code.findbugs:jsr305:2.0.1'
compile 'com.nineoldandroids:library:2.4.0' …Run Code Online (Sandbox Code Playgroud) 我实际上尝试用Espresso设置一些单元测试,经过几个小时的研究,应用程序只做了点击并通过EditText获得焦点,但之后没有任何内容
Caused by: android.support.test.espresso.AppNotIdleException: Looped for 1996 iterations over 60 SECONDS. The following Idle Conditions failed .
Run Code Online (Sandbox Code Playgroud)
我删除了所有动画和SwipeRefreshLayout因为我看到swiperefresh有一个错误
我实际上使用一些回调来替换Activity中的当前片段
如果有人有一些提示,我会在搜索u_u 4小时后离开
谢谢 :)
我的Gradle依赖项:
// App dependencies
compile 'com.android.support:support-annotations:23.3.0'
compile 'com.google.guava:guava:18.0'
// Testing-only dependencies
// Force usage of support annotations in the test app, since it is internally used by the runner module.
androidTestCompile 'com.android.support:support-annotations:23.3.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
Run Code Online (Sandbox Code Playgroud)
我在defaultConfig上添加了这个:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Run Code Online (Sandbox Code Playgroud)
有我的测试:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class HelloWorldEspressoTest {
public static final String USERNAME = "do_f"; …Run Code Online (Sandbox Code Playgroud) 当我登陆页面时,我不断收到此错误.有什么可以解决这个问题?
android.support.test.espresso.AppNotIdleException: Looped for 4539 iterations over 60 SECONDS. The following Idle Conditions failed .
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
Run Code Online (Sandbox Code Playgroud)