只有在吐司被藏起来之后才能完成测试吗?

时间:2019-05-02 12:38:57

标签: android android-espresso ui-testing

这是我的意式浓缩咖啡的测试:

@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()))
    }

测试buttonStart_click_serverError_showToast成功通过。当我收到网络服务器错误消息时,它(测试)检查显示为Toast。很好。

但是问题是在吐司被隐藏之前测试已经完成。 但是我只需要在buttonStart_click_serverError_showToast吐司被隐藏之后才开始另一个测试。 我该怎么做?

这是我的解决方法:

@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
    }
}

我使用3500密耳的睡眠

1 个答案:

答案 0 :(得分:1)

在测试结束时添加一个sleep命令,以便浏览器有时间在完成测试之前处理hide()。三秒钟应该起作用。

 @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)
}