在日常的 WebUI 自動化測試腳本編寫過程中,經常需要打開新的頁面,或者在多個打開的頁面之間進行切換,以對頁面元素進行相應的操作,以模擬用戶的行為,實現 UI 的自動化測試。在過往的時間中,經常有初學 Selenium(webdriver) 的朋友問及如何選擇窗口的問題,其實 Selenium 已經給我們提供的了相應的方法去解決這個問題。解決思路如下:
1、通過?webdriver.getWindowHandles() 獲取所有已打開窗口的信息 Set<String>
2、遍歷上述信息,并切換至對應的窗口
3、通常通過 URL 或者 title 判斷是否為期望的窗口,若是,則進行相應的后續操作;否,則舍棄,繼續遍歷(若還有未遍歷的)
4、若未找到對應的窗口,則對應的自動化測試用例腳本失敗,通過 TestNG 的 Assert.fail 方法報告失敗原因(也可以自己定義)
通過上面的解決思路可以看出,選擇窗口最常用的有兩種方法(1、URL;2、title)。此文以易迅網的使用向導(購物流程、在線支付)為實例進行現實說法。
閑話少述,話歸正傳,小二上碼。。。敬請各位小主參閱,希望能對您在日常的 WebUI 自動化腳本編寫有一定的啟發和幫助。若有不足或錯誤之處,敬請大神指正,不勝感激!

1 /** 2 * Aaron.ffp Inc. 3 * Copyright (c) 2004-2015 All Rights Reserved. 4 */ 5 package main.aaron.demo.window; 6 7 import main.aaron.sele.core.TestCase; 8 9 import org.openqa.selenium.By; 10 import org.testng.Assert; 11 import org.testng.annotations.Test; 12 import org.testng.annotations.AfterClass; 13 14 /** 15 * 16 * @author Aaron.ffp 17 * @version V1.0.0: autoSeleniumDemo main.aaron.demo.window SwitchWindow.java, 2015年6月19日 下午11:20:12 Exp $ 18 */ 19 public class SwitchWindow extends TestCase{ 20 private final String baseUrl = "http://www.yixun.com/" ; 21 22 /** 23 * switch window demo 1 : by page URL 24 * 25 * @author Aaron.ffp 26 * @version V1.0.0: autoSeleniumDemo main.aaron.demo.window SwitchWindow.java testSwitchWindow_01, 2015年6月22日 下午12:58:32 Exp $ 27 * 28 */ 29 @Test 30 public void testSwitchWindow_01(){ 31 String url = "http://st.yixun.com/help/index.htm" ; 32 33 try { 34 this .webdriver.get( this .baseUrl); 35 36 // click link text of how to shopping 37 this .webdriver.findElement(By.linkText("怎樣購物" )).click(); 38 39 // refresh the page. you can annotation this, because it's not necessary when network is good 40 // this.webdriver.navigate().refresh(); 41 42 // switch to the correct page, and return webdriver 43 this .webdriver = this .switchPageByUrl(url); 44 45 // get actual assertion text 46 String shop = this .webdriver.findElement(By.cssSelector(".mod_hd" )).getText(); 47 48 // assert 49 Assert.assertEquals(shop, "購物流程" ); 50 51 // close window if current URL is not expected, and get expected URL 52 this .getUrl(url); 53 } catch (Exception e) { 54 Assert.fail(e.getMessage()); 55 e.printStackTrace(); 56 } 57 } 58 59 /** 60 * switch window demo 1 : by page title 61 * 62 * @author Aaron.ffp 63 * @version V1.0.0: autoSeleniumDemo main.aaron.demo.window SwitchWindow.java testSwtichWindow_02, 2015-6-22 12:59:27 Exp $ 64 * 65 */ 66 @Test 67 public void testSwtichWindow_02(){ 68 String title = "在線支付 - 易迅網" ; 69 70 try { 71 this .webdriver.get( this .baseUrl); 72 73 // click link text of how to shopping 74 this .webdriver.findElement(By.linkText("在線支付" )).click(); 75 76 // refresh the page. you can annotation this, because it's not necessary when network is good 77 // this.webdriver.navigate().refresh(); 78 79 // switch to the correct page, and return webdriver 80 this .webdriver = this .switchPageByUrl(title); 81 82 // get actual assertion text 83 String webpay = this .webdriver.findElement(By.cssSelector(".mod_hd" )).getText(); 84 85 // assert 86 Assert.assertEquals(webpay, "在線支付" ); 87 88 // close window if current URL is not expected, and get expected URL 89 this .getUrl( this .webdriver.getCurrentUrl().toString()); 90 } catch (Exception e) { 91 Assert.fail(e.getMessage()); 92 e.printStackTrace(); 93 } 94 } 95 96 @AfterClass 97 public void afterClass(){ 98 this .webdriver.close(); 99 this .webdriver.quit(); 100 } 101 }
腳本運行結果如下所示:
通過頁面 URL 選擇窗口的方法,若當前已開窗口的數量小于 2 或無匹配的窗口,則均返回最后一個窗口

1 /** 2 * Switch window by page URL. Return the last if number of windows lower than 2 or not window's URL can matched. 3 * 4 * @author Aaron.ffp 5 * @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java switchPageByUrl, 2015-6-19 23:15:15 Exp $ 6 * 7 * @param url : page URL 8 * @return WebDriver 9 */ 10 public WebDriver switchPageByUrl(String url){ 11 // define variable to store current page title 12 String currentUrl = "" ; 13 14 url = "".equals(url)||(url == null ) ? "" :url; 15 16 // get all windows 17 Set<String> windows = this .webdriver.getWindowHandles(); 18 19 if (windows.size() < 2 ) { 20 return this .webdriver; 21 } 22 23 try { 24 for (String window : windows) { 25 // change window 26 this .webdriver.switchTo().window(window); 27 28 // refresh the page. you can annotation this, because it's not necessary when network is good 29 // this.webdriver.navigate().refresh(); 30 31 Thread.sleep(3000 ); 32 33 // get page url 34 currentUrl = this .webdriver.getCurrentUrl().toString(); 35 36 // verify the current page is expect or not, return this if correct 37 if (currentUrl.startsWith(url)) { 38 return this .webdriver; 39 } 40 } 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 45 return this .webdriver; 46 }
通過頁面 title 選擇窗口的方法,若當前已開窗口的數量小于 2 或無匹配的窗口,則均返回最后一個窗口

1 /** 2 * Switch window by page title. Return the last if number of windows lower than 2 or not window's title can matched. 3 * 4 * @author Aaron.ffp 5 * @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java switchPageByTitle, 2015-6-19 23:10:34 Exp $ 6 * 7 * @param title : page title 8 * 9 * @return WebDriver 10 */ 11 public WebDriver switchPageByTitle(String title){ 12 // define variable to store current page title 13 String currentTitle = "" ; 14 15 title = "".equals(title)||(title == null ) ? "" :title; 16 17 // get all windows 18 Set<String> windows = this .webdriver.getWindowHandles(); 19 20 if (windows.size() < 2 ) { 21 return this .webdriver; 22 } 23 24 try { 25 for (String window : windows) { 26 // change window 27 this .webdriver.switchTo().window(window); 28 29 Thread.sleep(3000 ); 30 31 // get page title 32 currentTitle = this .webdriver.getTitle().toString(); 33 34 // verify the current page is expect or not, return this if correct 35 if (currentTitle.equals(title)) { 36 return this .webdriver; 37 } 38 } 39 } catch (Exception e) { 40 e.printStackTrace(); 41 } 42 43 return this .webdriver; 44 }
PS:上述用例代碼中有調用 getUrl(String url) 方法,此方法的主要用途是關閉無效的網頁窗口,釋放系統資源,具體的方法源碼敬請參閱: Selenium2學習-015-WebUI自動化實戰實例-013-通過 URL 關閉多余的已開瀏覽器窗口
?
至此, WebUI 自動化功能測試腳本 第 014-Selenium 窗口選擇 順利完結,希望此文能夠給初學 Selenium 的您一份參考。
最后,非常感謝親的駐足,希望此文能對親有所幫助。熱烈歡迎親一起探討,共同進步。非常感謝! ^_^
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
