1

我已经开始将detox一些 e2e 集成到我的 React Native 应用程序中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"
      }
    ]
4

1 回答 1

0

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

await expect(element(by.id('homeScreenViewTag'))).toBeVisible();
于 2020-11-19T14:21:52.690 回答