[Selenium+java] Selenium Grid Tutorial:command Line and JSON Example

Source: Internet
Author: User
Tags assert seleniumhq testng selenium grid guru99

Original url:https://www.guru99.com/introduction-to-selenium-grid.html

What is Selenium Grid?

Selenium Grid is a part of the Selenium Suite this specializes in running multiple tests across different browsers, Operat ing systems, and machines in parallel.

The Selenium grid has 2 versions-the older grid 1 and the newer grid 2. We'll have focus on grid 2 because grid 1 are gradually being deprecated by the Selenium Team.

Selenium Grid uses a hub-node concept where you have run the test on a single machine called a hub, but the EXECU tion'll be do by different machines called nodes.

When to use Selenium Grid?

You should use Selenium Grid if you want to do either one or both of following:

    • Run your tests against different browsers, operating systems, and machines all at the same time. This would ensure the application you be testing is fully compatible with a wide range of BROWSER-O.S combinations.
    • Save time in the execution of your test suites. If you set up Selenium Grid to run, say, 4 tests at a time, then you would is able to finish the whole suite around 4 time s faster.

Grid 1.0 Vs Grid 2.0

Following is the main differences between Selenium Grid 1 and 2.

Grid 1

Grid 2

Selenium Grid 1 has its own remote control, which is different from the Selenium RC server. They is different programs.

Selenium Grid 2 is now bundled with the Selenium Server jar file

You need to install and configure Apache Ant first before your can use Grid 1.

Need to install the Apache Ant in Grid 2.

Can only support Selenium RC commands/scripts.

Can support both Selenium RC and Webdriver scripts.

You can only automate one browser per remote control.

One remote control can automate up to 5 browsers.

Selenium Grid Architecture

Selenium Grid has a Hub and Node Architecture.

The Hub
    • The hub is the central point where you load your tests into.
    • There should only is one hub in a grid.
    • The hub is launched with a single machine, say, a computer whose o.s are Windows 7 and whose browser is IE.
    • The machine containing the hub is where the tests would be a run, but you'll see the browser being automated on the node.
The Nodes
    • Nodes is the Selenium instances that'll execute the tests that's loaded on the hub.
    • There can be is one or more nodes in a grid.
    • Nodes can launched on multiple machines with different platforms and browsers.
    • The machines running the nodes need not being the same platform as that of the hub.

Selenium Grid can is set up in the different ways; One through command line and the other through JSON config file.

How to Set up Selenium Grid? Using Command Line

You'll use 2 machines. The first machine would be is the system that would run the hub while the other machine would run a node. For simplicity, let us call the machine where the hub runs as ' machine A ' while the machine where the node runs would be ' M Achine B. " It is the also important to note their IP addresses. Let us say this machine A have A IP address of 192.168.1.3 while machine B have an IP of 192.168.1.4.

Step 1

Download the Selenium Server from here.

Step 2

You can place the Selenium Server. jar file anywhere in your harddrive. But for the purpose of this tutorial, place it on the C drive of both machine A and machine B. After the doing this, you is now the done installing Selenium Grid. The following steps would launch the hub and the node.

Step 3

    • We are now going to launch a hub. Go to machine A. Using the command prompt, navigate to the root of machine a ' s-c drive, because that is the directory WH Ere we placed the Selenium Server.
    • On the command prompt, type Java-jar selenium-server-standalone-2.30.0.jar-role hub
    • The hub should successfully be launched. Your command prompt should look similar to the image below

Step 4

Another-Verify whether the hub is running are by using a browser. Selenium Grid, by default, uses machine A's port 4444 for its web interface. Simply open up a browser and go to Http://localhost:4444/grid/console

Also, can check if machine B can access the hub's web interface by launching a browser there and going to where "iporh Ostnameofmachinea "should be the IP address or the hostname of the machine where the hub is running. Since Machine A's IP address is 192.168.1.3 and then on the browser on machine B you should type Http://192.168.1.3:4444/grid /console

Step 5

    • Now, the hub was already set up, we were going to launch a node. Go to Machine B and launch a command prompt there.
    • Navigate to the root of drive C and type the code below. We used the IP address 192.168.1.3 because that's where the hub is running. We also used port 5566 though you could choose any free port number to you desire.
    • Note:you now has to give path to the Gecko driver if using Firefox. Here is updated code, needs to be used
Java-dwebdriver.gecko.driver= "C:\geckodriver.exe"-jar selenium-server-standalone-3.4.0.jar-role Webdriver-hub Http://192.168.1.3:4444/grid/register-port 5566

    • When you press Enter, your command prompt should is similar to the image below.

Step 6

Go to the Selenium Grid Web interface and refresh the page. You should see something like this.

At the this point, you are already configured a simple grid. You is now the ready-to-run a test remotely on machine B.

Designing Test Scripts that Can Run on the Grid

To design test scripts that would run on the grid, we need to use desiredcapabilites and the remotewebdriver objects.

    • desiredcapabilites is used to set the type of browser and OS that we'll automate
    • remotewebdriver is used to set which node (or machine), which our test would run against.

To use the desiredcapabilites object, you must first import this package

To use the remotewebdriver object, you must import these packages.

Using the Desiredcapabilites Object

Go to the Grid's web interface and hover on an image of the browser so want to automate. Take note of the platform, and the browsername showed by the ToolTip.

In this case, the platform are "XP" and the Browsername is "Firefox."

We'll use the platform and the browsername in our webdriver as shown below (for course you need to import the necessary p Ackages first).

Using the Remotewebdriver Object

Import the necessary packages for Remotewebdriver and then pass the Desiredcapabilities object that we created above as a Parameter for the Remotewebdriver object.

Running a Sample Test case on the Grid

Below is a simple webdriver Testng code, can create in Eclipse on machine A. Once you run it, automation'll be is performed on machine B.

The test should pass.

Selenium grid configuration using JSON File:

JSON stands for Javascript Object Notation. It is a standard format used for interchange of data between browser and the Web server. Selenium has a in Built JSON config file The can is used to set up Selenium grid.

Below is the steps to configure Selenium grid using JSON config file.

Step 1) Download the code for JSON config file using the below path Https://github.com/SeleniumHQ/selenium/blob/master/java/serve R/src/org/openqa/grid/common/defaults/defaulthub.json

Here is the code

{  "port": 4444,  "Newsessionwaittimeout":-1,  "Servlets": [],  "withoutservlets": [],  "Custom": {} ,  "Capabilitymatcher": "Org.openqa.grid.internal.utils.DefaultCapabilityMatcher",  "Registryclass": " Org.openqa.grid.internal.DefaultGridRegistry ",  " throwoncapabilitynotpresent ": True,  " cleanupcycle ": "  role": "Hub",  "Debug": False,  "Browsertimeout": 0,  "timeout": 1800}

Step 2) Copy and paste the code on a text editor such as Notepad with the extension '. JSON '

Step 3) Launch the hub using the below command using command prompt

Java-jar selenium-server-standalone-2.53.1.jar-role Hub-hubconfig Hubconfig.json

Note: Selenium stand alone jar file and the JSON file must be present on the same directory

Step 4) The below screen would appear which indicates the hub is set up successfully

Step 5) Open Web browser and try connecting to the below URL

Http://192.168.43.223:4444/grid/console

Note: The URL may vary from machine to machine. URL followed by ' Nodes should registers to ' on the above-screen must is used.

Step 6) Grid Console screen would appear which indicates the hub is up and running

Configure NODES using JSON:

Step 1) Download The sample node configuration file provided by Selenium using the below URL Https://github.com/SeleniumHQ/seleniu M/blob/selenium-2.53.0/java/server/src/org/openqa/grid/common/defaults/defaultnode.json

Note: IF You is USING SELENIUM 3.0 OR ABOVE, the BELOW JSON FILE must be used

https://github.com/SeleniumHQ/selenium/blob/master/java/server/src/org/openqa/grid/common/defaults/ Defaultnodewebdriver.json

Step 2) Copy and paste the code into a text editor such as Notepad and save the file with the extension '. JSON '

Step 3)

Run the below command on command prompt

Java-dwebdriver.chrome.driver= "Chromedriver.exe"-dwebdriver.ie.driver= "IEDriverServer.exe"- Dwebdriver.gecko.driver= "Geckodriver.exe"-jar selenium-server-standalone-2.53.1.jar-role node-nodeconfig Node1.json

Note: Before running the command, please do sure the drivers for each of the browsers has been downloaded onto local machine Directory

Step 4) The below screen would appear when enter key is pressed

Step 5) Navigate to the grid console to verify if the nodes has been successfully configured

The browsers is displayed on the grid console. Hence we can is sure that nodes is configured successfully.

Sample Test Cases on Selenium Grid:

 Package Com.objectrepository.demo; Import org.openqa.selenium.*; Import org.openqa.selenium.remote.DesiredCapabilities; Import java.net.MalformedURLException; Import Java.net.URL; Import Org.openqa.selenium.remote.RemoteWebDriver; Import Org.testng.Assert; Import org.testng.annotations.*;     public class Seleniumgrid {Webdriver driver;     String BaseURL, Nodeurl; @BeforeTest public void SetUp () throws malformedurlexception {BaseURL = "Http://demo.guru99.com/test/guru99ho         Me/";         Nodeurl = "Http://192.168.43.223:4444/wd/hub";         Desiredcapabilities capability = Desiredcapabilities.chrome ();         Capability.setbrowsername ("Chrome");         Capability.setplatform (PLATFORM.WIN10);     Driver = new Remotewebdriver (new URL (Nodeurl), capability);     } @AfterTest public void Aftertest () {driver.quit ();         } @Test public void Sampletest () {driver.get (BaseURL);         Driver.get (BaseURL); if (Driver.getpagesource(). Contains ("Mobile testing")) {Assert.asserttrue (True, "mobile testing Link Found");         } else {Assert.asserttrue (false, "failed:link not Found"); }     } }

The above code launches Chrome browser and navigates to the URL specified in the ' BaseURL ' variable. It verified a link name ' Mobile testing ' is displayed on the page

The URL on the ' nodeurl ' variable can is modified to reflect the IP Address of the remote machine. Test result can be verified on the default TestNG report generated

Summary
    • Selenium Grid is used to run multiple tests simultaneously on different browsers and platforms.
    • Grid uses the Hub-node concept.
    • The hub is the central point wherein you load your tests.
    • Nodes is the Selenium instances that'll execute the tests that's loaded on the hub.
    • To install Selenium Grid, need to download the Selenium Server jar file-the same file used in running Selenium RC tests.
    • There is 2 ways to verify if the hub is Running:one were through the command prompt, and the other were through a browser
    • To run test scripts in the Grid, you should use the Desiredcapabilities and the Remotewebdriver objects.
    • Desiredcapabilites is used to set the type of browser and OS that we'll automate
    • Remotewebdriver is used to set which node (or machine), which our test would run agains

[Selenium+java] Selenium Grid Tutorial:command Line and JSON Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.