Selenium is a tool that helps us to test the UI of web applications on a variety of browsers (Chrome, Firefox, …) completely automatically. It uses the WebDriver concept such as ChromeDriver, FirefoxDriver, RemoteWebDriver … to realize its ideas. Each browser, each different environment will have their own WebDriver. In this tutorial, I will introduce you to Selenium RemoteWebDriver!
Selenium RemoteWebDriver is a WebDriver that helps us to run the test remotely. This means, you can run the test code on your machine, but the code execution will happen on another machine.
To do that, first you have to run Selenium Standalone Server on the machine where you want to execute the test code and this Selenium Standalone Server must be configured to support the browser you want to run the test.
For example, if I want to test the website https://huongdanjava.com/ on Chrome browser using Selenium RemoteWebDriver, my steps will be as follows:
– I will download Selenium Standalone Server from https://docs.seleniumhq.org/download/. Selenium Standalone Server is an executable .jar file.
– Next, I will download ChromeDriver from https://sites.google.com/a/chromium.org/chromedriver/downloads. For this step, you have to check the Chrome version you are using to download ChromeDriver properly for that version.
I am using Chrome version 72.0.3626.109 (Official Build) (64-bit) on macOS so I will download ChromeDriver 72.0.3626.69 for macOS at https://chromedriver.storage.googleapis.com/index.html?path=72.0.3626.69/. The downloaded file will be a .zip file so you need to unzip this file!
– Now I will start Selenium Standalone Server with ChromeDriver with the following command:
1 |
java -Dwebdriver.chrome.driver="/Users/Khanh/Downloads/chromedriver" -jar selenium-server-standalone-3.141.59.jar |
with -Dwebdriver.chrome.driver is the parameter I use to declare the environment variable pointing to ChromeDriver. For different browsers, the environment variable name needs to be different, for example, for Firefox, the environment variable name might be -Dwebdriver.firefox.driver, as long as the value needed to point to FirefoxDriver! If you need Selenium Standalone Server to support multiple browsers, you can declare additional environment variables to WebDriver of those browsers.
selenium-server-standalone-3.141.59.jar is the file name of Selenium Standalone Server, the file name may be different for you.
My results when running the above command:
OK, now you was able to run a Selenium Standalone Server! Next, I will create a new Maven project to see how RemoteWebDriver works!
My project is as follows:
With Selenium Remote Driver dependency as follows:
1 2 3 4 5 |
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-remote-driver</artifactId> <version>3.141.59</version> </dependency> |
In case if you want to use multiple WebDriver, you can declare using Selenium Java dependency as follows:
1 2 3 4 5 |
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> |
For simplicity, I just created a class with main() method to run the example!
OK, now let’s try using Selenium Remote Web Driver to open the https://huongdanjava.com/ website with Chrome browser!
First, you need to initialize RemoteWebDriver object with capabilities for Chrome browser as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.huongdanjava.selenium; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; public class RemoteWebDriverExample { public static void main(String[] args) throws MalformedURLException { DesiredCapabilities capabilities = DesiredCapabilities.chrome(); WebDriver webDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub/"), capabilities); } } |
In which “http://localhost:4444/wd/hub/” is the Web Driver Hub address that we started with Selenium Standalone Server. Here, because I run Selenium Standalone Server on my machine, the value of the host is localhost, if you run it on another machine, you need to change this value!
To open the website https://huongdanjava.com/, you need to call the get() method with a value of https://huongdanjava.com/.
1 |
webDriver.get("https://huongdanjava.com/"); |
OK, let’s try running this example!
You will see a new Chrome window open and accessible to https://huongdanjava.com/ website as follows:
Now you can continue writing the UI test code.