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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
modules/selenium/src/main/java/org/testcontainers/containers/SeleniumUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 final class SeleniumUtils { | ||
|
||
public static final String DEFAULT_SELENIUM_VERSION = "2.45.0"; | ||
|
||
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
shouldn't it be 2.52.0 now?
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.
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 :)