您还可以将这些方法添加到您的基类中 - 对于这类事情非常有用 -
/**
* Waits for a window to appear, then switches to it.
* @param regex Regex enabled. Url of the window, or title.
* @return
*/
public void waitForWindow(String regex) {
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
try {
driver.switchTo().window(window);
p = Pattern.compile(regex);
m = p.matcher(driver.getCurrentUrl());
if (m.find()) {
attempts = 0;
return switchToWindow(regex);
}
else {
// try for title
m = p.matcher(driver.getTitle());
if (m.find()) {
attempts = 0;
return switchToWindow(regex);
}
}
} catch(NoSuchWindowException e) {
if (attempts <= MAX_ATTEMPTS) {
attempts++;
try {Thread.sleep(1);}catch(Exception x) { x.printStackTrace(); }
return waitForWindow(regex);
} else {
fail("Window with url|title: " + regex + " did not appear after " + MAX_ATTEMPTS + " tries. Exiting.");
}
}
}
// when we reach this point, that means no window exists with that title..
if (attempts == MAX_ATTEMPTS) {
fail("Window with title: " + regex + " did not appear after 5 tries. Exiting.");
return this;
} else {
System.out.println("#waitForWindow() : Window doesn't exist yet. [" + regex + "] Trying again. " + attempts + "/" + MAX_ATTEMPTS);
attempts++;
return waitForWindow(regex);
}
}
/**
* Switch's to a window that is already in existance.
* @param regex Regex enabled. Url of the window, or title.
* @return
*/
public void switchToWindow(String regex) {
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
System.out.println(String.format("#switchToWindow() : title=%s ; url=%s",
driver.getTitle(),
driver.getCurrentUrl()));
p = Pattern.compile(regex);
m = p.matcher(driver.getTitle());
if (m.find()) return this;
else {
m = p.matcher(driver.getCurrentUrl());
if (m.find()) return this;
}
}
fail("Could not switch to window with title / url: " + regex);
return this;
}
/**
* Close an open window.
* <br>
* If you have opened only 1 external window, then when you call this method, the context will switch back to the window you were using before.<br>
* <br>
* If you had more than 2 windows displaying, then you will need to call {@link #switchToWindow(String)} to switch back context.
* @param regex The title of the window to close (regex enabled). You may specify <code>null</code> to close the active window. If you specify <code>null</code> then the context will switch back to the initial window.
* @return
*/
public void closeWindow(String regex) {
if (regex == null) {
driver.close();
if (driver.getWindowHandles().size() == 1)
driver.switchTo().window(driver.getWindowHandles().iterator().next());
return this;
}
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
try {
driver.switchTo().window(window);
p = Pattern.compile(regex);
m = p.matcher(driver.getTitle());
if (m.find()) {
switchToWindow(regex); // switch to the window, then close it.
driver.close();
if (windows.size() == 2) // just default back to the first window.
driver.switchTo().window(windows.iterator().next());
} else {
m = p.matcher(driver.getCurrentUrl());
if (m.find()) {
switchToWindow(regex);
driver.close();
if (windows.size() == 2) driver.switchTo().window(windows.iterator().next());
}
}
} catch(NoSuchWindowException e) {
fail("Cannot close a window that doesn't exist. ["+regex+"]");
}
}
return this;
}
最后一种方法特别有用,因为如果窗口句柄的大小在关闭之前为 2,它会自动将上下文返回到基本窗口。