我已经花了一个多星期的时间使用Espresso进行Android测试。 但是我无法使空闲资源在我的项目中正常工作(集成到项目中)。 这是我添加到build.gradle的内容:
dependencies {
// Testing-only dependencies
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
}
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
测试类中包含哪些内容:
SetUp:
countingResource = new CountingIdlingResource("HelloWorldServerCalls");
Espresso.registerIdlingResources(countingResource);
在测试方法中:
Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
Log.d(TAG, "We are in WalkthroughActivity, user is not logged in");
//press button - walk to next activity
Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).perform(ViewActions.click());
//register MyUserHelperV2 - this is Server decorator
final LoginActivity act = (LoginActivity) getCurrentActivity();
LoginActivity.Server aHelper = act.getUserHelper();
MyUserHelperV2 helper = new MyUserHelperV2(aHelper, countingResource);
act.setUserHelper(helper);
//set password and email
Espresso.onView(ViewMatchers.withId(R.id.email)).perform(ViewActions.typeText("test@mail.ru"));
Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText("password111"));
//Check if button R.id.btnLogInApp exists:
Espresso.onView(ViewMatchers.withId(R.id.btnLogInApp)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
Espresso.closeSoftKeyboard();
Espresso.onView(ViewMatchers.withId(R.id.btnLogInApp)).perform(ViewActions.click());
//in last line we have PerformException - can not find R.id.btnLogInApp,
但是这个按钮肯定存在 - 我可以模拟非空闲(同步)调用,所有东西都正常工作。 我认为我可能在设置方面犯了一个错误 - 在Android 5上所有的工作都正常,在其他小于Android 5的版本上则不行。
我已经仔细查看了很多示例,并且完美地实现了Idle res。但是我的gradle系统太差了。 请帮帮我,我对espresso和gradle感到绝望,如果需要,我可以附加更多的规范代码。