Android:浓缩咖啡:点击是阻止UI,无法进行下一步检查



我有按钮的活动。单击此按钮时,我显示特定的布局。我想写浓缩咖啡测试,检查而不是按下按钮然后显示特定的布局。这里测试:

@RunWith(AndroidJUnit4::class)
class AddTraderActivityTest {
    // The IntentsTestRule class initializes Espresso Intents before each test, terminates the host activity, and releases Espresso Intents after each test
    @get:Rule
    var addTraderActivity: IntentsTestRule<AddTraderActivity> = IntentsTestRule(AddTraderActivity::class.java)
@Test
fun allFieldsFill_buttonStart_click_progress_isDisplayed() {
    onView(withId(R.id.baseTextInputEditText))
        .perform(typeText(BASE_TEST))
    onView(withId(R.id.quoteTextInputEditText))
        .perform(typeText(QUOTE_TEST))
    onView(withId(R.id.startButton))
        .perform(click())
    // not execute while not finish click
    onView(withId(R.id.containerProgressBarLayout))
        .check(matches(isDisplayed()))
} 

问题是当调用.perform(click())然后下一个方法

onView(withId(R.id.containerProgressBarLayout))
    .check(matches(isDisplayed()))

被调用。当click()的工作尚未完成时(单击按钮启动HTTP请求(。N 秒后(请求完成时(,Espresso继续执行并评估.check(matches(isDisplayed()

但是我需要检查单击按钮时是否显示了我的特定布局。

这里实现点击按钮。当单击按钮启动 ASYNC http 请求时,通过改造 2。完成后,调用回调方法以获取结果。

 public void doClickStart(String base, String quote) {
        isHidekKeyboardLiveData.setValue(true);
        isShowProgressLiveData.setValue(true);
        TransportService.executeTraderOperation(Trader.Operation.CREATE, base.trim(), quote.trim(), new DefaultRestClientCallback<Void>() {
            @Override
            public void onSuccess(Response<Void> response) {
                isShowProgressLiveData.setValue(false);
                isForwardToTradersLiveData.setValue(true);
            }
            @Override
            public void onError(ErrorResponse errorResponse) {
                isShowProgressLiveData.setValue(false);
                String message = errorResponse.getMessage();
                messageLiveData.setValue(new SingleEvent(message));
            }
        });
    }
public static void executeTraderOperation(Trader.Operation traderOperation, String base, String quote, Callback<Void> callback) {
        TraderMonitorRestClient traderMonitorRestClient = RestClientFactory.createRestClient(TraderMonitorRestClient.class);
        String sender = BuildConfig.APPLICATION_ID + "_" + BuildConfig.VERSION_NAME;
        String key = DateUtil.getDateAsString(new Date(), "mmHHddMMyyyy");
        Call<Void> call = traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base, quote, sender, key);
        // asynchronously
        call.enqueue(callback);
    }

在我的活动中,单击完成后,我完成了当前活动:

 addTraderViewModel.getIsForwardToTradersLiveData().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean isForwardToTraders) {
                if (isForwardToTraders) {
                    setResult(RESULT_OK);
                    finish();
                }
            }
        });

以下是失败测试的结果:

$ adb shell am instrument -w -r   -e debug false -e class 'com.myproject.activity.AddTraderActivityTest#allFieldsFill_buttonStart_click_progress_isDisplayed' com.myproject.debug.test/androidx.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
at androidx.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:169)
at androidx.test.espresso.base.RootViewPicker.get(RootViewPicker.java:83)
at androidx.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:77)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.provideRootView(ViewInteractionModule_ProvideRootViewFactory.java:35)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:24)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:10)
at androidx.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:62)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:276)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:268)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

从您发布的错误代码来看,您的Activity未启动测试。使用以下代码模拟活动:

@get:Rule
var activityRule: ActivityTestRule<YourActivity> =
   ActivityTestRule(YourActivity::class.java)

或者,如果您需要将参数传递给该活动,则传递给以下内容:

@get:Rule
var activityRule: ActivityTestRule<YourActivity> =
   ActivityTestRule(YourActivity::class.java)
@Test
fun `test activity X`() {
   val intent = Intent()
   intent.putExtra("your_key", "your_value");
   activityRule.launchActivity(intent)
}

最新更新