|
25 | 25 | import java.util.List;
|
26 | 26 | import java.util.Map;
|
27 | 27 |
|
| 28 | +import org.junit.After; |
28 | 29 | import org.junit.Before;
|
29 | 30 | import org.openqa.selenium.Capabilities;
|
| 31 | +import org.openqa.selenium.NoSuchSessionException; |
30 | 32 | import org.openqa.selenium.WebDriver;
|
31 | 33 | import org.openqa.selenium.htmlunit.HtmlUnitDriver;
|
32 | 34 | import org.openqa.selenium.htmlunit.WebDriverTestCase;
|
@@ -60,6 +62,18 @@ public void setUp() {
|
60 | 62 | sessionId = createDriverSession();
|
61 | 63 | }
|
62 | 64 |
|
| 65 | + @After |
| 66 | + public void cleanUp() { |
| 67 | + if (sessionId != null) { |
| 68 | + try { |
| 69 | + closeDriverSession(); |
| 70 | + } catch (NoSuchSessionException e) { |
| 71 | + // nothing to do here |
| 72 | + } |
| 73 | + sessionId = null; |
| 74 | + } |
| 75 | + } |
| 76 | + |
63 | 77 | @Override
|
64 | 78 | protected WebDriver getWebDriver() {
|
65 | 79 | return HtmlUnitDriverServer.getDriverSession(sessionId);
|
@@ -92,6 +106,21 @@ private static HttpRequest newSessionRequest(final Capabilities capabilities) {
|
92 | 106 | return request;
|
93 | 107 | }
|
94 | 108 |
|
| 109 | + private void closeDriverSession() throws NoSuchSessionException { |
| 110 | + List<String> windowHandles = getWindowHandles(sessionId); |
| 111 | + while ( ! windowHandles.isEmpty()) { |
| 112 | + HttpResponse response = HtmlUnitDriverServer.closeWindow(sessionId); |
| 113 | + assertEquals("Failed closing window", HTTP_OK, response.getStatus()); |
| 114 | + windowHandles = extractListOfStrings(response); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private List<String> getWindowHandles(final String sessionId) throws NoSuchSessionException { |
| 119 | + HttpResponse response = HtmlUnitDriverServer.getWindowHandles(sessionId); |
| 120 | + assertEquals("Failed getting window handles", HTTP_OK, response.getStatus()); |
| 121 | + return extractListOfStrings(response); |
| 122 | + } |
| 123 | + |
95 | 124 | protected <T> T extractValueOfType(final HttpResponse response, final Type type) {
|
96 | 125 | Map<String, T> content = fromJson(response, type);
|
97 | 126 | assertTrue("Failed finding response value", content.containsKey("value"));
|
|
0 commit comments