Automatically download OS-specific WebDriver executable
When using the Selenium browser functional testing library, testing with some browsers requires using an operating system-specific executable in addition to the driver .jar file. For example, when running Selenium tests with Chrome you need to have the ChromeDriver executable for the operating system you are using. Internet Explorer also requires a similar executable to run tests with it.
Having to manually download and manage these drivers for each operating systems you run tests on is a pain. Each developer on your team has their own laptop, potentially running different operating systems. Plus your continuous integration environment may be on a different OS.
In the past I used some custom download code in each project I worked on to detect the OS and download the driver. But carrying that download code from project to project is also inconvenient. I’d planned for years to write a small library to do the same thing, but thankfully someone else recently did it already!
The WebDriverManager project by Boni Garcia automatically handles downloading and configuring the drivers for your operating system. And it’s very easy to use.
First, add the dependency in your project’s build file. In this example I’m using Gradle as my build system, so I add this to build.gradle:
dependencies { // For downloading browser-specific drivers that browsers like Chrome and IE require testCompile("io.github.bonigarcia:webdrivermanager:1.4.1") { exclude group: 'org.seleniumhq.selenium' } }
Next, add code to download and configure the driver before the tests run. In this example I’m using Geb to write my tests, so in my GebConfig.groovy I add:
import io.github.bonigarcia.wdm.ChromeDriverManager import org.openqa.selenium.chrome.ChromeDriver environments { chrome { // Download and configure ChromeDriver using https://github.com/bonigarcia/webdrivermanager ChromeDriverManager.getInstance().setup() driver = { new ChromeDriver() } } }
Now when I run my Geb tests in the ‘chrome’ environment, WebDriverManager will automatically download the ChromeDriver executable for my OS and set up the required system property to use it. It’s that easy!
For a full working example of using WebDriverManager with Geb, check out this example project on Github: https://github.com/craigatk/grails3-geb-example
Happy testing!
great soloution. thanks Man.
I am using the above mention concept, but for me getInstance() throwing an error saying: The method getInstance() is undefined for the type ChromeDriverManager.
Great work. Briliant solution, pleasure to use. We happily use it for three months. Thanks!
Hi, but in my testing environment, sometimes, there is poor network. It often poped up the download failed error with this function.
How to fix it?
Hi Boni,
Thanks for this. Its working fine for chrome but i am facing difficulty in firefox, IE and Edge. Could you tell me where am going wrong?
I tried these in my GebConfig file
WebDriverManager.iedriver().setup()
driver = {
def driver = new InternetExplorerDriver()
driver.manage().window().maximize()
return driver
}
WebDriverManager.edgedriver().setup()
driver = {
def driver = new EdgeDriver()
driver.manage().window().maximize()
return driver
}
WebDriverManager.firefoxdriver().setup()
driver = {
WebDriver driver = new FirefoxDriver()
driver.manage().window().maximize()
return driver
}
It is throwing error saying
org.openqa.selenium.WebDriverException: java.net.SocketException: Connection reset
Complete stack trace is provided here
https://github.com/bonigarcia/webdrivermanager/issues/270