我正在我的 React-Native 项目上运行 detox,并且只能测试启动屏幕。启动屏幕进入登录屏幕,但排毒代码不允许我测试此元素。
测试代码:
describe('Splash', () => {
beforeEach(async () => {
await device.reloadReactNative();
});
it('should have splash screen', async () => {
await expect(element(by.id('splash'))).toBeVisible();
await expect(element(by.id('login'))).toBeVisible();
});
});
给出的错误:
● Splash › should have splash screen
Failed: [Error: Error: Cannot find UI Element.
Exception with Assertion: {
"Assertion Criteria": "assertWithMatcher:matcherForSufficientlyVisible(>=0.750000)",
"Element Matcher": "((!(kindOfClass('RCTScrollView')) && (respondsToSelector(accessibilityIdentifier) && accessibilityID('login'))) || (((kindOfClass('UIView') || respondsToSelector(accessibilityContainer)) && parentThatMatches(kindOfClass('RCTScrollView'))) && ((kindOfClass('UIView') || respondsToSelector(accessibilityContainer)) && parentThatMatches((respondsToSelector(accessibilityIdentifier) && accessibilityID('login'))))))",
"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"
}
]
当它运行时第一个测试通过而不测试登录组件
项目在屏幕上渲染需要时间。您可以使用waitFor
排毒提供的财产。
在大多数情况下,测试应自动与应用程序同步。当同步不起作用时,您可以使用 waitFor 来实现故障保护。
您可以阅读有关使用的更多信息waitFor
in the 文档 https://github.com/wix/Detox/blob/master/docs/APIRef.waitFor.md.
注意:每个 waitFor 调用都必须使用 withTimeout() 设置超时。调用 waitFor 而不设置超时将不会执行任何操作。
注意:当达到超时时,waitFor 不会抛出异常,而是继续执行下一行。为了确保您的测试按您期望的方式工作,请在以下行添加expect()。
因此,根据文档中的示例,您应该将测试更新为
it('should show login screen', async () => {
await expect(element(by.id('splash'))).toBeVisible()
await waitFor(element(by.id('login'))).toBeVisible().withTimeout(2000);
await expect(element(by.id('login'))).toBeVisible()
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)