Selenium Grid Tutorial: Writing first script for Grid using RemoteWebDriver

In previous posts, We have seen how to set up hub and nodes. But still we have not seen how to run your code on some remote machines.But code execution on remote machine could not be possible without RemoteWebDriver. But before moving ahead we need to cast our focus on this RemoteWebDriver.

\  This is implementation class of WebDriver interface which is plays a key role in script execution on some remote machine.RemoteWebDriver is divided in two parts
1- RemoteWebDriver Server
2- RemoteWebDriver Client



RemoteWebDriver Server: This part listens to all the request on a certain port from all the RemoteWebdriver client and on the basis of these request it approaches Firefox, Chrome or IR driver to fulfill the client request. So here when we run this command

java -jar <Selenium-server-stanalone 2.4X.x.jar>

we try to start the remote server.

RemoteWebDriver Client: All the language binding works as RemoteWebDriver client which comes into action when we try to execute any script. This client first tries to translate all the information related to code execution in JSON format and send it to the remoteWebdriver server using JSON wire protocol and then server talks to all the drivers available as per the request.

So now you are familiar with the concept of RemoteWebDriver but how it servers your purpose.
IT Certification Category (English)728x90

As you have already seen that RemoteWebDriver is implementation class of WebDriver and in your Selenium WebDriver script you always create a driver instance that talks to the respective browser as per our code. In the same fashion, you need to create an instance of WebDriver like this

WebDriver driver = new RemoteWebDriver(java.net.URL remoteAddress,
                       Capabilities desiredCapabilities)

where URL is nothing but the it is URL of Hub that we have used in previous post like
Selenium Grid Commandso here URL would be (as per above image)
URL = http://192.168.0.104:4444/wd/hub
Capabilities are nothing but used to provide information about the configuration which contains a type of browser, version and also provide information about the platform where the script should be executed remotely. So let’s see how to it is being used, Suppose you want to execute your code on firefox with version 40.0 and want to run this test on node machine which is Windows
So Capability would be written like this

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
	
	capabilities.setBrowserName("firefox");
	capabilities.setVersion("40.0");
	capabilities.setPlatform(Platform.WINDOWS);

So these are two important components of the script that are going to be executed on the remote machine using Grid concept.

So let’s write the complete code that runs on the Selenium Grid.

1- Start the hub and node.
2- Write the code
Scenario :
a) Open the Firefox browser on remote machine(Node)
b) Navigate to Google and enter string “Selenium Grid Tutorial”  in the search box




So create one class GridExampleTest in your eclipse  under package pack1 and past the following code

package pack1;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;

public class GridExampleTest {

	@Test
	public void test() throws MalformedURLException {
		DesiredCapabilities capabilities = DesiredCapabilities.firefox();

		capabilities.setBrowserName("firefox");
		capabilities.setVersion("40.0");
		capabilities.setPlatform(Platform.WINDOWS);

		RemoteWebDriver driver = new RemoteWebDriver(new URL(
				"http://192.168.0.103:4444/wd/hub"), capabilities);
		driver.get("http://google.com");
		driver.findElement(By.name("q")).sendKeys("Selenium Grid Tutorial");
		driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
	}
}

3- Now run the code.

Above code will execute complete scenario on Windows machine with Firefox browser with version 40.0.
IT Certification Category (English)728x90

This was the first code that will be used for grid example. In the coming post, you will see how you can run your script on multiple machines at the same time and would see how parallel execution of test cases are possible with Selenium Grid.

Dwarika Dhish Mishra

My name is Dwarika Dhish Mishra, its just my name and I am trying to bring the worth of my name in to actions and wants to be the solution not the problem. I believe in spreading knowledge and happiness. More over I am fun loving person and like travelling a lot. By nature I am a tester and a solution maker. I believe in the tag line of http://ted.org “Idea worth spreading” . For the same, I have created this blog to bring more and more learning to tester fraternity through day to day learning in professional and personal life. All contents are the part of my learning and so are available for all..So please spread the contents as much as you can at your end so that it could reach to every needful people in testing fraternity. I am pretty happy that more and more people are showing interest to become the part your Abode QA blog and I think this is good sign for us all because more and more content would be before you to read and to cherish. You may write or call me at my Email id: dwarika1987@gmail.com Cell: 9999978609

You may also like...

6 Responses

  1. Carlos ETG says:

    Hi.

    According to this tutorial, you can setp up a Selenium grid with one hub and one node. In my case, I need one hub and several nodes executing the same test case. How can I run the same test case in several IP adresses (or DNS)?

    Best regards.

    • Thanks Carlos, to connect with hub you need to put selenium standalone jar and register the node with hub the way we have register single node with hub. Hope this will help you to resolve your query

      • Carlos ETG says:

        Hi Dwarika.

        So in the java code, do I need to start a Webdriver variable with one IP Adress (or DNS) for every single node that I want to use?
        …….
        RemoteWebDriver driver = new RemoteWebDriver(new URL(“http://” + IP1 + “/wd/hub”), capabilities);
        RemoteWebDriver driver2 = new RemoteWebDriver(new URL(“http://” + IP2 + “/wd/hub”), capabilities);
        RemoteWebDriver driver3 = new RemoteWebDriver(new URL(“http://” + IP3 + “/wd/hub”), capabilities);
        …..

        and therefore to repeat all the code of the cases for every single of the driver* variables?

        Best regards.

        • For multiple invocation of browser you need to use testng testing framework and to use domain name first go and put host entry in your machine so that when ever you use dns it will take the ip directly

  2. Anand Kumar Muhale says:

    Hi,

    It is a nice article. Thanks for the efforts you put in it.
    It was really helpful.

    Best Regards

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.