在日常的 WebUI 自动化测试脚本执行的过程中,经常会打开不同的网页,进行相应的操作,此时可能会打开很多的网页,当打开的网页过多时,无效的网页资源对运行脚本的机器造成了过多无效的资源浪费,因而在日常的网页自动化测试脚本运行的过程中要关闭过多冗余的页面,降低系统无效损耗。

此文中所述方法通过 URL 对已开窗口进行匹配,将不匹配的窗口页面关闭,一定程度减少了系统损耗,有兴趣的小主们,可进一步优化相应的程序源码,或在日常脚本编写调用此方法的过程中尽量使参数 URL 足够精确,可最大限度的减少系统无效损耗。

多不闲聊,小二上码。。。敬请各位小主参阅,希望能对您在日常的 WebUI 自动化脚本编写有一定的启发和帮助。若有不足或错误之处,敬请大神指正,不胜感激!

 1     /**
 2      * close the window if current URL is not expected, and get expected URL
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java getUrl, 2015-6-22 12:22:30 Exp $
 6      * 
 7      * @param url : expected URL
 8      * @return WebDriver
 9      */
10     public WebDriver getUrl(String url){
11         // define variable to store current page title
12         String currentUrl = "";
13         
14         // get all windows
15         Set<String> windows = this.webdriver.getWindowHandles();
16         
17         if (windows.size() < 1) {
18             return this.webdriver;
19         }
20         
21         try {
22             for (String window : windows) {
23                 // change window
24                 this.webdriver.switchTo().window(window);
25                 
26                 // refresh the page. you can annotation this, because it's not necessary when network is good
27 //                this.webdriver.navigate().refresh();
28                 
29                 Thread.sleep(3000);
30                 
31                 // get page url
32                 currentUrl = this.webdriver.getCurrentUrl().toString();
33                 
34                 // verify the current page is expect or not, return this if correct
35                 if (!currentUrl.startsWith(url)) {
36                     this.webdriver.close();
37                 }
38             }
39         } catch (Exception e) {
40             e.printStackTrace();
41         } finally {
42             this.webdriver.get(url);
43         }
44 
45         return this.webdriver;
46     }
通过页面 URL 关闭不匹配的窗口,释放系统资源方法源代码

相关文章:

  • 2021-12-18
  • 2022-01-01
  • 2021-07-30
  • 2021-08-30
  • 2021-08-23
  • 2022-02-25
  • 2021-10-27
  • 2022-01-06
猜你喜欢
  • 2021-09-04
  • 2021-09-14
  • 2021-11-03
  • 2021-09-13
  • 2021-12-11
  • 2021-09-27
  • 2021-12-07
相关资源
相似解决方案