0
# Selenium Version Detection
1
2
The `SeleniumUtils` class provides utilities for detecting the Selenium version from the classpath and ensuring browser image compatibility. This is essential for automatic Docker image selection based on the Selenium API version in use.
3
4
## Imports
5
6
```java
7
import org.testcontainers.containers.SeleniumUtils;
8
import java.util.jar.Manifest;
9
```
10
11
## Version Detection
12
13
### Classpath Selenium Version Detection
14
15
Automatically detects the Selenium version from JARs on the classpath.
16
17
```java { .api }
18
public static String determineClasspathSeleniumVersion();
19
```
20
21
**Returns:** String representing the detected Selenium version, or DEFAULT_SELENIUM_VERSION if detection fails
22
23
**Usage:**
24
```java
25
import org.testcontainers.containers.SeleniumUtils;
26
27
String seleniumVersion = SeleniumUtils.determineClasspathSeleniumVersion();
28
System.out.println("Detected Selenium version: " + seleniumVersion);
29
// Output: "Detected Selenium version: 4.15.0"
30
```
31
32
**Detection Process:**
33
1. Scans all META-INF/MANIFEST.MF files on the classpath
34
2. Looks for Selenium version information in manifest attributes
35
3. Returns the first valid version found
36
4. Falls back to DEFAULT_SELENIUM_VERSION if no version is detected
37
38
### Manifest Version Extraction
39
40
Extracts Selenium version information from a JAR manifest.
41
42
```java { .api }
43
public static String getSeleniumVersionFromManifest(Manifest manifest);
44
```
45
46
**Parameters:**
47
- `manifest`: JAR manifest object to inspect
48
49
**Returns:** String representing the Selenium version, or null if not found
50
51
**Usage:**
52
```java
53
import java.util.jar.Manifest;
54
import java.util.jar.JarFile;
55
56
JarFile jarFile = new JarFile("selenium-api-4.15.0.jar");
57
Manifest manifest = jarFile.getManifest();
58
String version = SeleniumUtils.getSeleniumVersionFromManifest(manifest);
59
```
60
61
**Manifest Attribute Lookup:**
62
The method checks manifest attributes in the following order:
63
1. `Build-Info` section, `Selenium-Version` attribute (legacy format)
64
2. `Selenium` section, `Selenium-Version` attribute (Selenium 3.x+ format)
65
66
## Constants
67
68
### Default Version
69
70
```java { .api }
71
public static final String DEFAULT_SELENIUM_VERSION = "2.45.0";
72
```
73
74
The fallback version used when Selenium version cannot be determined from the classpath.
75
76
## Integration with Browser Containers
77
78
The SeleniumUtils class is automatically used by `BrowserWebDriverContainer` during container configuration to:
79
80
1. **Detect Selenium Version**: Determine the Selenium API version in use
81
2. **Select Compatible Images**: Choose appropriate browser Docker images
82
3. **Handle Version Compatibility**: Ensure browser images work with the detected Selenium version
83
84
### Automatic Image Selection
85
86
Based on the detected Selenium version, the container automatically selects:
87
88
**Selenium 4.x and later:**
89
- Standard images: `selenium/standalone-chrome`, `selenium/standalone-firefox`, `selenium/standalone-edge`
90
- VNC support is built into standard images
91
92
**Selenium 3.x and earlier:**
93
- Debug images: `selenium/standalone-chrome-debug`, `selenium/standalone-firefox-debug`
94
- Edge is not supported in older versions
95
- Separate debug images required for VNC support
96
97
### Version Compatibility Matrix
98
99
| Selenium Version | Chrome | Firefox | Edge | VNC Support |
100
|-----------------|---------|---------|------|-------------|
101
| 4.x+ | ✅ Standard/Debug | ✅ Standard/Debug | ✅ Standard | ✅ Built-in |
102
| 3.x | ✅ Debug only | ✅ Debug only | ❌ Not supported | ✅ Debug images |
103
| 2.x | ✅ Debug only | ✅ Debug only | ❌ Not supported | ✅ Debug images |
104
105
## Error Handling
106
107
### Version Detection Failures
108
109
When version detection fails:
110
1. Warning is logged about using default version
111
2. DEFAULT_SELENIUM_VERSION is returned
112
3. Container continues with fallback configuration
113
114
### Multiple Versions Detected
115
116
When multiple Selenium versions are found on classpath:
117
1. Warning is logged about version ambiguity
118
2. First detected version is used
119
3. May result in compatibility issues
120
121
### Logging
122
123
The utility class uses SLF4J logging for:
124
- **INFO**: Successfully detected versions
125
- **WARN**: Fallback to default version, multiple versions detected
126
- **DEBUG**: Detailed failure information
127
128
## Usage in Container Configuration
129
130
The version detection is automatically used when creating browser containers:
131
132
```java
133
// Automatic version detection and image selection
134
BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
135
.withCapabilities(new ChromeOptions());
136
137
// The container automatically:
138
// 1. Calls SeleniumUtils.determineClasspathSeleniumVersion()
139
// 2. Selects appropriate Chrome image based on version
140
// 3. Configures VNC recording based on version capabilities
141
```
142
143
## Utility Class Design
144
145
```java { .api }
146
public final class SeleniumUtils {
147
private SeleniumUtils(); // Private constructor - utility class
148
}
149
```
150
151
The class is designed as a utility class with:
152
- Private constructor to prevent instantiation
153
- All methods are static
154
- No instance state
155
- Thread-safe operations