How to start browser for Selenium WebDriver
By: Anton Smirnov
Tags: Selenium, Automation Tools, Automation Testing, Java, Selenoid

I think every QA Automation Engineer is familiar with this message in the console:

org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list

I agree, this problem is solved simply:

System.setProperty("webdriver.chrome.driver", "/path/to/driver");

However, using this method you will encounter several problems:

With this approach, you usually need to download and store the chromedriver in the resources folder. But the big problem is that if we run tests on different platforms, we need to create a different driver factory and write a bunch of additional code.

In this article, I will tell you how to work around this problem. And how Selenium is looking for auxiliary executables or shims.

The auxiliary file for this so-called Selenium driver for the browsers chromedriver(Chrome), geckodriver(Firefox), etc.

The main question to which you will get the answer is where you can put these auxiliary executable files so that Selenium can find them.

When the new DriverBrowser() constructor is called, it finds the proxy file and starts it. Therefore, for each browser you want to use, you need to download an executable helper file. After loading it is necessary to put it somewhere so that Selenium could find it.

To solve this problem, there are 4 ways:

1.Put anywhere and specify the path to this executable file directly in the program code. The client library for the Java programming language offers 2 options to choose from:

To set the system property webdriver.chrome.driver

System.setProperty("webdriver.chrome.driver", "/path/to/driver");

It is necessary to construct a driver :

webDriver = new ChromeDriver(

new ChromeDriverService.Builder()

.usingDriverExecutable(new File("/path/to/driver")).build());

2. Put in the directory that is mentioned in the PATH environment variable.

The PATH environment variable Unix-like operating systems, DOS, OS/2, and Microsoft Windows, which is a set of directories where executable files are located. Basically, each running process or user session has its own path variable.

To find out the contents of this variable in Windows OS, you need to call the command:

echo %PATH%

For Linux OS:

echo $PATH

It will display all directories registered in the PATH variable and the executable file can be put in any of the directories.

Windows can be placed in C:\Windows\system32 or you can create your own directory and include it in variable PATH.

For Linux /usr/local/bin or similar, create your own variable and include it in the PATH variable. Also, do not forget that the files should be marked as executable. That is, a flag should be set that marks special executable files.

The second place where you can put the executable working or current directory. This is the directory that is current when the tests or automation scripts are run. As a rule, this is the root directory of the project in which you are developing.

The logic of the executable search engine is as follows: Initially, it looks for the executable file in the current directory and if the file was not found, the search begins in the directories specified in the PATH environment variable.

3. Use in your project an automated library for managing proxy files or in other words webdrivermanager.

WebDriverManager allows automating the management of the binary drivers (e.g. chromedriver, geckodriver, etc.) required by Selenium WebDriver.

With this library, you can solve a rather banal driver management problem for Chrome, Firefox, or InternetExplorer.

If you use Selenium WebDriver, you will know that to use some browsers such as Chrome, Firefox, Opera, PhantomJS, Microsoft Edge, or Internet Explorer, you need to download a binary file that allows WebDriver to handle browsers. In Java, the path to this binary must be set as JVM properties, as follows:

System.setProperty("webdriver.chrome.driver", "/path/to/driver");

This is quite annoying since it forces you to link directly this binary file into your source code. In addition, you have to check manually when new versions of the binaries are released.

WebDriverManager comes to the rescue, performing in an automated way all this dirty job for you.

To use WebDriverManager from tests in a Maven project, you need to add the following dependency in your pom.xml (Java 8 or upper required):

<dependency>

<groupId>io.github.bonigarcia</groupId>

<artifactId>webdrivermanager</artifactId>

<version>3.6.2</version>

</dependency>

or in Gradle project:

dependencies {

testCompile("io.github.bonigarcia:webdrivermanager:3.6.2")

}

Once we have included this dependency, you can let WebDriverManager manage the WebDriver binaries for you. Take a look at this JUnit 4 example which uses Chrome with Selenium WebDriver (to use WebDriverManager in conjunction with JUnit 5, the extension Selenium-Jupiter is highly recommended):

public class Test {





private WebDriver driver;



@BeforeClass

public static void setupClass() {

WebDriverManager.chromedriver().setup();

}



@Before

public void setupTest() {

driver = new ChromeDriver();

}



@After

public void teardown() {

if (driver != null) {

driver.quit();

}

}



@Test

public void test() {

// Your test code here

}



}

Notice that simply adding WebDriverManager.chromedriver().setup(); WebDriverManager does magic for you:

1. It checks the version of the browser installed in your machine (e.g. Chrome, Firefox).

2. It checks the version of the driver (e.g. chromedriver, geckodriver). If unknown, it uses the latest version of the driver.

3. It downloads the WebDriver binary if it is not present on the WebDriverManager cache (~/.m2/repository/webdriver by default).

4. It exports the proper WebDriver Java environment variables required by Selenium (not done when using WebDriverManager from the CLI or as a Server).

More detailed information about the library on this link

4. Selenium Grid or Selenoid.

Selenium-Grid allows you to run tests on different machines in different browsers in parallel.

In General, Selenium Grid is used for several reasons:

1)To run tests on different operating systems, in different browsers;

2) To reduce the total test run time.

Selenium Grid consists of Hub and Node.

Hub is the Central point that receives requests and routes them to the Node. Such a command post. There can only be one Hub in a grid.

Node is a Selenium instance that will run commands loaded into the Hub. A Node can be a lot in the grid. Node — can run on different operating systems with different browsers.

Let’s get started.:

You need to download Selenium Server Standalone under this link.

Create a seleniumgrid folder and copy the downloaded selenium_server_x.xx to it.x.jar file.

Launch Hub.

in the console, you must run the command:

java -jar selenium_server_standalone__x.xx.x.jar -role hub

To check that everything started correctly, you need to type in the browser:

http://localhost:4444/grid/console

Launch Node.

in the console, you must run the command:

For FireFox :

java -jar selenium-server-standalone-_x.xx.x.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=firefox

Chrome and Internet Explorer have slightly different settings. You need to add the path to the drivers of the respective browsers -Dwebdriver.ie.driver=IEDriverServer.exe and -Dwebdriver.chrome.driver=chromedriver.exe. You can download drivers on the website selenium.org

For Chrome:

java -Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-server-standalone-_x.xx.x.jar -role webdriver -hub

http://localhost:4444/grid/register -port 5558 -browser browserName=chrome

For Internet Explorer:

java -Dwebdriver.ie.driver=IEDriverServer.exe -jar selenium-server-standalone-_x.xx.x.jar -role webdriver -hub

http://localhost:4444/grid/register -port 5558 -browser browserName=iexplorer

To check that everything is configured correctly, you need to open all the same http://localhost:4444/grid/console and make sure that there is displayed one instance IE and one Chrome.

You can configure the Hub and Node using JSON files:

Example for Hub:

java -jar selenium-server-standalone.jar -role hub -hubConfig hubconfig.json

Example for Node:

java -jar selenium-server-standalone.jar -role node -nodeConfig nodeconfig.json

Selenoid is a powerful Go implementation of the original Selenium hub code. It is using Docker to launch browsers. Please refer to the GitHub repository if you need sources.

Selenoid — main advantages

Quick installation

Convenient work with browsers.

Easy scaling.

Convenient UI and logging.

Queues and flexible limits.

Selenoid Setting:

Install Docker

Download Config Manager (12 MB):

$ wget –O cm

https://github.com/aerokube/cm/releases/download/1.6.1/cm_linux_amd64

$ wget –O cm https://github.com/aerokube/cm/releases/download/1.6.1/cm_linux_amd64 $ chmod +x cm

Start Selenoid: $ ./cm selenoid start — vnc

Selenoid runs on standard selenium — 4444 port

Selenoid browser Maintenance:

Ready-made container images with pre-configured browsers.

Each browser in an isolated container.

For each test clean browser, as if just installed in the OS.

Sample Java code to run tests in Selenium Grid or Selenoid:

DesiredCapabilities capability = DesiredCapabilities.chrome();

try {

driver = new RemoteWebDriver(new URL("URL for Seleniun Grid or selenoid"), capability);

} catch (MalformedURLException e) {

LOG.info(" ");

}

}

More detailed information about Selenoid on this link