Skip to content

Commit 232d318

Browse files
committed
more on window handling - support WebClient#reset()
1 parent 53f3764 commit 232d318

4 files changed

Lines changed: 124 additions & 9 deletions

File tree

src/main/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,12 @@ private HtmlUnitDriver(final BrowserVersion version, final boolean enableJavascr
276276
webClient_.addWebWindowListener(new WebWindowListener() {
277277
@Override
278278
public void webWindowOpened(final WebWindowEvent webWindowEvent) {
279-
// Ignore
279+
if (webWindowEvent.getWebWindow() instanceof TopLevelWindow) {
280+
// use the first top level window we are getting aware of
281+
if (currentWindow_ == null && webClient_.getTopLevelWindows().size() == 1) {
282+
currentWindow_ = new HtmlUnitWindow(webClient_.getTopLevelWindows().get(0));
283+
}
284+
}
280285
}
281286

282287
@Override
@@ -293,18 +298,26 @@ public void webWindowContentChanged(final WebWindowEvent event) {
293298
@Override
294299
public void webWindowClosed(final WebWindowEvent event) {
295300
elementsMap_.remove(event.getOldPage());
301+
302+
// the last window is gone
303+
if (getWebClient().getTopLevelWindows().size() == 0) {
304+
currentWindow_ = null;
305+
return;
306+
}
307+
296308
// Check if the event window refers to us or one of our parent windows
297309
// setup the currentWindow appropriately if necessary
298-
WebWindow curr = currentWindow_.getWebWindow();
310+
WebWindow ourCurrentWindow = currentWindow_.getWebWindow();
311+
final WebWindow ourCurrentTopWindow = currentWindow_.getWebWindow().getTopWindow();
299312
do {
300313
// Instance equality is okay in this case
301-
if (curr == event.getWebWindow()) {
302-
setCurrentWindow(currentWindow_.getWebWindow().getTopWindow());
314+
if (ourCurrentWindow == event.getWebWindow()) {
315+
setCurrentWindow(ourCurrentTopWindow);
303316
return;
304317
}
305-
curr = curr.getParentWindow();
318+
ourCurrentWindow = ourCurrentWindow.getParentWindow();
306319
}
307-
while (curr != currentWindow_.getWebWindow().getTopWindow());
320+
while (ourCurrentWindow != ourCurrentTopWindow);
308321
}
309322
});
310323

@@ -638,8 +651,7 @@ protected void get(final URL fullUrl) {
638651
getAlert().setAutoAccept(false);
639652
try {
640653
// we can't use webClient.getPage(url) here because selenium has a different
641-
// idea
642-
// of the current window and we like to load into to selenium current one
654+
// idea of the current window and we like to load into to selenium current one
643655
final BrowserVersion browser = getBrowserVersion();
644656
final WebRequest request = new WebRequest(fullUrl, browser.getHtmlAcceptHeader(),
645657
browser.getAcceptEncodingHeader());
@@ -664,6 +676,9 @@ protected void get(final URL fullUrl) {
664676
catch (final NoSuchSessionException e) {
665677
throw e;
666678
}
679+
catch (final NoSuchWindowException e) {
680+
throw e;
681+
}
667682
catch (final SSLHandshakeException e) {
668683
return;
669684
}
@@ -1205,7 +1220,10 @@ public WebClient getWebClient() {
12051220
}
12061221

12071222
public HtmlUnitWindow getCurrentWindow() {
1208-
if (currentWindow_ == null || currentWindow_.getWebWindow().isClosed()) {
1223+
if (webClient_ == null || currentWindow_ == null) {
1224+
throw new NoSuchSessionException("Session is closed");
1225+
}
1226+
if (currentWindow_.getWebWindow().isClosed()) {
12091227
throw new NoSuchWindowException("Window is closed");
12101228
}
12111229
return currentWindow_;

src/test/java/org/openqa/selenium/htmlunit/HtmlUnitDriverTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,4 +517,14 @@ protected WebClient modifyWebClient(final WebClient client) {
517517
}
518518
};
519519
}
520+
521+
@Test
522+
public void resetWebClient() {
523+
final HtmlUnitDriver webDriver = new HtmlUnitDriver();
524+
525+
webDriver.get("https://www.htmlunit.org");
526+
webDriver.getWebClient().reset();
527+
528+
webDriver.get("https://www.htmlunit.org");
529+
}
520530
}

src/test/java/org/openqa/selenium/htmlunit/WebDriverTestCase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,9 @@ public void releaseResources() {
15751575
catch (final NoSuchWindowException e) {
15761576
// ignore
15771577
}
1578+
catch (final NoSuchSessionException e) {
1579+
// ignore
1580+
}
15781581
catch (final UnhandledAlertException e) {
15791582
ex = e;
15801583
unhandledAlerts.add(e.getMessage());

src/test/java/org/openqa/selenium/htmlunit/html/WindowsTest.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
import java.util.HashSet;
2121
import java.util.Set;
2222

23+
import org.junit.Assert;
2324
import org.junit.Test;
2425
import org.junit.runner.RunWith;
2526
import org.openqa.selenium.By;
2627
import org.openqa.selenium.Keys;
28+
import org.openqa.selenium.NoSuchSessionException;
29+
import org.openqa.selenium.NoSuchWindowException;
2730
import org.openqa.selenium.WebDriver;
2831
import org.openqa.selenium.WebElement;
2932
import org.openqa.selenium.WindowType;
@@ -260,4 +263,85 @@ public void switchToNewTab() throws Exception {
260263
assertEquals("", driver.getTitle());
261264
assertEquals("about:blank", driver.getCurrentUrl());
262265
}
266+
267+
/**
268+
* @throws Exception if something goes wrong
269+
*/
270+
@Test
271+
public void closeCurrentWindow() throws Exception {
272+
final String htmlFirst =
273+
"<html>\n"
274+
+ "<head><title>First</title></head>\n"
275+
+ "<body>\n"
276+
+ "</body></html>\n";
277+
278+
final WebDriver driver = loadPage2(htmlFirst);
279+
280+
assertEquals("First", driver.getTitle());
281+
282+
Set<String> windowHandles = driver.getWindowHandles();
283+
assertEquals(1, windowHandles.size());
284+
285+
driver.switchTo().newWindow(WindowType.TAB);
286+
287+
windowHandles = new HashSet<>(driver.getWindowHandles());
288+
assertEquals(2, windowHandles.size());
289+
290+
assertEquals("", driver.getTitle());
291+
assertEquals("about:blank", driver.getCurrentUrl());
292+
293+
final String htmlSecond =
294+
"<html>\n"
295+
+ "<head><title>Second</title></head>\n"
296+
+ "<body>\n"
297+
+ "</body></html>\n";
298+
loadPage2(htmlSecond);
299+
300+
windowHandles = new HashSet<>(driver.getWindowHandles());
301+
assertEquals(2, windowHandles.size());
302+
303+
assertEquals("Second", driver.getTitle());
304+
305+
driver.close();
306+
307+
windowHandles = new HashSet<>(driver.getWindowHandles());
308+
assertEquals(1, windowHandles.size());
309+
310+
try {
311+
driver.getTitle();
312+
Assert.fail("NoSuchWindowException expected");
313+
}
314+
catch (final NoSuchWindowException e) {
315+
// expected
316+
}
317+
}
318+
319+
/**
320+
* @throws Exception if something goes wrong
321+
*/
322+
@Test
323+
public void closeLastWindow() throws Exception {
324+
final String htmlFirst =
325+
"<html>\n"
326+
+ "<head><title>First</title></head>\n"
327+
+ "<body>\n"
328+
+ "</body></html>\n";
329+
330+
final WebDriver driver = loadPage2(htmlFirst);
331+
332+
assertEquals("First", driver.getTitle());
333+
334+
final Set<String> windowHandles = driver.getWindowHandles();
335+
assertEquals(1, windowHandles.size());
336+
337+
driver.close();
338+
339+
try {
340+
driver.getWindowHandle();
341+
Assert.fail("NoSuchSessionException expected");
342+
}
343+
catch (final NoSuchSessionException e) {
344+
// expected
345+
}
346+
}
263347
}

0 commit comments

Comments
 (0)