Error: Error: No views in hierarchy found matching: (with tag value: is "email" and view has effective visibility=VISIBLE)

Error: Error: No views in hierarchy found matching: (with tag value: is "email" and view has effective visibility=VISIBLE)

您好,我正在对 React Native android 0.57 版进行排毒测试。我有两个屏幕启动屏幕和登录屏幕。我能够进入登录屏幕,但在登录屏幕中测试元素时出现此错误。代码是 `

describe.only('test2', () => {
    before(async () => {
      await device.reloadReactNative();
    });

    it('should have welcome screen', async () => {
      await expect(element(by.text('Welcome'))).toBeVisible();
    });

    it('should Click button', async () => {
      await expect(element(by.id('login'))).toBeVisible();
      await element(by.id('login')).tap();
    });

    it('should select email', async () => {
      await expect(element(by.id('email'))).toBeVisible();
    });

  })

`

最后一次执行时出现此错误。帮我解决这个问题。

渲染函数是

render() {
return (
    <View style={styles.mainContainer} testID='email'>
        {this.renderTopLogoContainer()}
        {this.renderBottomContainer()}
        <View style={{ height: 30, justifyContent: 'center' }}>
                {this.state.useMobile ? <Text style={{ color: colors.SECONDARY_FONT_COLOR, alignSelf: 'center', fontSize: 13, }} onPress={() => { this.setState({ useMobile: false, wrongEntryMesaage: '', userName: "" }) }}>{I18n.t("Use email")}</Text> : <Text style={{ color: colors.SECONDARY_FONT_COLOR, alignSelf: 'center', fontSize: 13 }} onPress={() => { this.setState({ useMobile: true, wrongEntryMesaage: '', userName: "" }) }}>{I18n.t("Use mobile")}</Text>}
        </View>
        <Button
            rounded title={I18n.t("GET OTP")}
            buttonStyle={styles.button}
            disabled={
                this.state.useMobile ? (this.state.userName.length === 10) && (this.props.processingRequest) || !(this.state.userName.length === 10) && !(this.props.processingRequest)
                    : (this.state.userName.length > 0) && (this.props.processingRequest) || !(this.state.userName.length > 0) && !(this.props.processingRequest)}
            onPress={this.onGetOTPForUserNamePressedDebounced} 
        />

        {this.props.getOtpFailed ? Snackbar.show({
            title: this.props.error.display_message,
            duration: Snackbar.LENGTH_LONG
        }) : null}
    </View>
);

}

检查 testID 的视图层次结构

因此,当尝试调试未显示的 testID 时,您应该检查视图层次结构,这在 iOS 中最容易完成。可以在此处找到操作说明 https://github.com/wix/Detox/blob/master/docs/Troubleshooting.RunningTests.md#debug-view-hierarchy

尝试超时

如果它在视图层次结构中,那么 Detox 可能会在实际显示之前等待 testID。您可以编辑上次测试,以便它使用 waitFor 和超时。

https://github.com/wix/Detox/blob/master/docs/Troubleshooting.RunningTests.md#test-tries-to-find-my-component-before-its-created

这样的事情可能会奏效。虽然您可能需要调整超时时间 await waitFor(element(by.text('email'))).toBeVisible().withTimeout(2000);

观看你的测试

您还应该观察您的测试并确保它按预期执行。它是从一个屏幕导航到另一个屏幕吗?你能看到被点击的按钮吗?

也许即使点击按钮导航也没有发生。