20
20
import static org .assertj .core .api .Assertions .assertThat ;
21
21
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
22
22
import static org .mockito .ArgumentMatchers .any ;
23
+ import static org .mockito .ArgumentMatchers .argThat ;
23
24
import static org .mockito .Mockito .mock ;
25
+ import static org .mockito .Mockito .verify ;
26
+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
24
27
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 ;
25
33
26
34
import com .google .common .collect .ImmutableMap ;
27
35
30
38
import org .openqa .selenium .Capabilities ;
31
39
import org .openqa .selenium .ImmutableCapabilities ;
32
40
import org .openqa .selenium .Platform ;
41
+ import org .openqa .selenium .SessionNotCreatedException ;
33
42
import org .openqa .selenium .testing .UnitTests ;
34
43
35
44
import java .io .IOException ;
45
+ import java .io .UncheckedIOException ;
36
46
import java .util .UUID ;
37
47
38
48
@ Category (UnitTests .class )
@@ -42,12 +52,108 @@ public class RemoteWebDriverInitializationTest {
42
52
@ Test
43
53
public void testQuitsIfStartSessionFails () {
44
54
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" );
47
57
48
58
assertThat (quitCalled ).isTrue ();
49
59
}
50
60
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
+
51
157
@ Test
52
158
public void canHandleNonStandardCapabilitiesReturnedByRemoteEnd () throws IOException {
53
159
Response resp = new Response ();
@@ -75,4 +181,13 @@ public void quit() {
75
181
quitCalled = true ;
76
182
}
77
183
}
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
+ }
78
193
}
0 commit comments