使用espresso测试登录会抛出异常 [英] Testing Login with espresso is throwing exception

查看:68
本文介绍了使用espresso测试登录会抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试在按下按钮时尝试登录时按钮的文本是否更改.当系统正在使用服务器检查凭据时,登录按钮文本实际上是将文本更改为 Verification ... .一旦完成,该文本将更改为 LOGIN .每当我尝试使用espresso进行测试时,UI部分便完成了为edittext分配值并单击按钮的操作,然后线程冻结,并在数次后引发错误.由于我是测试的新手,如果您能解释一下如何解决此问题或者在这种情况下应该采取的方法,将不胜感激.

I am trying to test the text of the button is changing while trying to login when the button is pressed. Login button text is actually changing the text to Verifying... when the system is checking the credentials with server and once done, the text changes to LOGIN again. Whenever I am trying to test that with espresso;the UI part is completing assigning values to the edittext and clicked the button, the thread then freezes and few times later it throws an error. As I am new in testing, I would be grateful if you could explain how this problem can be resolved or what is the approach that I should take for this kind of scenarios.

这是我的考试课程.

@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.

I have prepared an 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

这篇关于使用espresso测试登录会抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆