Skip to content

Commit d867b37

Browse files
authored
[java] Fix asserts for maps and sets (#16808)
* Fix asserts for Sets and Maps The problem is that AssertJ's method `isEqualTo` doesn't always work for Sets and Maps because it assumes strict order of elements: 1. `assertThat(MAP).isEqualTo(Map.of(...))` 2. `assertThat(SET).isEqualTo(Set.of(...))` In both cases, we have to replace `isEqualTo` by `containsExactlyInAnyOrderEntriesOf` or similar method. * Simplify creating lists in ByChainedTest After the simplification, it became obvious that some of ByChainedTest tests don't really test anything. They create a bunch of lists (e.g. `elems345`) which are not used. And only the empty list of elements is loaded and asserted. Seems that this complex/misleading test logic in ByChainedTest was introduced in commit c4861b8 (some "patch from BenChambers"). Needs to be reviewed and fixed. * slightly improve PDF check in BrowsingContextTest now we check that the resulting content at least _looks like PDF_.
1 parent d2de0b2 commit d867b37

23 files changed

+364
-375
lines changed

java/test/org/openqa/selenium/ProxyTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static org.assertj.core.api.Assertions.assertThat;
2121
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
22+
import static org.assertj.core.api.InstanceOfAssertFactories.LIST;
2223
import static org.openqa.selenium.Proxy.ProxyType.AUTODETECT;
2324
import static org.openqa.selenium.Proxy.ProxyType.DIRECT;
2425
import static org.openqa.selenium.Proxy.ProxyType.MANUAL;
@@ -28,7 +29,6 @@
2829
import static org.openqa.selenium.remote.CapabilityType.PROXY;
2930

3031
import java.util.HashMap;
31-
import java.util.List;
3232
import java.util.Map;
3333
import org.junit.jupiter.api.Disabled;
3434
import org.junit.jupiter.api.Tag;
@@ -235,7 +235,7 @@ void manualProxyToJson() {
235235
assertThat(json.get("socksVersion")).isEqualTo(5);
236236
assertThat(json.get("socksUsername")).isEqualTo("test1");
237237
assertThat(json.get("socksPassword")).isEqualTo("test2");
238-
assertThat(json.get("noProxy")).isEqualTo(List.of("localhost", "127.0.0.*"));
238+
assertThat(json.get("noProxy")).asInstanceOf(LIST).containsExactly("localhost", "127.0.0.*");
239239
assertThat(json.entrySet()).hasSize(9);
240240
}
241241

java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
package org.openqa.selenium.bidi.browsingcontext;
1919

20+
import static java.nio.charset.StandardCharsets.US_ASCII;
2021
import static org.assertj.core.api.Assertions.assertThat;
2122
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2223
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
2324
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
2425
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
2526

27+
import java.util.Base64;
2628
import java.util.List;
2729
import org.junit.jupiter.api.Test;
2830
import org.openqa.selenium.By;
@@ -511,8 +513,12 @@ void canPrintPage() {
511513
// Comparing expected PDF is a hard problem.
512514
// As long as we are sending the parameters correctly it should be fine.
513515
// Trusting the browsers to do the right thing.
514-
// Hence, just checking if the response is base64 encoded string.
516+
// Hence, just checking if the response is base64 encoded string looking like PDF.
515517
assertThat(printPage).contains("JVBER");
518+
byte[] pdf = Base64.getDecoder().decode(printPage);
519+
assertThat(pdf)
520+
.containsSequence("%PDF-".getBytes(US_ASCII))
521+
.containsSequence("%%EOF".getBytes(US_ASCII));
516522
}
517523

518524
@Test

java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.io.File;
3333
import java.time.Duration;
3434
import java.util.Base64;
35-
import java.util.HashMap;
3635
import java.util.List;
3736
import java.util.Map;
3837
import java.util.Set;
@@ -50,6 +49,7 @@
5049
class ChromeOptionsTest {
5150

5251
@Test
52+
@SuppressWarnings("unchecked")
5353
void optionsAsMapShouldBeImmutable() {
5454
Map<String, Object> options = new ChromeOptions().asMap();
5555
assertThatExceptionOfType(UnsupportedOperationException.class)
@@ -99,12 +99,13 @@ void canAddW3CCompliantOptions() {
9999
assertThat(mappedOptions.get("strictFileInteractability")).isEqualTo(true);
100100
assertThat(mappedOptions.get(ENABLE_DOWNLOADS)).isEqualTo(true);
101101

102-
Map<String, Long> expectedTimeouts = new HashMap<>();
103-
expectedTimeouts.put("implicit", 1000L);
104-
expectedTimeouts.put("pageLoad", 2000L);
105-
expectedTimeouts.put("script", 3000L);
106-
107-
assertThat(expectedTimeouts).isEqualTo(mappedOptions.get("timeouts"));
102+
assertThat(mappedOptions.get("timeouts"))
103+
.asInstanceOf(MAP)
104+
.containsExactlyInAnyOrderEntriesOf(
105+
Map.of(
106+
"implicit", 1000L,
107+
"pageLoad", 2000L,
108+
"script", 3000L));
108109
}
109110

110111
@Test
@@ -113,15 +114,15 @@ void canAddSequentialTimeouts() {
113114
chromeOptions.setImplicitWaitTimeout(Duration.ofSeconds(1));
114115

115116
Map<String, Object> mappedOptions = chromeOptions.asMap();
116-
Map<String, Long> expectedTimeouts = new HashMap<>();
117-
118-
expectedTimeouts.put("implicit", 1000L);
119-
assertThat(expectedTimeouts).isEqualTo(mappedOptions.get("timeouts"));
117+
assertThat(mappedOptions.get("timeouts"))
118+
.asInstanceOf(MAP)
119+
.containsExactlyInAnyOrderEntriesOf(Map.of("implicit", 1000L));
120120

121121
chromeOptions.setPageLoadTimeout(Duration.ofSeconds(2));
122-
expectedTimeouts.put("pageLoad", 2000L);
123122
Map<String, Object> mappedOptions2 = chromeOptions.asMap();
124-
assertThat(expectedTimeouts).isEqualTo(mappedOptions2.get("timeouts"));
123+
assertThat(mappedOptions2.get("timeouts"))
124+
.asInstanceOf(MAP)
125+
.containsExactlyInAnyOrderEntriesOf(Map.of("implicit", 1000L, "pageLoad", 2000L));
125126
}
126127

127128
@Test
@@ -130,11 +131,9 @@ void mixAddingTimeoutsCapsAndSetter() {
130131
chromeOptions.setCapability(TIMEOUTS, Map.of("implicit", 1000));
131132
chromeOptions.setPageLoadTimeout(Duration.ofSeconds(2));
132133

133-
Map<String, Number> expectedTimeouts = new HashMap<>();
134-
expectedTimeouts.put("implicit", 1000);
135-
expectedTimeouts.put("pageLoad", 2000L);
136-
137-
assertThat(chromeOptions.asMap().get("timeouts")).isEqualTo(expectedTimeouts);
134+
assertThat(chromeOptions.asMap().get("timeouts"))
135+
.asInstanceOf(MAP)
136+
.containsExactlyInAnyOrderEntriesOf(Map.of("implicit", 1000, "pageLoad", 2000L));
138137
}
139138

140139
@Test
@@ -395,6 +394,6 @@ void shouldBeAbleToMergeAnAndroidOption() {
395394
var caps = new MutableCapabilities();
396395
var merged = original.merge(caps);
397396

398-
assertThat(merged.asMap()).isEqualTo(original.asMap());
397+
assertThat(merged.asMap()).containsExactlyInAnyOrderEntriesOf(original.asMap());
399398
}
400399
}

java/test/org/openqa/selenium/firefox/FirefoxProfileTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void convertingToJsonShouldNotPolluteTempDir() throws IOException {
171171
Arrays.stream(sysTemp.list())
172172
.filter(f -> f.endsWith("webdriver-profile"))
173173
.collect(Collectors.toSet());
174-
assertThat(after).isEqualTo(before);
174+
assertThat(after).containsExactlyInAnyOrderElementsOf(before);
175175
}
176176

177177
@Test
@@ -184,14 +184,16 @@ void shouldConvertItselfIntoAMeaningfulRepresentation() throws IOException {
184184

185185
File dir = Zip.unzipToTempDir(json, "webdriver", "duplicated");
186186

187-
File prefs = new File(dir, "user.js");
188-
assertThat(prefs).exists();
187+
try {
188+
File prefs = new File(dir, "user.js");
189+
assertThat(prefs).exists();
189190

190-
try (Stream<String> lines = Files.lines(prefs.toPath())) {
191-
assertThat(lines).anyMatch(s -> s.contains("i.like.cheese"));
191+
try (Stream<String> lines = Files.lines(prefs.toPath())) {
192+
assertThat(lines).anyMatch(s -> s.contains("i.like.cheese"));
193+
}
194+
} finally {
195+
FileHandler.delete(dir);
192196
}
193-
194-
FileHandler.delete(dir);
195197
}
196198

197199
private List<String> readGeneratedProperties(FirefoxProfile profile) throws Exception {

java/test/org/openqa/selenium/firefox/GeckoDriverServiceTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static org.mockito.Mockito.spy;
2626
import static org.mockito.Mockito.verify;
2727

28-
import java.io.File;
2928
import java.time.Duration;
3029
import org.junit.jupiter.api.Tag;
3130
import org.junit.jupiter.api.Test;
@@ -34,18 +33,23 @@
3433
class GeckoDriverServiceTest {
3534

3635
@Test
37-
void builderPassesTimeoutToDriverService() {
38-
File exe = new File("someFile");
36+
void builderUsesDefaultTimeoutForDriverService() {
3937
Duration defaultTimeout = Duration.ofSeconds(20);
40-
Duration customTimeout = Duration.ofSeconds(60);
4138

4239
GeckoDriverService.Builder builderMock = spy(GeckoDriverService.Builder.class);
4340
builderMock.build();
4441

4542
verify(builderMock).createDriverService(any(), anyInt(), eq(defaultTimeout), any(), any());
43+
}
44+
45+
@Test
46+
void builderPassesTimeoutToDriverService() {
47+
Duration customTimeout = Duration.ofSeconds(60);
48+
GeckoDriverService.Builder builderMock =
49+
spy(GeckoDriverService.Builder.class).withTimeout(customTimeout);
4650

47-
builderMock.withTimeout(customTimeout);
4851
builderMock.build();
52+
4953
verify(builderMock).createDriverService(any(), anyInt(), eq(customTimeout), any(), any());
5054
}
5155

java/test/org/openqa/selenium/grid/config/ConcatenatingConfigTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.assertj.core.api.Assertions.assertThat;
2121

2222
import java.util.Map;
23-
import java.util.Set;
2423
import org.junit.jupiter.api.Test;
2524

2625
class ConcatenatingConfigTest {
@@ -38,7 +37,7 @@ void shouldReturnSectionNames() {
3837
"FOO_", "should not show up",
3938
"BAR_FOOD_IS", "cheese sticks"));
4039

41-
assertThat(config.getSectionNames()).isEqualTo(Set.of("cheese", "vegetables"));
40+
assertThat(config.getSectionNames()).containsExactlyInAnyOrder("cheese", "vegetables");
4241
}
4342

4443
@Test
@@ -54,6 +53,6 @@ void shouldReturnOptionNamesInSection() {
5453
"FOO_", "should not show up",
5554
"BAR_FOOD_IS", "cheese sticks"));
5655

57-
assertThat(config.getOptions("cheese")).isEqualTo(Set.of("current", "selected"));
56+
assertThat(config.getOptions("cheese")).containsExactlyInAnyOrder("current", "selected");
5857
}
5958
}

java/test/org/openqa/selenium/ie/InternetExplorerOptionsTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,14 @@ void shouldSurviveASerializationRoundTrip() {
102102

103103
@Test
104104
void shouldSetIeOptionsCapabilityWhenConstructedFromExistingCapabilities() {
105-
InternetExplorerOptions expected = new InternetExplorerOptions();
106-
expected.setCapability("requireWindowFocus", true);
107-
108105
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
109106
desiredCapabilities.setPlatform(Platform.WINDOWS);
110107
InternetExplorerOptions seen = new InternetExplorerOptions(desiredCapabilities);
111108
seen.setCapability("requireWindowFocus", true);
112109

113-
assertThat(seen.getCapability(IE_OPTIONS)).isEqualTo(expected.getCapability(IE_OPTIONS));
110+
assertThat(seen.getCapability(IE_OPTIONS))
111+
.asInstanceOf(MAP)
112+
.containsExactlyInAnyOrderEntriesOf(Map.of("requireWindowFocus", true));
114113
}
115114

116115
@Test

java/test/org/openqa/selenium/interactions/PointerInputTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ void encodesWrappedElementInMoveOrigin() {
5252
new Json().toType(rawJson, ActionSequenceJson.class, PropertySetting.BY_FIELD);
5353

5454
assertThat(json.actions).hasSize(1);
55-
ActionJson firstAction = json.actions.get(0);
56-
assertThat(firstAction.origin).containsEntry(W3C.getEncodedElementKey(), "12345");
55+
assertThat(json.actions.get(0).origin)
56+
.containsExactlyInAnyOrderEntriesOf(Map.of(W3C.getEncodedElementKey(), "12345"));
5757
}
5858

5959
@Test

java/test/org/openqa/selenium/interactions/WheelInputTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ void shouldEncodeWrappedElementInScrollOrigin() {
5656
new Json().toType(rawJson, ActionSequenceJson.class, PropertySetting.BY_FIELD);
5757

5858
assertThat(json.actions).hasSize(1);
59-
ActionJson firstAction = json.actions.get(0);
60-
assertThat(firstAction.origin).containsEntry(W3C.getEncodedElementKey(), "12345");
59+
assertThat(json.actions.get(0).origin)
60+
.containsExactlyInAnyOrderEntriesOf(Map.of(W3C.getEncodedElementKey(), "12345"));
6161
}
6262

6363
@Test

0 commit comments

Comments
 (0)