在日常的 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 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

