1. Problem
When running a UI test using selenium, it is very common to face driver compatibility issues. For example, the local machine installed browser version is 114 and chromedriver
loaded with the library is expecting browser version 112.
The error indicates that the used browser driver version and the installed browser are not compatible. The SessionNotCreatedException exception trace looks like this:
Caused by: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500.
Message: session not created: This version of ChromeDriver only supports Chrome version 112
Current browser version is 114.0.5735.110 with binary path C:\Program Files\Google\Chrome\Application\chrome.exe
Host info: host: 'LAPTOP-7JGBN7BP', ip: '192.168.0.130'
Build info: version: '4.8.1', revision: '8ebccac989'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
2. Solution
There are quite a few solutions to fix this compatibility issue such as downloading the compatible driver version from the respective download pages (chrome drivers, firefox drivers or Edge drivers) that match the installed browser version. Also, we can uninstall and then install the compatible browser versions also.
But the above solutions may not work for a long time as and when we update the browsers either manually or through automatic updates. In corporate machines, we even may not know that the browser has been updated.
To properly fix this issue, one good way is to use the WebDriverManager library. It is an open-source Java library that carries out the management (i.e., download, setup, and maintenance) of the drivers required by Selenium WebDriver in a fully automated manner. Its latest version provides other relevant features, such as the capability to discover browsers installed in the local system, running browsers in Docker containers seamlessly, and monitoring capabilities.
2.1. Maven
Include the latest version of io.github.bonigarcia:webdrivermanager from the Maven repo.
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.3</version>
<scope>test</scope>
</dependency>
We can also use it in a Gradle project:
dependencies {
testImplementation("io.github.bonigarcia:webdrivermanager:5.3.3")
}
2.2. Usage
To automate the driver management, we need to select a given manager in the WebDriverMager API (e.g., chromedriver()
for Chrome) and invoke the method setup()
.
@BeforeAll
static void setup() {
WebDriverManager.chromedriver().setup();
}
When executed, the setup() method tries to find the browser version installed on the machine. Then, using the browser version, it tries to find the proper driver version through various methods. Once the driver version is discovered, WebDriverManager downloads the driver to a local cache (located at ~/.cache/selenium
) and exports the driver path using Java system properties. These drivers are reused in subsequent calls.
Similarly, we can setup the drivers for Firefox, Edge, Opera, Chromium, and Internet Explorer using one of the following statements as follows:
WebDriverManager.firefoxdriver().setup();
WebDriverManager.edgedriver().setup();
WebDriverManager.operadriver().setup();
WebDriverManager.chromiumdriver().setup()
WebDriverManager.iedriver().setup();
2.3. Example
The following is a JUnit 5 test for launching the browser, opening the Google home page, and asserting its title.
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class AppTest {
static WebDriver browser;
@BeforeAll
static void setup() {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setHeadless(false);
options.addArguments("start-maximized"); // open Browser in maximized mode
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-gpu"); // applicable to Windows os only
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox"); // Bypass OS security model
options.addArguments("--disable-in-process-stack-traces");
options.addArguments("--disable-logging");
options.addArguments("--log-level=3");
options.addArguments("--remote-allow-origins=*");
browser = new ChromeDriver(options);
}
@Test
@DisplayName("The google.com web site should have the correct title")
void testProjectWebSiteShouldHaveCorrectTitle() {
browser.get("https://google.com/");
Assertions.assertEquals("Google", browser.getTitle());
}
}
When we run the test, it loads the correct Chrome driver, launches the Chrome version, opens the Google home page and verifies the webpage title successfully.
Happy Learning !!
Comments