3535import org .testcontainers .containers .wait .strategy .WaitStrategy ;
3636import org .testcontainers .lifecycle .TestDescription ;
3737import org .testcontainers .lifecycle .TestLifecycleAware ;
38+ import org .testcontainers .utility .ComparableVersion ;
3839import org .testcontainers .utility .DockerImageName ;
3940
4041/**
4445 */
4546public class BrowserWebDriverContainer <SELF extends BrowserWebDriverContainer <SELF >> extends GenericContainer <SELF > implements LinkableContainer , TestLifecycleAware {
4647
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" );
4952 private static final DockerImageName [] COMPATIBLE_IMAGES = new DockerImageName [] {
5053 CHROME_IMAGE ,
5154 FIREFOX_IMAGE ,
52- DockerImageName . parse ( "selenium/standalone-chrome" ) ,
53- DockerImageName . parse ( "selenium/standalone-firefox" )
55+ CHROME_DEBUG_IMAGE ,
56+ FIREFOX_DEBUG_IMAGE
5457 };
5558
5659 private static final String DEFAULT_PASSWORD = "secret" ;
@@ -156,23 +159,6 @@ protected void configure() {
156159
157160 String seleniumVersion = SeleniumUtils .determineClasspathSeleniumVersion ();
158161
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-
176162 if (recordingMode != VncRecordingMode .SKIP ) {
177163
178164 if (vncRecordingDirectory == null ) {
@@ -244,12 +230,14 @@ public static String getDockerImageForCapabilities(Capabilities capabilities, St
244230 }
245231
246232 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+
248236 switch (browserName ) {
249237 case BrowserType .CHROME :
250- return CHROME_IMAGE .withTag (seleniumVersion );
238+ return ( supportsVncWithoutDebugImage ? CHROME_IMAGE : CHROME_DEBUG_IMAGE ) .withTag (seleniumVersion );
251239 case BrowserType .FIREFOX :
252- return FIREFOX_IMAGE .withTag (seleniumVersion );
240+ return ( supportsVncWithoutDebugImage ? FIREFOX_IMAGE : FIREFOX_DEBUG_IMAGE ) .withTag (seleniumVersion );
253241 default :
254242 throw new UnsupportedOperationException ("Browser name must be 'chrome' or 'firefox'; provided '" + browserName + "' is not supported" );
255243 }
@@ -280,10 +268,6 @@ public int getPort() {
280268
281269 @ Override
282270 protected void containerIsStarted (InspectContainerResponse containerInfo ) {
283- driver = Unreliables .retryUntilSuccess (30 , TimeUnit .SECONDS ,
284- () -> Timeouts .getWithTimeout (10 , TimeUnit .SECONDS ,
285- () -> new RemoteWebDriver (getSeleniumAddress (), capabilities )));
286-
287271 if (vncRecordingContainer != null ) {
288272 LOGGER .debug ("Starting VNC recording" );
289273 vncRecordingContainer .start ();
@@ -298,7 +282,19 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
298282 *
299283 * @return a new Remote Web Driver instance
300284 */
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+ }
302298 return driver ;
303299 }
304300
0 commit comments