我有一套测试,如果量角器无法在当前视图中找到元素,我想自动滚动到页面上的元素。
这些测试是在对 UI 进行一些更改以修复标题之前编写的。这导致很少的 e2e 测试失败。
目前,我必须访问每个失败的“it 阻止”并使用辅助函数滚动到元素。
如果无法定位元素,我希望指示量角器再次查找元素。
这可能吗?
您可以创建一个 wait(ExpectedCondition),例如在单击元素或执行任何其他操作之前等待元素超时。
等待 waitUntilPresenceOfElement(element(by.id('some id')));
export async function waitUntilPresenceOfElement(element: ElementFinder, timeout: number = 5000): Promise<any> {
return await browser.wait(ExpectedConditions.presenceOf(element),
timeout,
'Waiting for element ' + element.locator() + 'to be PRSENCE with timeout ' + timeout + 'ms'
); }
如果找不到超时定义的元素,则说明有问题。
第二种解决方案是使用异常句柄并创建一些重试函数。
export async function clearText(textBox: ElementFinder) {
try {
await textBox.clear().then(() => {
console.log('Text field has been cleared.');
});
} catch (error) {
// example for no such element exception
if (error instanceof NoSuchElementError) {
// retry function
} else {
console.log('Error wile trying to clear text from element.');
throw error;
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句