当应用程序在Espresso中闲置时强制执行操作 - Android

Arb*_*rBo 8 java testing android android-espresso

很多人都问过如何让Espresso框架在执行操作或断言之前等待后台任务完成.我知道在这些情况下,IdlingResource通常就是答案.

我的问题恰恰相反.我有一个Espresso正在等待的倒计时,因为ProgressBar会更新.在倒计时期间,我有一个"取消"按钮来停止倒计时.

我想写的测试将设置后台任务,并检查取消按钮是否将应用程序带回上一屏幕.现在它在尝试单击取消按钮之前等待后台任务完成,但任务完成后取消按钮消失.

即使应用程序没有闲置,我如何"强制"Espresso执行操作(单击取消)?

L4r*_*4ry 2

老实说,我认为这是不可能的。我有同样的问题。我通过使用UIAutomator单击(在您的情况下)取消按钮来修复它。

就我而言,我有一个登录按钮,之后有一个地图。该应用程序在登录按钮之后永远不会空闲,甚至只是在 MapFragment 中(Espresso 的问题),因此我必须使用 UIAutomator 作为登录按钮,并检查登录后地图是否出现。

我将其弹出到应用程序的依赖项中:

androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
Run Code Online (Sandbox Code Playgroud)

在我的LoginTest.kt文件中:

import androidx.test.uiautomator.*
//your imports here

@RunWith(AndroidJUnit4::class)
@LargeTest
class LoginTest {
    @Rule
    @JvmField
    //your rules here
    lateinit var mDevice:UiDevice


    @Before
    fun setUp() {
        //your setUp stuff
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

    }

    @Test
    fun checkLogin(){
        onView(withId(R.id.loginBtn)).perform(click())   //goes to LoginFragment

        mDevice.findObject(UiSelector().text("LOGIN")).click()   //clicks login button
        //this is where I had to use the UIAutomator because espresso would never be
        //idle after the login button

        mDevice.wait(Until.findObject(By.text("YOU HAVE LOGGED IN")),15000)
        //this waits until the object with the given text appears, max. wait time is 15 seconds
        //as espresso would still not be idle, I had to check if the login was successful 
        //with (again) the help of UIAutomator
    }

    @After
    fun tearDown() {

    }

}
Run Code Online (Sandbox Code Playgroud)

希望这对某人有帮助