Trong trường hợp thử nghiệm của tôi, tôi phải ghi lại trong 1 giờ, trong robotium solo.sleep (600000) đã thực hiện công việc của tôi, nhưng trong espresso tôi đang bối rối với khái niệm IdlingResource. Tôi phải bắt đầu ghi âm và chờ đợi một thời gian (tùy thuộc vào loại thử nghiệm) 15mins, 60mins, vvEspresso cách đợi một thời gian (1 giờ)?
mã tương đương trong robotium
solo.clickOnView(solo.getView("start_record"));
solo.sleep(duration * 60 * 1000);
solo.clickOnView(solo.getView("stop_record"));
Tôi cố gắng để sử dụng nó như thế này trong espresso
@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<? extends Activity> activityClass;
static {
try {
activityClass = (Class<? extends Activity>) 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());
}
}
chạy không tải tài nguyên
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;
}
}
Ngoại lệ:
android.support.test.espresso.IdlingResourceTimeoutException: Wait for [com.adbs.recorder.IntentServiceIdlingResource] to become idle timed out
Cách tiếp cận này không hoạt động nếu tôi có nhiều hơn một phương pháp thử nghiệm và phải nhàn rỗi ở cuối mỗi phương pháp cho tự nhiên. Tôi sử dụng Espresso 2.x. –
sử dụng 'registerIdleTransitionCallback' phương pháp này là gì và cách sử dụng? –