【问题标题】:How to test a complete application flow with Detox?如何使用 Detox 测试完整的应用程序流程?
【发布时间】:2018-04-03 12:10:35
【问题描述】:

我已经开始将 detox 集成到我的 React Native 应用程序中,用于一些 e2e testing。

到目前为止效果很好,但是我遇到了一个障碍。我正在尝试测试成功登录,并且流程有效:

  1. 用户输入正确的凭据
  2. Redux 将:
    • 将用户对象存储在异步存储中
    • 向对讲机注册用户
    • 触发成功操作
    • 将用户重定向到主屏幕 - navigateToHomeScreen()
    • 使用用户凭据更新 Sentry

navigateToHomeScreen:

const navigateToHomeScreen = () => dispatch =>
  dispatch(
    NavigationActions.reset({
      index: 0,
      actions: [NavigationActions.navigate({ routeName: 'Home' })],
    })
  );

在我的Home 屏幕中,我有这个作为我的渲染函数:

<View style={styles.screen} testID="home">
  <Header
    onMenuPress={navigationMenu.toggle}
    onFilterPress={filterMenu.toggle}
  />
  <Feed {...{ navigation }} />
  <IntercomButton visible={!isActive} />
  <Search />
</View>

在我的排毒测试中,我遇到了这种情况,但失败了:

it('should trigger a login', async () => {
    const loginFormEmail = element(by.id('login-form-email'));
    const loginFormPassword = element(by.id('login-form-password'));
    const loginButton = element(by.id('login-button'));

    await expect(loginFormEmail).toBeVisible();
    await expect(loginFormPassword).toBeVisible();

    await expect(element(by.id('login-activity-spinner'))).toBeNotVisible();
    await loginFormEmail.replaceText('xxx');
    await loginFormPassword.replaceText('xxx');
    await loginButton.tap();

    await expect(element(by.id('home'))).toBeVisible();

});

我收到以下错误,提示找不到testID="home":

 Error: Cannot find UI Element.
    Exception with Assertion: {
      "Assertion Criteria" : "assertWithMatcher:matcherForSufficientlyVisible(>=0.750000)",
      "Element Matcher" : "(((respondsToSelector(accessibilityIdentifier) && accessibilityID('home')) && !(kindOfClass('RCTScrollView'))) || (kindOfClass('UIScrollView') && ((kindOfClass('UIView') || respondsToSelector(accessibilityContainer)) && ancestorThatMatches(((respondsToSelector(accessibilityIdentifier) && accessibilityID('home')) && kindOfClass('RCTScrollView'))))))",
      "Recovery Suggestion" : "Check if the element exists in the UI hierarchy printed below. If it exists, adjust the matcher so that it accurately matches element."
    }

    Error Trace: [
      {
        "Description" : "Interaction cannot continue because the desired element was not found.",
        "Error Domain" : "com.google.earlgrey.ElementInteractionErrorDomain",
        "Error Code" : "0",
        "File Name" : "GREYElementInteraction.m",
        "Function Name" : "-[GREYElementInteraction matchedElementsWithTimeout:error:]",
        "Line" : "124"
      }
    ]

【问题讨论】:

  • 这对您来说仍然是个问题还是您已经设法解决了?
  • 我无法修复它,但 E2E 测试(在旧项目上)被取消了。我仍然想知道怎么做@Andrew
  • 在您的应用中点击登录按钮后会发生什么?是进行网络调用,然后在短暂等待后登录并移至主页,还是立即移至主页?
  • 它发出一个网络请求,然后在成功后,它移动到主页@Andrew
  • 这可能是两个可能的问题:首先,testID 没有正确设置。您应该检查view hierarchy 以查看它是否存在。如果它已正确设置并且您确定它存在,则可能是时间问题并且在主页可见之前调用测试。您可以使用waitFor() 和.withTimeout() 来尝试正确捕获它。你可以看到更多的细节here@Dan

标签: detox


【解决方案1】:

我遇到了同样的问题,即我尝试测试的登录流程。我正在使用 react-navigation 并且根据文档,您可以将 testID 传递给选项卡栏,但由于某些原因,在用户登录后检测导航控件(标题,底部选项卡栏)确实很不稳定。所以我只是添加了一个 testID到一个我控制的元素,我不必担心它是否被设置或它被设置的顺序。在主屏幕上,我在主屏幕上的 View 标签上设置了一个 testID,它可以正常工作而不会超时。如果可能,尽量不要在你无法控制的东西上设置 testID。

await expect(element(by.id('homeScreenViewTag'))).toBeVisible();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多