JUnit 5 – When to use the External MethodSource
January 14, 2024JUnit 5 – When to use ArgumentsSource
February 6, 2024The past
In the past, to use Selenium to create the test automation for your frontend tests, it was necessary to use the browser drivers, which are a set of files created by the browser vendor to send the commands from your code to the browser.
The drivers were introduced into Selenium 2, which was a project created by Simon Stuard at Google at the same time as Selenium (version 1). If you are interested in its history, take a look at the Selenium History (a page from 2009! thanks to the web.archive.org). This page also tells you, in short, the history of Selenium 1 until 3.
Before Selenium 4, the current version when this article was written, to be able to run your tests using any supported browser we needed to:
- download the browser driver compatible with the same version of the driver installed in your machine
- explicitly set a property in your code
This is a sample code we were using in the past:
public class Selenium2Test {
@Test
public void runChromeTests() {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
}
}
Line 5 shows that you, first, need to set a specific environment variable to associate the browser driver location, so you could be able to create a new instance of WebDriver
for its browser (line 6).
You can see that a common problem was the management of these drivers as they should be compatible with the local browser, and to give it good coverage you could end up with 5 different drivers to manage:
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); // Google Chrome
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver"); // Firefox
System.setProperty("webdriver.edge.driver", "/path/to/msedgedriver"); // Edge (before the adoption of Chromium)
System.setProperty("webdriver.opera.driver", "/path/to/operadriver"); // Opera
System.setProperty("webdriver.ie.driver", "C:/path/to/IEDriverServer.exe"); // Internet Explorer
The (good) workaround
Boni Gacia created a tool called WebDriverManager with a good motivation: effortless download, setup, and maintenance of the browser drivers.
So, instead of you doing it, the library will do it for you. Look how easy is the solution using Google Chrome:
class WebDriverManagerExample {
void chromeTest() {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
}
}
Line 4 adds the magic of:
- auto discovering the local browser version
- download the browser driver for the matching browser version
- setting the system property
You would end up only using one line of code to run the tests for any browser, and yes, WebDriverManager still supports “legacy” browsers such as Opera, Internet Explorer, and Edge:
WebDriverManager.chromedriver().setup();
WebDriverManager.firefoxdriver().setup();
WebDriverManager.edgedriver().setup();
WebDriverManager.operadriver().setup();
WebDriverManager.chromiumdriver().setup()
WebDriverManager.iedriver().setup();
You can still rely on WebDriverManager, as I’m relying on it to run the web test automation using Safari. You can see one example in the BrowserFactory class from the selenium-java-lean-test-architecture.
The Selenium Manager!
Selenium Manager was released and announced on November 4, 2022, inspired by open-source solutions for the browser driver management problem, including WebDriverManager. The funny thing is that Boni Garcia, the creator of the WebDriverManager is the lead developer of Selenium Manager.
You can use this command-line tool to manage any aspect of the browser drivers.
It’s part of the Selenium ecosystem along with Selenium IDE (yes, it still exists!), Selenium WebDriver and Selenium Grid.
A good thing is that you don’t even need to manage the browser drivers using Selenium 4: it does it automatically for you like the WebDriverManager!
So, instead of having this:
class WebDriverManagerExample {
void chromeTest() {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
}
}
You can simply have this:
class SeleniumManagerTest {
void managerTest() {
WebDriver driver = new ChromeDriver();
}
}
Note that the difference is the absence of any browser driver configuration: you can create an instance of the browser and use it! Currently, it supports Google Chrome, Firefox, and Edge.
Some notes
Example using the Factory Pattern
You can see the easy application of a mixed approach in the selenium-java-lean-test-architecture in the BrowserFactory class: Chrome and Firefox just instantiate the browser as the project is using Selenium 4.
GitHub Actions will run your tests
If you are using GitHub actions to run your tests, there are no additional actions as adding a container that contains a browser: the hosted GitHub runners ubuntu-latest, windows-latest, and macos-latest already include browsers and even the Selenium Drivers. Click on each link to see the current versions.
4 Comments
Hi, this is good Information.
Can you share any article releted to POM pattern in selenium.
Sure.
I don’t have any, yet, but the [selenium-java-lean-test-architecture](https://github.com/eliasnogueira/selenium-java-lean-test-architecture/) shows it using the POM pattern using also the Page Factory.
How do you address when testing in a controlled environment (i.e., when you do not want Selenium Manager not to surreptitiously update the test machine with latest drivers without you knowing)?
Thank you for your question, Robert.
You can control in in both “libraries”.
Using Selenium Manager, already there for the newest Selenium versions, uses the auto discovery feature, as described here, but you can use the
--driver-version
to set the same version of your local browser.If you want to control it via code, I recommend you still using the WebDriverManager. It has a resolution algorithm, which I suspect is the same in the Selenium Manager as the committer is the same. But you can also set it via code by using the browser versions approach where you need to use the method
browserVersion()
.