【问题标题】:Forcing action while app not idle in Espresso - Android在 Espresso 中应用程序未空闲时强制执行操作 - Android
【发布时间】:2018-03-06 22:50:44
【问题描述】:

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

我的问题正好相反。 我有一个 Espresso 默认等待的倒计时,因为 ProgressBar 会更新。在倒计时期间,我有一种“取消”按钮来停止倒计时。

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

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

【问题讨论】:

  • 不是通过EspressoViewInteractions 执行此操作,而是尝试将Runnable 发布到队列的最前面来执行您的操作?理论上它应该做你想要的。您可以尝试将Handler 与主要的Looper 一起使用(例如new Handler(Looper.getMainLooper).postAtFrontOfQueue(yourRunnable))。
  • 您找到解决方案了吗?我遇到了类似的问题,我想在我的应用程序进行 API 调用时验证我的进度条是否正确显示。但我不能导致我的应用在拨打此类电话时没有空闲。
  • 我还没有找到解决办法。我尝试了 oaskamay 的建议,方法是启动进度条,然后使用带有可运行对象的处理程序,尝试单击取消按钮,但奇怪的是它崩溃了,没有错误消息。当可运行对象只是记录一条消息时,不会发生崩溃。

标签: java android testing android-espresso


【解决方案1】:

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

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

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

androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

在我的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() {

    }

}

希望这对某人有帮助

【讨论】:

  • 您的意思是:我们不使用浓缩咖啡吗?我们会用 UIAutomator 代替它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多