我在我的反应式项目上正在运行排毒,只能测试飞溅屏幕。Splash屏幕进入登录屏幕,但排毒代码不允许我测试此元素。
测试代码:
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
的更多信息。
注意:每个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()
});