Selenium Grid Webdriver Code Example in Java

In our earlier post on Selenium Grid, we’d explained an easier method to download and install the Selenium Grid. Also, the post did teach you how to configure multiple browsers like Firefox, Chrome, and the IE with Selenium Grid. In today’s tutorial, you’ll get to learn how to run parallel tests with Selenium Grid Webdriver. And since we’ll create a multi-node setup in this post using Grid and Remote Webdriver, which can even help in using Selenium for load testing.

We’ll give you the best approach to speed up test execution using Selenium Grid. We’ll make sure you should be able to set up and test, so we’ll try to keep the things simple throughout the post. Also, we’ll provide a fully working Selenium Grid Webdriver code in Java which you can easily run in the Eclipse IDE. If you are a newbie in automation and want to learn Selenium Grid Webdriver, then just go through the step by step process of building a Selenium Webdriver project in Eclipse. It’ll help in ramping up quickly.

You would be amazed to know that even the companies like Saucelabs and BrowserStack are making use of Selenium for load testing. They are using a vast cloud infrastructure and customized Selenium to scale browser automation and load testing. Just imagine that together they are supporting 1000+ browsers and devices for testing using Selenium.

Before we jump to the code section, it’s important for you to understand the use case we’ll cover using the Selenium Grid Webdriver code. In this tutorial, we’ll access a website which does the arithmetic calculations. So, the Selenium Grid Webdriver code will start the three browsers in parallel. Then, it’ll open the web page and process the calculation request at the same time in all three browsers. However, the operation will end up fast. But you would perceive the difference as the Selenium Grid will speed up the tests by running them in parallel.

With Selenium Grid, you not only speed up the test execution but also make it doing the cross-browser testing. That’s the way you can ensure the application is running consistently across different browsers. We recommend you must adapt this tool in regular practice to ease up test execution.

Selenium Grid Webdriver Code Example in Java.

We’ve split up this Selenium Grid tutorial in three parts. And each part has multiple steps so that the code should remain easy to grasp.

1- Writing Selenium Grid Webdriver Code using TestNG in Java.

Step-1:

We will develop a test using TestNG. In the following example, we will launch the browsers on nodes using remote webDriver. It can pass on their capabilities to the driver so that the driver has all information to execute on Nodes.

The code below will read the browser parameter from the <TestNG.xml> file.

Step-1.1: Here is the sample Selenium Grid Webdriver code to run parallel tests.

Step-2:

So, you just need to copy-paste the above in a Java file in the Eclipse IDE. In the next few steps, we’ll tell you how to add the <TestNG.xml> to your code and what to keep in this file.

We use this file to pass the browser parameter and for specifying the test order. So, first of all, create an XML under the project folder in the Eclipse IDE.

Selenium Grid Webdriver Code - 1.2

Selenium Grid Webdriver Code – 1.2

 

Step-3:

Select the <File> option from the <General> menu and press the <Next> button.

Selenium Grid Webdriver Code - 1.3

Selenium Grid Webdriver Code – 1.3

Step-4:

Enter the name of the file as <GridDemo.xml> and press the <Finish> button. By default, its name is <TestNG.xml>.

Selenium Grid Webdriver Code - 1.4

Selenium Grid Webdriver Code – 1.4

Step-5:

You can now see that file <GridDemo.xml> gets created under the project folder as shown below.

Selenium Grid Webdriver Code - 1.5

Selenium Grid Webdriver Code – 1.5

Step-6:

In this section, you can view the contents of the XML file. Please check that we’ve added three tests and put them in a suite. Also, we mentioned <parallel=”tests”>. So that, all the tests could run in parallel.

You’ve now completed the development part of the Selenium Grid Webdriver code. So, what remains is the execution of the tests on multiple browsers. Check out the second part to see the test execution.

 

2- Test Execution.

Step-1:

Select the created XML i.e. <GridDemo.xml>. Right-click and choose <Run As >> TestNG Suite> option.

Selenium Grid Webdriver Code - 2.1

Selenium Grid Webdriver Code – 2.1

Step-2:

Now open the Node where we have launched all the browsers. You will see all of them running simultaneously.

Note: For setting up the hub node, please check our earlier post on download and use Selenium Grid to run multiple browsers.

Selenium Grid Webdriver Code - 2.2

Selenium Grid Webdriver Code – 2.2

 

3- Result Analysis.

Step-1:

After successfully completing the test execution, the result summary gets printed in the console as you can view from the below snapshot.

Selenium Grid Webdriver Code - 3.1

Selenium Grid Webdriver Code – 3.1

Step-2:

Next, to see the status of all tests, Navigate to the <Results of Running Suite> Tab. Here, TestNG displays the result summary in the following manner.

Selenium Grid Webdriver Code - 3.2

Selenium Grid Webdriver Code – 3.2

Step-3:

Finally, let’s check out the auto-generated <TestNG> report. With this, we’ll be able to see the test results in HTML format.

Selenium Grid Webdriver Code - 3.3

Selenium Grid Webdriver Code – 3.3

 

Final Word.

We hope the above tutorial on the “Selenium Grid Webdriver Code Example” would help you in using Selenium Grid in your environment. And you’ll be able to speed up the test execution by running tests in parallel using Selenium Grid. Also, if you decide to use Selenium for load testing, then please share your experience after you implement it.

Hope, you succeed in reaping the full benefits from reading our latest post on the Selenium Webdriver and Selenium Grid.

Thanks to you all for visiting. Please drop your feedback in the comment box section.

 

All the Best,

TechBeamers.

Leave a Reply


*