测试使用浓咖啡登录引发异常
问题描述:
我试图在按下按钮时尝试登录时正在更改按钮的文本。登录按钮文本实际上是将文本更改为正在验证...当系统正在检查服务器的凭据,并且一旦完成,文本更改为LOGIN再次。每当我尝试用espresso测试时,UI部分正在完成将值分配给edittext并单击该按钮,然后该线程冻结,几次后它会引发错误。由于我是测试新手,如果能够解释如何解决此问题或者我应该如何处理这种情况,我将不胜感激。测试使用浓咖啡登录引发异常
这是我的测试班。
@LargeTest
@RunWith(AndroidJUnit4.class)
public class LoginTest {
private String userName;
private String userPass;
private LoginIdlingResource idlingResource;
@Rule
public ActivityTestRule<LoginActivity> activityRule = new
ActivityTestRule<LoginActivity>(LoginActivity.class);
@Before
public void assignCredentials(){
userName = "ABC";
userPass = "ABC";
}
@Before
public void registerIntentServiceIdlingResource() throws Exception {
LoginActivity activity = activityRule.getActivity();
idlingResource = new LoginIdlingResource(activity);
Espresso.registerIdlingResources(idlingResource);
}
@Test
public void buttonTextChanged(){
onView(withId(R.id.edittext_user))
.perform(typeText(userName));
onView(withId(R.id.edittext_pass))
.perform(typeText(userPass));
onView(withId(R.id.submit_login))
.perform(click());
onView(withId(R.id.submit_login))
.check(matches(withText("Verifying...")));
}
@After
public void unregisterIntentServiceIdlingResource() {
Espresso.unregisterIdlingResources(idlingResource);
}
}
我准备了一个IdlingResource。
public class LoginIdlingResource implements IdlingResource {
private LoginActivity mActivity;
private ResourceCallback mCallBack;
public LoginIdlingResource(LoginActivity context){
mActivity = context;
}
@Override
public String getName() {
return "LoginActivityIdleName";
}
@Override
public boolean isIdleNow() {
boolean idle = isIdle();
if (idle && mCallBack!=null){
mCallBack.onTransitionToIdle();
}
return idle;
}
@Override
public void registerIdleTransitionCallback(ResourceCallback callback) {
this.mCallBack = callback;
}
public boolean isIdle() {
return mActivity != null && mCallBack != null &&
!mActivity.isNetworkOperationGoingOn();
}
}
这是产生以下异常
android.support.test.espresso.PerformException: Error performing 'single
click - At Coordinates: 539, 903 and precision: 16, 16' on view 'with id:
com.abc.rt:id/submit_login'.
at android.support.test.espresso.PerformException$Builder.
build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.
getUserFriendlyError(DefaultFailureHandler.java:80)
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.
doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.
perform(ViewInteraction.java:87)
at com.abc.rt.LoginTest.buttonTextChanged(LoginTest.java:92)
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:50)
at org.junit.internal.runners.model.ReflectiveCallable.
run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.
invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.
evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.
evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.
evaluate(RunAfters.java:27)
at android.support.test.internal.statement.UiThreadStatement.
evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.
evaluate(ActivityTestRule.java:270)
答
尝试注册您的IdlingResource
单击该按钮后:
// remove @Before
@Test
public void buttonTextChanged(){
onView(withId(R.id.edittext_user))
.perform(typeText(userName));
onView(withId(R.id.edittext_pass))
.perform(typeText(userPass));
onView(withId(R.id.submit_login))
.perform(click());
LoginIdlingResource idlingResource = new LoginIdlingResource(activityRule.getActivity());
Espresso.registerIdlingResources(idlingResource);
onView(withId(R.id.submit_login))
.check(matches(withText("Verifying...")));
Espresso.unregisterIdlingResources(idlingResource);
}
// remove @After
+0
在将文本与“正在验证...”进行匹配时,未发现发生异常。这是因为onButtonClick,登录活动正在移动到另一个活动,并且该视图在该活动中不可用。在IdlingResource未注册之前它不应该保留整个工作流程吗?或者我误解了这种情况? +1为你的时间:) – Aveek
你能提供的方法的源代码 “isNetworkOperationGoingOn()” – AndroidGuy