机器人中的等效代码
solo.clickOnVIEw(solo.getVIEw("start_record")); solo.sleep(duration * 60 * 1000); solo.clickOnVIEw(solo.getVIEw("stop_record"));
我尝试在espresso中使用它
@RunWith(AndroIDJUnit4.class)@SmallTestpublic class AaEspressoTest {private static final String LAUNCHER_ACTIVITY_FulL_CLASSname = "com.absd.rec.RecorderActivity";private static Class<?> launcherActivityClass;private Solo solo;private static CoreRecordingTest skyroTestRunner;private static Class<? extends Activity> activityClass;static { try { activityClass = (Class<? extends Activity>) Class.forname(LAUNCHER_ACTIVITY_FulL_CLASSname); } catch (ClassNotFoundException e) { throw new RuntimeException(e); }}@Rulepublic final ActivityTestRule<?> activityRule = new ActivityTestRule<>(activityClass);private IntentServiceIDlingResource IDlingResource;@Beforepublic voID registerIntentServiceIDlingResource() { Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); IDlingResource = new IntentServiceIDlingResource(instrumentation.getTargetContext()); Espresso.registerIDlingResources(IDlingResource);}@Afterpublic voID unregisterIntentServiceIDlingResource() { Espresso.unregisterIDlingResources(IDlingResource);}@Testpublic voID testHello() throws Exception { onVIEw(withID(AaEspressoTest.getID("recorderpage_record"))).perform(click()); registerIntentServiceIDlingResource(); onVIEw(withID(AaEspressoTest.getID("recorderpage_stop"))).perform(click()); }}
空闲资源
public class IntentServiceIDlingResource implements IDlingResource {private final Context context;private ResourceCallback resourceCallback;public static boolean status = false;public IntentServiceIDlingResource(Context context) { this.context = context;}@OverrIDepublic String getname() { return IntentServiceIDlingResource.class.getname();}@OverrIDepublic boolean isIDleNow() { return getTimer();}@OverrIDepublic voID registerIDleTransitionCallback(ResourceCallback resourceCallback) { this.resourceCallback = resourceCallback;}private static boolean getTimer() { new CountDownTimer(5000,1000) { @OverrIDe public voID onTick(long millisUntilFinished) { // Do nothing status = false; } @OverrIDe public voID onFinish() { status = true; } }; return status;}}
例外:
androID.support.test.espresso.IDlingResourceTimeoutException: Wait for [com.adbs.recorder.IntentServiceIDlingResource] to become IDle timed out解决方法 您需要一个具有isIDleNow()的IDlingResource,只有在特定的时间量过去时才返回true.为了达到这个目的,保存开始时间并与当前时间进行比较:
public class elapsedtimeIDlingResource implements IDlingResource { private final long startTime; private final long waitingTime; private ResourceCallback resourceCallback; public elapsedtimeIDlingResource(long waitingTime) { this.startTime = System.currentTimeMillis(); this.waitingTime = waitingTime; } @OverrIDe public String getname() { return elapsedtimeIDlingResource.class.getname() + ":" + waitingTime; } @OverrIDe public boolean isIDleNow() { long elapsed = System.currentTimeMillis() - startTime; boolean IDle = (elapsed >= waitingTime); if (IDle) { resourceCallback.onTransitionToIDle(); } return IDle; } @OverrIDe public voID registerIDleTransitionCallback( ResourceCallback resourceCallback) { this.resourceCallback = resourceCallback; }}
在测试中创建并注册此空闲资源:
@Testpublic static voID waitForOneHour() { long waitingTime = DateUtils.HOUR_IN_MILliS; // Start onVIEw(withID(AaEspressoTest.getID("recorderpage_record"))) .perform(click()); // Make sure Espresso does not time out IDlingPolicIEs.setMasterPolicyTimeout( waitingTime * 2,TimeUnit.MILliSECONDS); IDlingPolicIEs.setIDlingResourceTimeout( waitingTime * 2,TimeUnit.MILliSECONDS); // Now we wait IDlingResource IDlingResource = new elapsedtimeIDlingResource(waitingTime); Espresso.registerIDlingResources(IDlingResource); // Stop onVIEw(withID(AaEspressoTest.getID("recorderpage_stop"))) .perform(click()); // Clean up Espresso.unregisterIDlingResources(IDlingResource);}
您需要setMasterPolicyTimeout和setIDlingResourceTimeout调用,以确保Espresso由于超时而不会终止测试.
完整例子:https://github.com/chiuki/espresso-samples/tree/master/idling-resource-elapsed-time
总结以上是内存溢出为你收集整理的android – Espresso如何等待一段时间(1小时)?全部内容,希望文章能够帮你解决android – Espresso如何等待一段时间(1小时)?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)