如果通过调用browser测试对象的close方法,则只能关闭一个浏览器窗口:
browser_htmlBrowser().close();
要想关闭多个IE浏览器窗口,可以通过杀死IE进程的方式:
try {
Runtime.getRuntime().exec("cmd /c tskill iexplore");
} catch (IOException e) {
System.out.println(e.toString());
}
也可以通过查找窗口名的方式来找到每个IE窗口并关闭:
IWindow[] windows = getTopWindowsWithCaptionPattern(".*Microsoft Internet Explorer",true);
for(int i=0;i<windows.length;i++)
{
windows[i].close();
}
protected IWindow[] getTopWindowsWithCaptionPattern(String pattern, boolean ignoreCase)
{
Regex regex = ( ignoreCase ? new Regex(pattern, Regex.MATCH_CASEINDEPENDENT) :
new Regex(pattern) );
java.util.Vector matches = new java.util.Vector(10);
IWindow[] topWindows = getTopWindows();
int length = ( topWindows != null ? topWindows.length : 0 );
for ( int i = 0; i < length; ++i )
{
String text = topWindows[i].getText();
if ( regex.matches(text) )
matches.add(topWindows[i]);
}
int resultLength = matches.size();
IWindow[] result = ( resultLength != 0 ?
new IWindow[resultLength] :
null );
for ( int i = 0; i < resultLength; ++i )
result[i] = (IWindow)matches.elementAt(i);
return result;
}
还可以通过查找Domain对象的方式找到所有Html.HtmlBrowser类型的对象,然后调用其close方法:
for (int i = 0; i < domains.length; ++i) {
if (domains[i].getName().equals("Html")) {
//We found an Html domain.
TestObject[] topObjects = domains[i].getTopObjects();
if (topObjects != null) {
try {
for (int j = 0; j < topObjects.length; ++j) {
//found a browser window
if (topObjects[j]
.getProperty(".class")
.equals("Html.HtmlBrowser")) {
BrowserTestObject browser =
new BrowserTestObject(topObjects[j]);
browser.activate();
browser.close();
}
}
} catch (Exception e) {
System.out.println("Error in closing All Browsers: " + e);
}
}
}
}