浓咖啡如何等待一段时间(1小时)?

在我的测试案例中,我必须记录1小时,在机器人solo.sleep(600000)完成了我的工作,但在浓缩咖啡中,我对IdlingResource概念感到困惑。 我必须开始录音并等待一段时间(取决于测试types)15分钟,60分钟等。

机器人中的等效代码

solo.clickOnView(solo.getView("start_record")); solo.sleep(duration * 60 * 1000); solo.clickOnView(solo.getView("stop_record")); 

我尝试在浓缩咖啡中使用它

 @RunWith(AndroidJUnit4.class) @SmallTest public 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 activityClass; static { try { activityClass = (Class) Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } @Rule public final ActivityTestRule activityRule = new ActivityTestRule(activityClass); private IntentServiceIdlingResource idlingResource; @Before public void registerIntentServiceIdlingResource() { Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); idlingResource = new IntentServiceIdlingResource(instrumentation.getTargetContext()); Espresso.registerIdlingResources(idlingResource); } @After public void unregisterIntentServiceIdlingResource() { Espresso.unregisterIdlingResources(idlingResource); } @Test public 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; } @Override public String getName() { return IntentServiceIdlingResource.class.getName(); } @Override public boolean isIdleNow() { return getTimer(); } @Override public 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()isIdleNow() ,只有在经过了特定的时间后才返回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; } } 

在测试中创建并注册此空闲资源:

 @Test public 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); } 

您需要setMasterPolicyTimeoutsetIdlingResourceTimeout调用,以确保Espresso不会因超时而终止测试。

完整示例: https : //github.com/chiuki/espresso-samples/tree/master/idling-resource-elapsed-time

Espresso等待所有已注册资源变为空闲的默认超时是一分钟。

您可以使用IdlingPolicies类更改此设置以设置显式超时:

 IdlingPolicies.setIdlingResourceTimeout(1, TimeUnit.HOURS); 
 @Before public void registerIdlingResource() { IdlingPolicies.setMasterPolicyTimeout(60 * 1000 * 3, TimeUnit.MILLISECONDS); IdlingPolicies.setIdlingResourceTimeout(60 * 1000 * 3, TimeUnit.MILLISECONDS); mIdlingResource = BooleanIdlingResource.getIdlingResource(); // To prove that the test fails, omit this call: IdlingRegistry.getInstance().register(mIdlingResource); } 

我测试我的项目,它的工作原理。 只需在注册空闲资源之前进行设置 请检查:

https://github.com/googlesamples/android-testing/tree/master/ui/espresso/IdlingResourceSample和

https://developer.android.com/reference/android/support/test/espresso/IdlingPolicies