在大多数测试环境中,网络或者测试服务器主机之间并不是永远不出问题的,很多时候一个客户端的一个跳转的请求会因为不稳定的网络或者偶发的其它异常hang死在那里半天不动,直到人工干预动作的出现。
而WebDriver测试执行时,偶然也会因此发生页面跳转或者加载的超时异常,而使得流程性的测试中断,给测试完整性和有效性带来很大的损失。其实这种问题很好解决,只需要重写或者封装一下GET方法来实现跳转就行了。
在做这个封装之前,我们得事先讲一下driver.get(url)和driver.navigate().to(url)之间的差别:
- driver.navigate().to(url):跳转到指定的url,只执行跳转动作,不判断、不等待指定的页面是否加载成功;
- driver.get(url):跳转到指定的url,并且检查页面是否加载完毕,如果指定了pageLoadTimeout,而在指定时间内没有加载完毕则会抛出org.openqa.selenium.TimeoutException;
这样,我们就可以很轻易的解决跳转不稳定的问题了:
- protected void get(String url, int actionCount) {
- boolean inited = false;
- int index = 0, timeout = 10;
- while (!inited && index < actionCount){
- timeout = (index == actionCount - 1) ? maxLoadTime : 10;
- inited = navigateAndLoad(url, timeout);
- index ++;
- }
- if (!inited && index == actionCount){
- throw new RuntimeException("can not get the url [" + url + "] after retry " + actionCount + "times!");
- }
- }
-
- protected void get(String url) {
- get(url, 2);
- }
-
- private boolean navigateAndLoad(String url, int timeout){
- try {
- driver.manage().timeouts().pageLoadTimeout(timeout, TimeUnit.SECONDS);
- driver.get(url);
- return true;
- } catch (TimeoutException e) {
- return false;
- } catch (Exception e) {
- failValidation();
- LOG.error(e);
- throw new RuntimeException(e);
- }finally{
- driver.manage().timeouts().pageLoadTimeout(maxLoadTime, TimeUnit.SECONDS);
- }
- }
转:【WebDriver】封装GET方法来解决页面跳转不稳定的问题,布布扣,bubuko.com
转:【WebDriver】封装GET方法来解决页面跳转不稳定的问题
原文:http://www.cnblogs.com/lci05/p/3807432.html