2017-08-29 96 views
0

我試圖在按下按鈕時嘗試登錄時正在更改按鈕的文本。登錄按鈕文本實際上是將文本更改爲正在驗證...當系統正在檢查服務器的憑據,並且一旦完成,文本更改爲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) 
+0

你能提供的方法的源代碼 「isNetworkOperationGoingOn()」 – AndroidGuy

回答

1

嘗試註冊您的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