排毒:使用停止按钮测试React-Native微调器

时间:2018-07-19 10:22:05

标签: react-native detox

想知道是否有人遇到过类似的问题。

在我正在使用的应用程序中,我们有一个微调器,用于显示下载内容,中间带有一个停止按钮。当用户点击微调器/停止按钮时,下载将被取消。作为参考,在iOS上旋转/停止按钮如下所示:

spinner with stop button

我正在尝试使用Detox为此功能编写e2e测试。使用自动同步无法正常工作,因为动画(和下载)使线程保持运行状态。我尝试使用device.disableSynchronization(),但没有成功。

这是我的e2e测试以供参考:

it('should start and then cancel a download from the <My Learning> screen', async() => {
  // setup
  await device.reloadReactNative()
  await expect(element(by.id('feed_screen'))).toBeVisible()
  await element(by.id('LearningPlan_Tab')).tap()
  await expect(element(by.id('learning-plan-screen'))).toBeVisible()
  
  // Tap the download icon, it will turn into a spinner
  await element(by.id('offline_download_c2a')).atIndex(1).tap()
  
  // Alert box appears with Cancel/Download options
  await expect(element(by.label('Download')).atIndex(1)).toBeVisible()
  await element(by.label('Download')).atIndex(1).tap()
  
  // Ideally this would work, but it doesn't (the above alert box doesn't dismiss properly)
  await device.disableSynchronization()
  await waitFor(element(by.id('download_spinner_c2a')).atIndex(0)).toBeVisible().withTimeout(5000)
  await element(by.id('download_spinner_c2a')).atIndex(0).tap()
  await device.enableSynchronization()
  await element(by.label('Cancel download')).tap()
  await expect(element(by.id('offline_download_c2a')).atIndex(1)).toBeVisible()
})

运行此测试时,该应用似乎仍在等待下载完成。有没有人知道最好的测试方法的建议,或者甚至有可能?

1 个答案:

答案 0 :(得分:0)

我已经找到一种使它工作的方法,尽管它看起来很不整洁:

  it('should start and then cancel a download from the <My Learning> screen', async () => {
    //...
    await device.disableSynchronization()
    // This timeout seems to help
    await     waitFor(element(by.id('download_spinner_c2a')).atIndex(0)).toBeVisible().withTimeout(10000) 
    await element(by.id('download_spinner_c2a')).atIndex(0).tap()
    await device.enableSynchronization()
    await element(by.label('Cancel download')).tap()
    await expect(element(by.id('offline_download_c2a')).atIndex(1)).toBeVisible()
  })