-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatic selenium version matching #97
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.util.Enumeration; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.jar.Attributes; | ||
import java.util.jar.Manifest; | ||
|
||
/** | ||
* Utility methods for Selenium. | ||
*/ | ||
public class SeleniumUtils { | ||
|
||
public static final String DEFAULT_SELENIUM_VERSION = "2.45.0"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't it be 2.52.0 now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, in that 2.52.0 is the latest available version :D. However, a major feature of Testcontainers' selenium support is that versions of browsers are stable and users should not be afflicted by sudden breakage when a browser version silently changes in the background. The automatic detection of classpath selenium version should make this a moot point, as finally there's only one single source of the truth for the version the user wants to use. If for some reason this automated detection fails, though, I wouldn't want to trip up a user who had been happily using v2.45.0, so unfortunately the default needs to be the lowest common denominator rather than the latest version. The best case is the user gets exactly the version of the container images they need (matching their API JAR). The worst case is they get the older version they would have had to deal with anyway, hopefully :) |
||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SeleniumUtils.class); | ||
|
||
private SeleniumUtils() {} | ||
|
||
/** | ||
* Based on the JARs detected on the classpath, determine which version of selenium-api is available. | ||
* @return the detected version of Selenium API, or DEFAULT_SELENIUM_VERSION if it could not be determined | ||
*/ | ||
public static String determineClasspathSeleniumVersion() { | ||
Set<String> seleniumVersions = new HashSet<>(); | ||
try { | ||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | ||
Enumeration<URL> manifests = classLoader.getResources("META-INF/MANIFEST.MF"); | ||
|
||
while (manifests.hasMoreElements()) { | ||
URL manifestURL = manifests.nextElement(); | ||
try (InputStream is = manifestURL.openStream()) { | ||
Manifest manifest = new Manifest(); | ||
manifest.read(is); | ||
|
||
Attributes buildInfo = manifest.getAttributes("Build-Info"); | ||
if (buildInfo != null) { | ||
String seleniumVersion = buildInfo.getValue("Selenium-Version"); | ||
|
||
if (seleniumVersion != null) { | ||
seleniumVersions.add(seleniumVersion); | ||
LOGGER.info("Selenium API version {} detected on classpath", seleniumVersion); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} catch (Exception e) { | ||
LOGGER.debug("Failed to determine Selenium-Version from selenium-api JAR Manifest", e); | ||
} | ||
|
||
if (seleniumVersions.size() == 0) { | ||
LOGGER.warn("Failed to determine Selenium version from classpath - will use default version of {}", DEFAULT_SELENIUM_VERSION); | ||
return DEFAULT_SELENIUM_VERSION; | ||
} | ||
|
||
String foundVersion = seleniumVersions.iterator().next(); | ||
if (seleniumVersions.size() > 1) { | ||
LOGGER.warn("Multiple versions of Selenium API found on classpath - will select {}, but this may not be reliable", foundVersion); | ||
} | ||
|
||
return foundVersion; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this class could be also made final according to best practices for utility classes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, good point - will do.