在日常的 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 }