問題描述
在我的測試中,我有一個階段,在按下按鈕后,應(yīng)用程序會執(zhí)行大量異步計算并向云服務(wù)發(fā)出請求,之后它會顯示某個視圖.
In my test I have a stage where after pressing a button application does a lot of asynchronous calculations and requests to the cloud service, after which it displays a certain view.
是否可以使用 Espresso 的 IdlingResource
實現(xiàn)等到某個視圖出現(xiàn)?
Is it possible to use Espresso's IdlingResource
implementation to wait until a certain view appears?
我已閱讀此處的答案,并且評論似乎表明您可以使用 IdlingResource
代替,但我不明白如何.Espresso 似乎沒有任何內(nèi)置方法來處理長操作,但必須編寫自己的等待循環(huán)感覺就像是 hack.
I've read an answers here and comments seems to suggest that you can use IdlingResource
instead, but I don't understand how. Espresso does not seem to have any built-in way to handle long operations, but having to write your own waiting loops feels like a hack.
有什么方法可以解決這個問題,或者我應(yīng)該按照鏈接線程中的答案建議做嗎?
Any way to solve this or should I just do as the answer in the linked thread suggests?
推薦答案
您的 IdlingResource 可能如下所示:
Your IdlingResource could look like this:
import android.support.test.espresso.IdlingResource;
import android.support.test.espresso.ViewFinder;
import android.support.test.espresso.ViewInteraction;
import android.view.View;
import org.hamcrest.Matcher;
import java.lang.reflect.Field;
import static android.support.test.espresso.Espresso.onView;
public class ViewShownIdlingResource implements IdlingResource {
private static final String TAG = ViewShownIdlingResource.class.getSimpleName();
private final Matcher<View> viewMatcher;
private ResourceCallback resourceCallback;
public ViewShownIdlingResource(final Matcher<View> viewMatcher) {
this.viewMatcher = viewMatcher;
}
@Override
public boolean isIdleNow() {
View view = getView(viewMatcher);
boolean idle = view == null || view.isShown();
if (idle && resourceCallback != null) {
resourceCallback.onTransitionToIdle();
}
return idle;
}
@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.resourceCallback = resourceCallback;
}
@Override
public String getName() {
return this + viewMatcher.toString();
}
private static View getView(Matcher<View> viewMatcher) {
try {
ViewInteraction viewInteraction = onView(viewMatcher);
Field finderField = viewInteraction.getClass().getDeclaredField("viewFinder");
finderField.setAccessible(true);
ViewFinder finder = (ViewFinder) finderField.get(viewInteraction);
return finder.getView();
} catch (Exception e) {
return null;
}
}
}
然后,您可以創(chuàng)建一個等待您的視圖的輔助方法:
Then, you could create a helper method waiting for your view:
public void waitViewShown(Matcher<View> matcher) {
IdlingResource idlingResource = new ViewShownIdlingResource(matcher);///
try {
IdlingRegistry.getInstance().register(idlingResource);
onView(matcher).check(matches(isDisplayed()));
} finally {
IdlingRegistry.getInstance().unregister(idlingResource);
}
}
最后,在你的測試中:
@Test
public void someTest() {
waitViewShown(withId(R.id.<some>));
//do whatever verification needed afterwards
}
您可以通過讓 IdlingResource 等待任何條件來改進此示例,而不僅僅是等待可見性條件.
You could improve this example by making IdlingResource wait for any condition, not just for the visibility one.
這篇關(guān)于是否可以使用 Espresso 的 IdlingResource 等到某個視圖出現(xiàn)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!