在日常的 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
}
至此, WebUI 自動化功能測試腳本 第 013-通過 URL 關閉多余的已開瀏覽器窗口 順利完結,希望此文能夠給初學 Selenium 的您一份參考。
最后,非常感謝親的駐足,希望此文能對親有所幫助。熱烈歡迎親一起探討,共同進步。非常感謝! ^_^
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

