问题描述
我正在使用Espresso 2.2编写仪器测试.
I am writing Instrumentation tests with usage of Espresso 2.2.
我要测试的流程:
- 测试点击了radioButton
- onClick启动对API的请求
- 每隔一段时间我收到回复
- 积极响应触发活动中调用的接口方法
- onRequestSuccess,我在屏幕上显示了名为vSetupAmount的附加面板
我想在单击radioButton之后注册IdleResource,以便它等待直到vSetupAmount可见.但是我不能使它工作.请告诉我我在做什么错.
I want to register IdleResource after click on radioButton so it waits until vSetupAmount becomes visible. But I can't make it work. Please tell me what am I doing wrong.
我已经写过这样的IdleResource:
I have written such IdleResource:
public class AmountViewIdlingResource implements IdlingResource {
private CountriesAndRatesActivity activity;
private ResourceCallback callback;
private SetupAmountView amountView;
public AmountViewIdlingResource(CountriesAndRatesActivity activity) {
this.activity = activity;
amountView = (SetupAmountView) this.activity.findViewById(R.id.vSetupAmount);
}
@Override public String getName() {
return "Amount View idling resource";
}
@Override public boolean isIdleNow() {
callback.onTransitionToIdle();
return amountView.getVisibility() == View.VISIBLE;
}
@Override public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.callback = resourceCallback;
}
}
因此,我正在将活动传递给IdleResource,并使用变量链接视图.我了解,在isIdleNow()返回值true之前,IdleResource不会通过测试.因此,如果view是View.GONE,那么它将不会走得更远.
So I am passing activity to IdleResource, link view with variable. I understand that IdleResource won't let test go through until isIdleNow() returns value true. So if view is View.GONE then it won't go further.
它在测试中的外观:
// click of radioButton picked from radioGroup
onView(withId(rgDeliveries.getChildAt(id).getId())).perform(scrollTo(), click());
// wait for view to become visible
Espresso.registerIdlingResources(new AmountViewIdlingResource(getActivity()));
// go to button on view
onView(withId(R.id.btnGetStarted)).perform(scrollTo());
// unregister idle resource
for ( IdlingResource resource : getIdlingResources()) {
Espresso.unregisterIdlingResources(resource);
}
因此,我单击了radioButton. IdleResource已成功注册,但没有任何反应.在我的设备上,API响应来了. vSetupAmount正在显示,但
So I get my click on radioButton. IdleResource is successfully registered but nothing happens. On my device API response comes. vSetupAmount is being displayed but
amountView.getVisibility() == View.VISIBLE;
一直被检查的
(但我在屏幕上看到我的视图)始终返回false.
which is being checked forever (but I see my view on screen) always returns false.
我在做什么错了?
推荐答案
我知道这可能为时已晚,但我的回答可能会对某人有所帮助.
I know this might be too late, but my answer might help someone.
- 实例化并注册您的
AmountViewIdlingResource
时,请保留对其的引用,并仅取消注册该引用,而不取消所有IdlingResources
的注册. - 您的
isIdleNow()
方法的实现只能在您的空闲资源并非每次都真正空闲的情况下才调用回调的onTransitionToIdle()
方法.
- When you instantiate and register your
AmountViewIdlingResource
, keep a reference to it and only unregister that reference, don't unregister allIdlingResources
. - Your implementation of the
isIdleNow()
method should only call the callback'sonTransitionToIdle()
method if your idling resource is really idle not every time.
我需要做类似的事情,以等待屏幕上出现一个按钮,而该按钮仅在HTTP调用完成后才可见.我对视图可见性空闲资源的实现如下:
I needed to do something similar, to wait for a button to appear on the screen, which in turn only got to be visible after an HTTP call finished. My implementation of a view visibility Idling Resource is as follows:
public class ViewVisibilityIdlingResource implements IdlingResource {
private final View mView;
private final int mExpectedVisibility;
private boolean mIdle;
private ResourceCallback mResourceCallback;
public ViewVisibilityIdlingResource(final View view, final int expectedVisibility) {
this.mView = view;
this.mExpectedVisibility = expectedVisibility;
this.mIdle = false;
this.mResourceCallback = null;
}
@Override
public final String getName() {
return ViewVisibilityIdlingResource.class.getSimpleName();
}
@Override
public final boolean isIdleNow() {
mIdle = mIdle || mView.getVisibility() == mExpectedVisibility;
if (mIdle) {
if (mResourceCallback != null) {
mResourceCallback.onTransitionToIdle();
}
}
return mIdle;
}
@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
mResourceCallback = resourceCallback;
}
}
希望这会有所帮助
这篇关于等待直到通过IdleResource看到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!