• 转:【WebDriver】封装GET方法来解决页面跳转不稳定的问题


    在大多数测试环境中,网络或者测试服务器主机之间并不是永远不出问题的,很多时候一个客户端的一个跳转的请求会因为不稳定的网络或者偶发的其它异常hang死在那里半天不动,直到人工干预动作的出现。
          而WebDriver测试执行时,偶然也会因此发生页面跳转或者加载的超时异常,而使得流程性的测试中断,给测试完整性和有效性带来很大的损失。其实这种问题很好解决,只需要重写或者封装一下GET方法来实现跳转就行了。
          在做这个封装之前,我们得事先讲一下driver.get(url)和driver.navigate().to(url)之间的差别:

    • driver.navigate().to(url):跳转到指定的url,只执行跳转动作,不判断、不等待指定的页面是否加载成功;
    • driver.get(url):跳转到指定的url,并且检查页面是否加载完毕,如果指定了pageLoadTimeout,而在指定时间内没有加载完毕则会抛出org.openqa.selenium.TimeoutException;

          这样,我们就可以很轻易的解决跳转不稳定的问题了:

      1. /** 
      2.  * rewrite the get method, adding user defined log</BR> 
      3.  * 地址跳转方法,使用WebDriver原生get方法,加入失败重试的次数定义。 
      4.  *  
      5.  * @param url the url you want to open. 
      6.  * @param actionCount retry times when load timeout occuers. 
      7.  * @throws RuntimeException 
      8.  */  
      9. protected void get(String url, int actionCount) {  
      10.     boolean inited = false;  
      11.     int index = 0, timeout = 10;  
      12.     while (!inited && index < actionCount){  
      13.         timeout = (index == actionCount - 1) ? maxLoadTime : 10;//最后一次跳转使用最大的默认超时时间  
      14.         inited = navigateAndLoad(url, timeout);  
      15.         index ++;  
      16.     }  
      17.     if (!inited && index == actionCount){//最终跳转失败则抛出运行时异常,退出运行  
      18.         throw new RuntimeException("can not get the url [" + url + "] after retry " + actionCount + "times!");  
      19.     }  
      20. }  
      21.   
      22. /** 
      23.  * rewrite the get method, adding user defined log</BR> 
      24.  * 地址跳转方法,使用WebDriver原生get方法,默认加载超重试【1】次。 
      25.  *  
      26.  * @param url the url you want to open. 
      27.  * @throws RuntimeException 
      28.  */  
      29. protected void get(String url) {  
      30.     get(url, 2);  
      31. }  
      32.   
      33. /** 
      34.  * judge if the url has navigate and page load completed.</BR> 
      35.  * 跳转到指定的URL并且返回是否跳转完整的结果。 
      36.  *  
      37.  * @param url the url you want to open. 
      38.  * @param timeout the timeout for page load in seconds. 
      39.  * @return if page load completed. 
      40.  */  
      41. private boolean navigateAndLoad(String url, int timeout){  
      42.     try {  
      43.         driver.manage().timeouts().pageLoadTimeout(timeout, TimeUnit.SECONDS);  
      44.         driver.get(url);  
      45.         return true;//跳转并且加载页面成功在返回true  
      46.     } catch (TimeoutException e) {  
      47.         return false;//超时的情况下返回false  
      48.     } catch (Exception e) {  
      49.         failValidation();//共用的异常处理方法  
      50.         LOG.error(e);//记录错误日志  
      51.         throw new RuntimeException(e);//抛出运行时异常,退出运行  
      52.     }finally{  
      53.         driver.manage().timeouts().pageLoadTimeout(maxLoadTime, TimeUnit.SECONDS);  
      54.     }  
      55. }  
  • 相关阅读:
    [UE4]roll pitch yaw
    [UE4]用向量表示方向
    [UE4]关闭自动曝光
    Mysql数据库常用分库和分表方式
    mycat分库分表 mod-long
    windows安装mycat(转)
    Mycat读写分离、主从切换学习(转)
    Windows版Mycat结合mysql安装配置+水平切分(转载)
    数据库高性能读写分离集群操作说明
    spring MVC、mybatis配置读写分离,ReplicationDriver(转载)
  • 原文地址:https://www.cnblogs.com/lci05/p/3807432.html
Copyright © 2020-2023  润新知