WebDriver封装GET方法来解决页面跳转不稳定的问题
来源:互联网 发布:进口软件涉及的税种 编辑:程序博客网 时间:2024/05/19 22:51
在大多数测试环境中,网络或者测试服务器主机之间并不是永远不出问题的,很多时候一个客户端的一个跳转的请求会因为不稳定的网络或者偶发的其它异常hang死在那里半天不动,直到人工干预动作的出现。而WebDriver测试执行时,偶然也会因此发生页面跳转或者加载的超时异常,而使得流程性的测试中断,给测试完整性和有效性带来很大的损失。其实这种问题很好解决,只需要重写或者封装一下GET方法来实现跳转就行了。
在做这个封装之前,我们得事先讲一下driver.get(url)和driver.navigate().to(url)之间的差别:
- driver.navigate().to(url):跳转到指定的url,只执行跳转动作,不判断、不等待指定的页面是否加载成功;
- driver.get(url):跳转到指定的url,并且检查页面是否加载完毕,如果指定了pageLoadTimeout,而在指定时间内没有加载完毕则会抛出org.openqa.selenium.TimeoutException;
- /**
- * rewrite the get method, adding user defined log</BR>
- * 地址跳转方法,使用WebDriver原生get方法,加入失败重试的次数定义。
- *
- * @param url the url you want to <span id="2_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=11&is_app=0&jk=69829344341ee6cd&k=open&k0=open&kdi0=0&luki=2&n=10&p=baidu&q=v77y4_cpr&rb=0&rs=1&seller_id=1&sid=cde61e3444938269&ssp2=1&stid=0&t=tpclicked3_hc&td=2102575&tu=u2102575&u=http%3A%2F%2Fwww%2Eylzx8%2Ecn%2Fzonghe%2Fopen%2Dsource%2F247951%2Ehtml&urlid=0" target="_blank" mpid="2" style="color: rgb(1, 70, 108); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">open</span></a></span>.
- * @param actionCount retry times when load timeout occuers.
- * @throws RuntimeException
- */
- 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!");
- }
- }
- /**
- * rewrite the get method, adding user defined log</BR>
- * 地址跳转方法,使用WebDriver原生get方法,默认加载超重试【1】次。
- *
- * @param url the url you want to open.
- * @throws RuntimeException
- */
- protected void get(String url) {
- get(url, 2);
- }
- /**
- * judge if the url has navigate and page load completed.</BR>
- * 跳转到指定的URL并且返回是否跳转完整的结果。
- *
- * @param url the url you want to open.
- * @param timeout the timeout for page load in seconds.
- * @return if page load completed.
- */
- private boolean navigateAndLoad(String url, int timeout){
- try {
- driver.manage().timeouts().pageLoadTimeout(timeout, TimeUnit.SECONDS);
- driver.get(url);
- return true;//跳转并且加载页面成功在返回true
- } catch (TimeoutException e) {
- return false;//超时的情况下返回false
- } catch (Exception e) {
- failValidation();//共用的异常处理方法
- LOG.error(e);//记录错误日志
- throw new RuntimeException(e);//抛出运行时异常,退出运行
- }finally{
- driver.manage().timeouts().pageLoadTimeout(maxLoadTime, TimeUnit.SECONDS);
- }
- }
阅读全文
0 0
- WebDriver封装GET方法来解决页面跳转不稳定的问题
- WebDriver封装GET方法来解决页面跳转不稳定的问题
- 【开源自动化测试疑难FAQ】【WebDriver】封装GET方法来解决页面跳转不稳定的问题
- 解决session不稳定的问题
- 解决同一个页面不能跳转的问题
- 解决Ubuntu 9.04无线网络的不稳定问题
- webdriver页面元素等待问题处理方法
- webdriver页面元素等待问题处理方法
- selenium Webdriver 封装方法
- 页面跳转的方法
- 页面跳转的方法
- 页面跳转的方法
- WebDriver判断页面是否刷新的方法
- 解决UINavigationController+UITabbar跳转页面隐藏tabbar的一种方法
- 页面跳转的问题
- 关于weblogic apache 的页面跳转问题(已解决)
- 解决django中超链接页面不跳转的问题
- 解决Android页面跳转时回退到桌面的问题
- Caffe从入门到精通09
- SQL的经验累积
- git学习,常用命令
- 直播 | 第二届中国数据标准化及治理大会
- 560万Facebook人际关系数据,揭秘家庭职业传承“真相”
- WebDriver封装GET方法来解决页面跳转不稳定的问题
- map vs forEach
- 网络边缘将驱动本地数据中心需求
- sublime text3快捷键
- ACCP 全部电子版教材(S1-Y2)下载地址
- 选择排序
- 百度地图开发-----添加多个行政区域
- 这个行业一半人月薪超过8千!
- 开通CSDN