Is it possilble to finish test only after Toast was hide?

Issue

Here my Espresso’s test:

@RunWith(AndroidJUnit4::class)
class AddTraderActivityNetworkTest {
    @Rule
    @JvmField
    var addTraderIntentTestRule: IntentsTestRule<AddTraderActivity>  IntentsTestRule(AddTraderActivity::class.java)

    @Before
    fun setup() {
        mockServer  MockWebServer()
        mockServer.start(8081)
    }

 @Test
    fun buttonStart_click_serverError_showToast() {
        mockServer.enqueue(MockResponse()
                .setResponseCode(500))

        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())
        onView(withText(R.string.server_error)).inRoot(ToastMatcher())
                .check(matches(isDisplayed()))
    }

Test buttonStart_click_serverError_showToast success pass. When I get network server error it (test) check is show Toast. Nice.

But the problem is that test is finish BEFORE Toast was hide. But I need to start another test only AFTER toast of buttonStart_click_serverError_showToast was hide. How I can do this?

Here my solution:

@RunWith(AndroidJUnit4::class)
class AddTraderActivityNetworkTest {
    private val context  InstrumentationRegistry.getInstrumentation().getContext()
    private lateinit var mockServer: MockWebServer

    private val TAG  AddTraderActivityNetworkTest::class.java.name

    @Rule
    @JvmField
    var addTraderIntentTestRule: IntentsTestRule<AddTraderActivity>  IntentsTestRule(AddTraderActivity::class.java)

    @Before
    fun setup() {
        mockServer  MockWebServer()
        mockServer.start(8081)
     }

     @Test
    fun buttonStart_click_clientError_showToast() {
        //stub response
        mockServer.enqueue(MockResponse()
                .setResponseCode(400))

        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())
        onView(withText(R.string.client_error)).inRoot(ToastMatcher())
                .check(matches(isDisplayed()))
         Thread.sleep(ToastUtil.LONG_DELAY_MILLS)
    }     


class ToastUtil {
    companion object {
        const val LONG_DELAY_MILLS 3500L
        const val SHORT_DELAY_MILLS  2000L
    }
}

I use sleep of 3500 Mills

Solution

Add a sleep command at the end of your test so that the browser has time to process the hide() before finishing the test. Three seconds should work.

 @Test
    fun buttonStart_click_serverError_showToast() {
        Thread.sleep(ToastUtil.LONG_DELAY)
        // stub response
        mockServer.enqueue(MockResponse()
                .setResponseCode(500))
    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())
    onView(withText(R.string.server_error)).inRoot(ToastMatcher())
            .check(matches(isDisplayed()))
Thread.sleep(3000)
}

Answered By – user2743227

Leave a Comment