我正在React-Native项目上运行排毒,并且只能测试启动屏幕。初始屏幕转到登录屏幕,但排毒代码不允许我测试此元素。
测试代码:
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"
}
]
运行不测试登录组件时,第一个测试通过
答案 0 :(得分:0)
需要时间项目才能在屏幕上呈现。您可以使用排毒提供的waitFor
属性。
在大多数情况下,测试应与应用程序自动同步。当同步不起作用时,您可以使用waitFor进行故障保护。
您可以在documentation中阅读有关使用waitFor
的更多信息。
注意:每个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()
});