Skip to content

Commit 29d3131

Browse files
committed
[java] Refactoring unit tests for RemoteWebDriver and RemoteWebElement and adding unit tests for recently implemented operations (getDomAttribute, getDomProperty, getAriaRole, getAccessibleName)
1 parent cf7d061 commit 29d3131

6 files changed

+946
-860
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.remote;
19+
20+
import java.util.Map;
21+
22+
class MultiCommandPayload extends CommandPayload {
23+
24+
private final int times;
25+
26+
MultiCommandPayload(int times, String name, Map<String, ?> parameters) {
27+
super(name, parameters);
28+
this.times = times;
29+
}
30+
31+
public int getTimes() {
32+
return times;
33+
}
34+
}

java/client/test/org/openqa/selenium/remote/RemoteWebDriverInitializationTest.java

+117-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,16 @@
2020
import static org.assertj.core.api.Assertions.assertThat;
2121
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2222
import static org.mockito.ArgumentMatchers.any;
23+
import static org.mockito.ArgumentMatchers.argThat;
2324
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.verify;
26+
import static org.mockito.Mockito.verifyNoMoreInteractions;
2427
import static org.mockito.Mockito.when;
28+
import static org.openqa.selenium.remote.WebDriverFixture.echoCapabilities;
29+
import static org.openqa.selenium.remote.WebDriverFixture.exceptionResponder;
30+
import static org.openqa.selenium.remote.WebDriverFixture.nullResponder;
31+
import static org.openqa.selenium.remote.WebDriverFixture.nullValueResponder;
32+
import static org.openqa.selenium.remote.WebDriverFixture.valueResponder;
2533

2634
import com.google.common.collect.ImmutableMap;
2735

@@ -30,9 +38,11 @@
3038
import org.openqa.selenium.Capabilities;
3139
import org.openqa.selenium.ImmutableCapabilities;
3240
import org.openqa.selenium.Platform;
41+
import org.openqa.selenium.SessionNotCreatedException;
3342
import org.openqa.selenium.testing.UnitTests;
3443

3544
import java.io.IOException;
45+
import java.io.UncheckedIOException;
3646
import java.util.UUID;
3747

3848
@Category(UnitTests.class)
@@ -42,12 +52,108 @@ public class RemoteWebDriverInitializationTest {
4252
@Test
4353
public void testQuitsIfStartSessionFails() {
4454
assertThatExceptionOfType(RuntimeException.class)
45-
.isThrownBy(() -> new BadStartSessionRemoteWebDriver(mock(CommandExecutor.class), new ImmutableCapabilities()))
46-
.withMessageContaining("Stub session that should fail");
55+
.isThrownBy(() -> new BadStartSessionRemoteWebDriver(mock(CommandExecutor.class), new ImmutableCapabilities()))
56+
.withMessageContaining("Stub session that should fail");
4757

4858
assertThat(quitCalled).isTrue();
4959
}
5060

61+
@Test
62+
public void constructorShouldThrowIfExecutorIsNull() {
63+
assertThatExceptionOfType(IllegalArgumentException.class)
64+
.isThrownBy(() -> new RemoteWebDriver((CommandExecutor) null, new ImmutableCapabilities()))
65+
.withMessage("RemoteWebDriver cannot work without a command executor");
66+
}
67+
68+
@Test
69+
public void constructorShouldThrowIfExecutorThrowsOnAnAttemptToStartASession() {
70+
CommandExecutor executor = WebDriverFixture.prepareExecutorMock(exceptionResponder);
71+
72+
assertThatExceptionOfType(SessionNotCreatedException.class)
73+
.isThrownBy(() -> new RemoteWebDriver(executor, new ImmutableCapabilities()))
74+
.withMessageContaining("Build info: ")
75+
.withMessageContaining("Driver info: org.openqa.selenium.remote.RemoteWebDriver")
76+
.withMessageContaining("Command: [null, newSession {desiredCapabilities=Capabilities {}}]");
77+
78+
verifyNoCommands(executor);
79+
}
80+
81+
@Test
82+
public void constructorShouldThrowIfExecutorReturnsNullOnAnAttemptToStartASession() {
83+
CommandExecutor executor = WebDriverFixture.prepareExecutorMock(nullResponder);
84+
assertThatExceptionOfType(SessionNotCreatedException.class)
85+
.isThrownBy(() -> new RemoteWebDriver(executor, new ImmutableCapabilities()));
86+
87+
verifyNoCommands(executor);
88+
}
89+
90+
@Test
91+
public void constructorShouldThrowIfExecutorReturnsAResponseWithNullValueOnAnAttemptToStartASession() {
92+
CommandExecutor executor = WebDriverFixture.prepareExecutorMock(nullValueResponder);
93+
assertThatExceptionOfType(SessionNotCreatedException.class)
94+
.isThrownBy(() -> new RemoteWebDriver(executor, new ImmutableCapabilities()));
95+
96+
verifyNoCommands(executor);
97+
}
98+
99+
@Test
100+
public void constructorShouldThrowIfExecutorReturnsSomethingButNotCapabilitiesOnAnAttemptToStartASession() {
101+
CommandExecutor executor = WebDriverFixture.prepareExecutorMock(valueResponder("OK"));
102+
assertThatExceptionOfType(SessionNotCreatedException.class)
103+
.isThrownBy(() -> new RemoteWebDriver(executor, new ImmutableCapabilities()));
104+
105+
verifyNoCommands(executor);
106+
}
107+
108+
@Test
109+
public void constructorStartsSessionAndPassesCapabilities() throws IOException {
110+
CommandExecutor executor = WebDriverFixture.prepareExecutorMock(echoCapabilities, nullValueResponder);
111+
ImmutableCapabilities capabilities = new ImmutableCapabilities("browserName", "cheese browser");
112+
113+
RemoteWebDriver driver = new RemoteWebDriver(executor, capabilities);
114+
115+
verify(executor).execute(argThat(
116+
command -> command.getName().equals(DriverCommand.NEW_SESSION)
117+
&& command.getSessionId() == null
118+
&& command.getParameters().get("desiredCapabilities") == capabilities
119+
));
120+
verifyNoMoreInteractions(executor);
121+
assertThat(driver.getSessionId()).isNotNull();
122+
}
123+
124+
@Test
125+
public void canHandlePlatformNameCapability() {
126+
WebDriverFixture fixture = new WebDriverFixture(
127+
new ImmutableCapabilities(
128+
"browserName", "cheese browser", "platformName", Platform.MOJAVE),
129+
echoCapabilities, nullValueResponder);
130+
131+
assertThat(fixture.driver.getCapabilities().getPlatformName())
132+
.satisfies(p -> p.is(Platform.MOJAVE));
133+
}
134+
135+
@Test
136+
public void canHandlePlatformOSSCapability() {
137+
WebDriverFixture fixture = new WebDriverFixture(
138+
new ImmutableCapabilities(
139+
"browserName", "cheese browser", "platform", Platform.MOJAVE),
140+
echoCapabilities, nullValueResponder);
141+
142+
assertThat(fixture.driver.getCapabilities().getPlatformName())
143+
.satisfies(p -> p.is(Platform.MOJAVE));
144+
}
145+
146+
@Test
147+
public void canHandleUnknownPlatformNameAndFallsBackToUnix() {
148+
WebDriverFixture fixture = new WebDriverFixture(
149+
new ImmutableCapabilities(
150+
"browserName", "cheese browser", "platformName", "cheese platform"),
151+
echoCapabilities, nullValueResponder);
152+
153+
assertThat(fixture.driver.getCapabilities().getPlatformName())
154+
.satisfies(p -> p.is(Platform.UNIX)); // fallback
155+
}
156+
51157
@Test
52158
public void canHandleNonStandardCapabilitiesReturnedByRemoteEnd() throws IOException {
53159
Response resp = new Response();
@@ -75,4 +181,13 @@ public void quit() {
75181
quitCalled = true;
76182
}
77183
}
184+
185+
public void verifyNoCommands(CommandExecutor executor) {
186+
try {
187+
verify(executor).execute(argThat(cmd -> cmd.getName().equals(DriverCommand.NEW_SESSION)));
188+
} catch (IOException ex) {
189+
throw new UncheckedIOException(ex);
190+
}
191+
verifyNoMoreInteractions(executor);
192+
}
78193
}

0 commit comments

Comments
 (0)