35
35
import org .testcontainers .containers .wait .strategy .WaitStrategy ;
36
36
import org .testcontainers .lifecycle .TestDescription ;
37
37
import org .testcontainers .lifecycle .TestLifecycleAware ;
38
+ import org .testcontainers .utility .ComparableVersion ;
38
39
import org .testcontainers .utility .DockerImageName ;
39
40
40
41
/**
44
45
*/
45
46
public class BrowserWebDriverContainer <SELF extends BrowserWebDriverContainer <SELF >> extends GenericContainer <SELF > implements LinkableContainer , TestLifecycleAware {
46
47
47
- private static final DockerImageName CHROME_IMAGE = DockerImageName .parse ("selenium/standalone-chrome-debug" );
48
- private static final DockerImageName FIREFOX_IMAGE = DockerImageName .parse ("selenium/standalone-firefox-debug" );
48
+ private static final DockerImageName CHROME_IMAGE = DockerImageName .parse ("selenium/standalone-chrome" );
49
+ private static final DockerImageName FIREFOX_IMAGE = DockerImageName .parse ("selenium/standalone-firefox" );
50
+ private static final DockerImageName CHROME_DEBUG_IMAGE = DockerImageName .parse ("selenium/standalone-chrome-debug" );
51
+ private static final DockerImageName FIREFOX_DEBUG_IMAGE = DockerImageName .parse ("selenium/standalone-firefox-debug" );
49
52
private static final DockerImageName [] COMPATIBLE_IMAGES = new DockerImageName [] {
50
53
CHROME_IMAGE ,
51
54
FIREFOX_IMAGE ,
52
- DockerImageName . parse ( "selenium/standalone-chrome" ) ,
53
- DockerImageName . parse ( "selenium/standalone-firefox" )
55
+ CHROME_DEBUG_IMAGE ,
56
+ FIREFOX_DEBUG_IMAGE
54
57
};
55
58
56
59
private static final String DEFAULT_PASSWORD = "secret" ;
@@ -156,23 +159,6 @@ protected void configure() {
156
159
157
160
String seleniumVersion = SeleniumUtils .determineClasspathSeleniumVersion ();
158
161
159
- if (capabilities == null ) {
160
- if (seleniumVersion .startsWith ("2." )) {
161
- logger ().info ("No capabilities provided, falling back to DesiredCapabilities.chrome()" );
162
- capabilities = DesiredCapabilities .chrome ();
163
- } else {
164
- logger ().info ("No capabilities provided, falling back to ChromeOptions" );
165
- capabilities = new ChromeOptions ();
166
- }
167
- }
168
-
169
- // Hack for new selenium-chrome image that contains Chrome 92.
170
- // If not disabled, container startup will fail in most cases and consume excessive amounts of CPU.
171
- if (capabilities instanceof ChromeOptions ) {
172
- ChromeOptions options = (ChromeOptions ) this .capabilities ;
173
- options .addArguments ("--disable-gpu" );
174
- }
175
-
176
162
if (recordingMode != VncRecordingMode .SKIP ) {
177
163
178
164
if (vncRecordingDirectory == null ) {
@@ -244,12 +230,14 @@ public static String getDockerImageForCapabilities(Capabilities capabilities, St
244
230
}
245
231
246
232
private static DockerImageName getStandardImageForCapabilities (Capabilities capabilities , String seleniumVersion ) {
247
- String browserName = capabilities .getBrowserName ();
233
+ String browserName = capabilities == null ? BrowserType .CHROME : capabilities .getBrowserName ();
234
+ boolean supportsVncWithoutDebugImage = new ComparableVersion (seleniumVersion ).isGreaterThanOrEqualTo ("4" );
235
+
248
236
switch (browserName ) {
249
237
case BrowserType .CHROME :
250
- return CHROME_IMAGE .withTag (seleniumVersion );
238
+ return ( supportsVncWithoutDebugImage ? CHROME_IMAGE : CHROME_DEBUG_IMAGE ) .withTag (seleniumVersion );
251
239
case BrowserType .FIREFOX :
252
- return FIREFOX_IMAGE .withTag (seleniumVersion );
240
+ return ( supportsVncWithoutDebugImage ? FIREFOX_IMAGE : FIREFOX_DEBUG_IMAGE ) .withTag (seleniumVersion );
253
241
default :
254
242
throw new UnsupportedOperationException ("Browser name must be 'chrome' or 'firefox'; provided '" + browserName + "' is not supported" );
255
243
}
@@ -280,10 +268,6 @@ public int getPort() {
280
268
281
269
@ Override
282
270
protected void containerIsStarted (InspectContainerResponse containerInfo ) {
283
- driver = Unreliables .retryUntilSuccess (30 , TimeUnit .SECONDS ,
284
- () -> Timeouts .getWithTimeout (10 , TimeUnit .SECONDS ,
285
- () -> new RemoteWebDriver (getSeleniumAddress (), capabilities )));
286
-
287
271
if (vncRecordingContainer != null ) {
288
272
LOGGER .debug ("Starting VNC recording" );
289
273
vncRecordingContainer .start ();
@@ -298,7 +282,19 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
298
282
*
299
283
* @return a new Remote Web Driver instance
300
284
*/
301
- public RemoteWebDriver getWebDriver () {
285
+ public synchronized RemoteWebDriver getWebDriver () {
286
+ if (driver == null ) {
287
+ if (capabilities == null ) {
288
+ logger ().warn ("No capabilities provided - this will cause an exception in future versions. Falling back to ChromeOptions" );
289
+ capabilities = new ChromeOptions ();
290
+ }
291
+
292
+ driver = Unreliables .retryUntilSuccess (30 , TimeUnit .SECONDS , () -> {
293
+ return Timeouts .getWithTimeout (10 , TimeUnit .SECONDS , () -> {
294
+ return new RemoteWebDriver (getSeleniumAddress (), capabilities );
295
+ });
296
+ });
297
+ }
302
298
return driver ;
303
299
}
304
300
0 commit comments