Handling WebDriver Dependencies
One of the challenges we face while automating UI test cases is downloading the web driver’s dependencies (browser drivers). What if I told you there was an easier way to get rid of these drivers and let the automation script do it for you! Confused?!
This blog is all about that. Follow this blog to the end and you should be able to work out magic on your end(without browser drivers).
Let me start this blog by going over the actual process of using browser drivers for UI test cases. The steps that we usually follow are close to the steps listed below (For this example I am using chrome browser, other browsers follow a similar process)
1.Download chrome driver depending on your browser version which is installed on your local machine. (You can download the driver based on the platform you are using. My chrome browser version is 94.0 running on Windows, so I’ve downloaded chromedriver_win32.zip)
2.Create a maven project now. I have created one which looks something like this.
3.Add the maven selenium dependency inside pom.xml. You can find it here — Selenium 3.141.59. Once you add it your pom.xml should look like this.
4.To invoke the browser from our selenium scripts, we need the location of the drivers that we downloaded in step 1. The location then has to be specified in the code so that selenium knows the location of the browser driver. The easiest way to get the location of the browser driver is to place the driver inside the project and then use the system property to fetch the location of the project folder. This can be done as follows.
System.setProperty(“webdriver.chrome.driver”, System.getProperty(“user.dir” + “/drivers/chromedriver.exe”))
5.The last step is to invoke the browser with selenium code and open the required website. When you run the below code, you should now see a Chrome browser opened.
WebDriver driver = new ChromeDriver(); driver.get(“https://www.google.com”);
With all the pain endured, it might not seem that bad.
Now, let’s replicate the same steps and try to open the chrome browser without browser drivers (Are you ready to see some magic?!!). The steps are as shown below.
Create a separate maven project for this. I have created one as below.
Just as we did previously, we will add the maven selenium dependency inside tom.xml
Now comes the beautiful part.
We will not be downloading the browser drivers this time instead, we will add a maven dependency called WebDriver Manager (link below) which will take care of the steps we followed above, like
a. Downloading the browser driver
b. Placing it inside the project
c. Setting system property for browser driver
Maven Dependency — WebDriver Manager. Once you add it to pom.xml it should look something like this.
Add the following code to invoke the chrome browser
WebDriverManager.chromedriver.setup(); WebDriver driver = new ChromeDriver(); driver.get(“https://www.google.com”);
If you want to invoke Firefox browser, the code would be as follows
WebDriverManager.firefoxdriver.setup(); WebDriver driver = new FirefoxDriver(); driver.get(“https://www.google.com”);
If you want to invoke Edge browser, the code would be as follows
WebDriverManager.edgedriver.setup(); WebDriver driver = new EdgeDriver(); driver.get(“https://www.google.com”);
If you want to invoke Safari browser, the code would be as follows
WebDriverManager.safaridriver.setup(); WebDriver driver = new SafariDriver(); driver.get(“https://www.google.com”);
Run the above code and BANG!
There it is, a chrome browser opened like a charm.
The days of pain are long gone, where we have to worry about -
- Browser driver version
- Getting the location of the browser driver (or) placing the browser driver inside the project folder. (Same has to be done when integrating the scripts in jenkins)
- Setting the system property of chrome driver
Think of the difference that one dependency has created with or without CI/CD integration.
Which do you think is easier? Well, I say automate everything that is possible!
That’s all for now. Before I end this blog, I have one thing to say to all the QA folks out there.