页面元素等待问题
有遇到过自动化的Case不稳定的情况吗?有时候通过,有时候不通过。一些自动化框架为了应对这种case,甚至开发出来fail自动重run几次的功能。作为自动化框架,实现该功能是不错的,但是作为写自动化case的工程师,应该尽量写出稳定的自动化case。
对于WebUI的自动化而言,一种常见的不稳定是页面上的元素加载时间不定,特别是在Ajax,事件驱动,或者JS延迟加载等情况下,页面元素出现 的时间短的几毫秒,长的几秒钟。这对于读取页面元素就带来了一些麻烦。等得时间过短,导致fail;等得时间过长,又可能白白占用/浪费case的运行时 间。
幸运的是,WebDriver提供了一些解决方案。请参考这篇文档:
http://seleniumhq.org/docs/04_webdriver_advanced.html
可以自己封装一个smart一点的FindElement方法,示例如下:
public static WebElement smartFindElement(WebDriver driver, By locator) {
WebDriverWait wait = new WebDriverWait(driver, 5); //最多等5秒
return wait.until(presenceOfElementLocated(locator));
}
private static Function presenceOfElementLocated(final By locator) {
return new Function() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
};
}
本文探讨了WebUI自动化中页面元素加载时间不确定的问题,并提出了一种使用WebDriverWait来智能等待元素加载的方法,以此提高自动化测试的稳定性。
1万+

被折叠的 条评论
为什么被折叠?



