Skip to content

Commit 5f96eb0

Browse files
committed
Refactoring tests
1 parent 7dc41eb commit 5f96eb0

File tree

4 files changed

+71
-99
lines changed

4 files changed

+71
-99
lines changed

java/client/test/org/openqa/selenium/VisibilityTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ public class VisibilityTest extends JUnit4TestBase {
5050
public void testShouldAllowTheUserToTellIfAnElementIsDisplayedOrNot() {
5151
driver.get(pages.javascriptPage);
5252

53-
assertThat(driver.findElement(By.id("displayed")).isDisplayed(),
54-
is(true));
55-
assertThat(driver.findElement(By.id("none")).isDisplayed(), is(false));
56-
assertThat(driver.findElement(By.id("suppressedParagraph")).isDisplayed(), is(false));
57-
assertThat(driver.findElement(By.id("hidden")).isDisplayed(), is(false));
53+
assertTrue(driver.findElement(By.id("displayed")).isDisplayed());
54+
assertFalse(driver.findElement(By.id("none")).isDisplayed());
55+
assertFalse(driver.findElement(By.id("suppressedParagraph")).isDisplayed());
56+
assertFalse(driver.findElement(By.id("hidden")).isDisplayed());
5857
}
5958

6059
@Test

java/client/test/org/openqa/selenium/WindowSwitchingTest.java

Lines changed: 65 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.hamcrest.Matchers.equalTo;
2121
import static org.hamcrest.Matchers.instanceOf;
2222
import static org.junit.Assert.assertEquals;
23-
import static org.junit.Assert.assertFalse;
2423
import static org.junit.Assert.assertNotNull;
2524
import static org.junit.Assert.assertThat;
2625
import static org.junit.Assume.assumeFalse;
@@ -34,9 +33,11 @@
3433
import static org.openqa.selenium.testing.Driver.REMOTE;
3534
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;
3635

37-
import com.google.common.collect.Sets;
38-
36+
import org.junit.Rule;
3937
import org.junit.Test;
38+
import org.junit.rules.TestRule;
39+
import org.junit.rules.TestWatcher;
40+
import org.junit.runner.Description;
4041
import org.openqa.selenium.support.ui.ExpectedConditions;
4142
import org.openqa.selenium.testing.Ignore;
4243
import org.openqa.selenium.testing.JUnit4TestBase;
@@ -47,9 +48,33 @@
4748
import org.openqa.selenium.testing.drivers.Browser;
4849

4950
import java.util.Set;
51+
import java.util.stream.Collectors;
5052

5153
public class WindowSwitchingTest extends JUnit4TestBase {
5254

55+
@Rule
56+
public final TestRule switchToMainWindow = new TestWatcher() {
57+
private String mainWindow;
58+
59+
@Override
60+
protected void starting(Description description) {
61+
super.starting(description);
62+
mainWindow = driver.getWindowHandle();
63+
}
64+
65+
@Override
66+
protected void finished(Description description) {
67+
try {
68+
if (! mainWindow.equals(driver.getWindowHandle())) {
69+
driver.close();
70+
}
71+
} catch (Exception ignore) {
72+
}
73+
driver.switchTo().window(mainWindow);
74+
super.finished(description);
75+
}
76+
};
77+
5378
@SwitchToTopAfterTest
5479
@NoDriverAfterTest(failedOnly = true)
5580
@Test
@@ -58,7 +83,6 @@ public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOpera
5883
TestUtilities.getEffectivePlatform().is(Platform.WINDOWS));
5984

6085
driver.get(pages.xhtmlTestPage);
61-
String current = driver.getWindowHandle();
6286
Set<String> currentWindowHandles = driver.getWindowHandles();
6387

6488
driver.findElement(By.linkText("Open new window")).click();
@@ -75,30 +99,20 @@ public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOpera
7599
driver.findElement(By.id("iframe_page_heading"));
76100
driver.switchTo().frame("iframe1");
77101
assertThat(driver.getWindowHandle(), equalTo(handle));
78-
79-
driver.close();
80-
driver.switchTo().window(current);
81102
}
82103

83104
@Test
84105
public void testShouldThrowNoSuchWindowException() {
85106
driver.get(pages.xhtmlTestPage);
86-
String current = driver.getWindowHandle();
87-
88-
try {
89-
Throwable t = catchThrowable(() -> driver.switchTo().window("invalid name"));
90-
assertThat(t, instanceOf(NoSuchWindowException.class));
91-
} finally {
92-
driver.switchTo().window(current);
93-
}
107+
Throwable t = catchThrowable(() -> driver.switchTo().window("invalid name"));
108+
assertThat(t, instanceOf(NoSuchWindowException.class));
94109
}
95110

96111
@NoDriverAfterTest(failedOnly = true)
97112
@Ignore({MARIONETTE})
98113
@Test
99114
public void testShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle() {
100115
driver.get(pages.xhtmlTestPage);
101-
String current = driver.getWindowHandle();
102116
Set<String> currentWindowHandles = driver.getWindowHandles();
103117

104118
driver.findElement(By.linkText("Open new window")).click();
@@ -108,20 +122,15 @@ public void testShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle() {
108122
driver.switchTo().window("result");
109123
driver.close();
110124

111-
try {
112-
Throwable t = catchThrowable(driver::getWindowHandle);
113-
assertThat(t, instanceOf(NoSuchWindowException.class));
114-
} finally {
115-
driver.switchTo().window(current);
116-
}
125+
Throwable t = catchThrowable(driver::getWindowHandle);
126+
assertThat(t, instanceOf(NoSuchWindowException.class));
117127
}
118128

119129
@NoDriverAfterTest(failedOnly = true)
120130
@Ignore({MARIONETTE})
121131
@Test
122132
public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed() {
123133
driver.get(pages.xhtmlTestPage);
124-
String current = driver.getWindowHandle();
125134
Set<String> currentWindowHandles = driver.getWindowHandles();
126135

127136
driver.findElement(By.linkText("Open new window")).click();
@@ -131,23 +140,18 @@ public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed(
131140
driver.switchTo().window("result");
132141
driver.close();
133142

134-
try {
135-
Throwable t = catchThrowable(driver::getTitle);
136-
assertThat(t, instanceOf(NoSuchWindowException.class));
143+
Throwable t = catchThrowable(driver::getTitle);
144+
assertThat(t, instanceOf(NoSuchWindowException.class));
137145

138-
Throwable t2 = catchThrowable(() -> driver.findElement(By.tagName("body")));
139-
assertThat(t2, instanceOf(NoSuchWindowException.class));
140-
} finally {
141-
driver.switchTo().window(current);
142-
}
146+
Throwable t2 = catchThrowable(() -> driver.findElement(By.tagName("body")));
147+
assertThat(t2, instanceOf(NoSuchWindowException.class));
143148
}
144149

145150
@NoDriverAfterTest(failedOnly = true)
146151
@Ignore({MARIONETTE})
147152
@Test
148153
public void testShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIsClosed() {
149154
driver.get(pages.xhtmlTestPage);
150-
String current = driver.getWindowHandle();
151155
Set<String> currentWindowHandles = driver.getWindowHandles();
152156

153157
driver.findElement(By.linkText("Open new window")).click();
@@ -158,12 +162,8 @@ public void testShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIs
158162
WebElement body = driver.findElement(By.tagName("body"));
159163
driver.close();
160164

161-
try {
162-
Throwable t = catchThrowable(body::getText);
163-
assertThat(t, instanceOf(NoSuchWindowException.class));
164-
} finally {
165-
driver.switchTo().window(current);
166-
}
165+
Throwable t = catchThrowable(body::getText);
166+
assertThat(t, instanceOf(NoSuchWindowException.class));
167167
}
168168

169169
@NoDriverAfterTest
@@ -181,14 +181,13 @@ public void testShouldBeAbleToIterateOverAllOpenWindows() {
181181
Set<String> allWindowHandles = driver.getWindowHandles();
182182

183183
// There should be three windows. We should also see each of the window titles at least once.
184-
Set<String> seenHandles = Sets.newHashSet();
185-
for (String handle : allWindowHandles) {
186-
assertFalse(seenHandles.contains(handle));
184+
Set<String> allWindowTitles = allWindowHandles.stream().map(handle -> {
187185
driver.switchTo().window(handle);
188-
seenHandles.add(handle);
189-
}
186+
return driver.getTitle();
187+
}).collect(Collectors.toSet());
190188

191189
assertEquals(3, allWindowHandles.size());
190+
assertEquals(3, allWindowTitles.size());
192191
}
193192

194193
@JavascriptEnabled
@@ -202,7 +201,6 @@ public void testClickingOnAButtonThatClosesAnOpenWindowDoesNotCauseTheBrowserToH
202201
driver.get(pages.xhtmlTestPage);
203202
Boolean isIEDriver = TestUtilities.isInternetExplorer(driver);
204203
Boolean isIE6 = TestUtilities.isIe6(driver);
205-
String currentHandle = driver.getWindowHandle();
206204
Set<String> currentWindowHandles = driver.getWindowHandles();
207205

208206
driver.findElement(By.name("windowThree")).click();
@@ -215,20 +213,15 @@ public void testClickingOnAButtonThatClosesAnOpenWindowDoesNotCauseTheBrowserToH
215213
if (TestUtilities.isChrome(driver) && TestUtilities.getEffectivePlatform(driver).is(ANDROID)) {
216214
Thread.sleep(1000);
217215
}
218-
try {
219-
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("close")));
220-
driver.findElement(By.id("close")).click();
221216

222-
if (isIEDriver && !isIE6) {
223-
Alert alert = wait.until(alertIsPresent());
224-
alert.accept();
225-
}
217+
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("close"))).click();
226218

227-
// If we make it this far, we're all good.
228-
} finally {
229-
driver.switchTo().window(currentHandle);
230-
driver.findElement(By.id("linkId"));
219+
if (isIEDriver && !isIE6) {
220+
Alert alert = wait.until(alertIsPresent());
221+
alert.accept();
231222
}
223+
224+
// If we make it this far, we're all good.
232225
}
233226

234227
@JavascriptEnabled
@@ -242,7 +235,6 @@ public void testCanCallGetWindowHandlesAfterClosingAWindow() throws Exception {
242235

243236
Boolean isIEDriver = TestUtilities.isInternetExplorer(driver);
244237
Boolean isIE6 = TestUtilities.isIe6(driver);
245-
String currentHandle = driver.getWindowHandle();
246238
Set<String> currentWindowHandles = driver.getWindowHandles();
247239

248240
driver.findElement(By.name("windowThree")).click();
@@ -256,29 +248,23 @@ public void testCanCallGetWindowHandlesAfterClosingAWindow() throws Exception {
256248
if (TestUtilities.isChrome(driver) && TestUtilities.getEffectivePlatform(driver).is(ANDROID)) {
257249
Thread.sleep(1000);
258250
}
259-
try {
260-
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("close"))).click();
261-
262-
if (isIEDriver && !isIE6) {
263-
Alert alert = wait.until(alertIsPresent());
264-
alert.accept();
265-
}
266251

267-
Set<String> allHandles = wait.until(windowHandleCountToBe(allWindowHandles - 1));
252+
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("close"))).click();
268253

269-
assertEquals(currentWindowHandles.size(), allHandles.size());
270-
} finally {
271-
driver.switchTo().window(currentHandle);
254+
if (isIEDriver && !isIE6) {
255+
Alert alert = wait.until(alertIsPresent());
256+
alert.accept();
272257
}
258+
259+
Set<String> allHandles = wait.until(windowHandleCountToBe(allWindowHandles - 1));
260+
261+
assertEquals(currentWindowHandles.size(), allHandles.size());
273262
}
274263

275264
@Test
276265
public void testCanObtainAWindowHandle() {
277266
driver.get(pages.xhtmlTestPage);
278-
279-
String currentHandle = driver.getWindowHandle();
280-
281-
assertNotNull(currentHandle);
267+
assertNotNull(driver.getWindowHandle());
282268
}
283269

284270
@Test
@@ -310,17 +296,12 @@ public void testCanCloseWindowWhenMultipleWindowsAreOpen() {
310296
// There should be two windows. We should also see each of the window titles at least once.
311297
assertEquals(2, allWindowHandles.size());
312298

313-
for (String handle : allWindowHandles) {
314-
if (! handle.equals(mainHandle)) {
315-
driver.switchTo().window(handle);
316-
driver.close();
317-
break;
318-
}
319-
}
299+
allWindowHandles.stream().filter(anObject -> ! mainHandle.equals(anObject)).forEach(handle -> {
300+
driver.switchTo().window(handle);
301+
driver.close();
302+
});
320303

321304
assertEquals(1, driver.getWindowHandles().size());
322-
323-
driver.switchTo().window(mainHandle);
324305
}
325306

326307
@NoDriverAfterTest(failedOnly = true)
@@ -340,13 +321,10 @@ public void testCanCloseWindowAndSwitchBackToMainWindow() {
340321
// There should be two windows. We should also see each of the window titles at least once.
341322
assertEquals(2, allWindowHandles.size());
342323

343-
for (String handle : allWindowHandles) {
344-
if (! handle.equals(mainHandle)) {
345-
driver.switchTo().window(handle);
346-
driver.close();
347-
break;
348-
}
349-
}
324+
allWindowHandles.stream().filter(anObject -> ! mainHandle.equals(anObject)).forEach(handle -> {
325+
driver.switchTo().window(handle);
326+
driver.close();
327+
});
350328

351329
driver.switchTo().window(mainHandle);
352330

java/client/test/org/openqa/selenium/WindowTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import org.openqa.selenium.testing.TestUtilities;
3737
import org.openqa.selenium.testing.drivers.SauceDriver;
3838

39-
import java.util.logging.Logger;
40-
4139
public class WindowTest extends JUnit4TestBase {
4240

4341
@Test

java/client/test/org/openqa/selenium/testing/TestUtilities.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.openqa.selenium.remote.CapabilityType;
2727
import org.openqa.selenium.testing.drivers.SauceDriver;
2828

29-
import java.util.function.Consumer;
3029
import java.util.regex.Matcher;
3130
import java.util.regex.Pattern;
3231

@@ -96,13 +95,11 @@ public static boolean isChrome(WebDriver driver) {
9695
}
9796

9897
public static boolean isOldChromedriver(WebDriver driver) {
99-
Capabilities caps;
100-
try {
101-
caps = ((HasCapabilities) driver).getCapabilities();
102-
} catch (ClassCastException e) {
98+
if (!(driver instanceof HasCapabilities)) {
10399
// Driver does not support capabilities -- not a chromedriver at all.
104100
return false;
105101
}
102+
Capabilities caps = ((HasCapabilities) driver).getCapabilities();
106103
String chromedriverVersion = (String) caps.getCapability("chrome.chromedriverVersion");
107104
if (chromedriverVersion != null) {
108105
String[] versionMajorMinor = chromedriverVersion.split("\\.", 2);

0 commit comments

Comments
 (0)