Selenium Grid
Muhammad Awais Rathore
IT Consultant at Remote Estimation LLC | CTO at MAYIMTECH | Ph.D. Scholar
Selenium Grid is part of the Selenium project. It lets you distribute test execution across several machines. You can connect to it with Selenium Remote by specifying the browser, browser version, and operating system you want. You specify these values through Selenium Remote's Capabilities.
There are two main elements to Selenium Grid -- a hub, and nodes. First you need to stand up a hub. Then you can connect (or "register") nodes to that hub. Nodes are where your tests will run, and the hub is responsible for making sure your tests end up on the right one (e.g., the machine with the operating system and browser you specified in your test).
Setting Up Selenium Grid:
- Download latest version of selenium stand-alone server from here.
- Then we need to start the hub.
- For starting hub just open cmd. You should have java install before configuring selenium grid. I am assuming that you have Java installed. For checking whether java is installed or not type java -version .
java -jar selenium-server-standalone-2.47.1.jar -role hub
It will launch a hub on your local host and on port 4444. To view grid console simply go to browser and type https://localhost:4444/grid/console
Repeat same process for creating node ,this time change the command to something like this. In this we have define the hub in which node will lie so the command will look like
java -jar selenium-server-standalone-2.47.1.jar -role node -hub http:localhost:4444/grid/register
This will register your node with the hub. In case you are creating node on different machine simply replace localhost with your server name.
Keep in mind that you need to configure and launch selenium standalone server on both machine(hub & node).
Now you will see your console looks like this having 5 instances for Firefox and chrome and 1 instance for IE.
Now you good to write code & run on nodes. Hub will auto decide if you have more than one nodes connected to hub. Hub will decide and pass selenium commands to the appropriate node depending upon the desired capabilities defined in your test scripts to run those selenium command.
Setting up the selenium scripts: the Remotewebdriver object
Now we have the grid set up, we need the tests. This tutorial makes a few assumptions: you know how to, and have already written, some functional webdriver tests in C# bindings that use Nunit as the runner. If this is not the case you may need to take a step back and go through some quick and easy examples of this before moving on.
Here is the code in full:
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Remote;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit;
using NUnit.Framework;
namespace TestOfGrid
{
[TestFixture]
public class Driver
{
IWebDriver driver;
[SetUp]
public void Setup()
{
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.Firefox();
capabilities.SetCapability(CapabilityType.BrowserName, "firefox");
capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows));
driver = new RemoteWebDriver(new Uri("https://localhost:4444/wd/hub"), capabilities);
}
[TearDown]
public void Teardown()
{
driver.Quit();
}
[Test]
public void GoogleSearch()
{
string homepage = "https://www.google.co.uk";
//Navigate to the site
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
driver.Navigate().GoToUrl(homepage);
}
}
}
A little breakdown of what we added:
The additional import using OpenQA.Selenium.Remote; was given at the top to give us access to the classes DesiredCapabilities and RemoteWebDriver.
In our Setup we added two objects, an instance of DesiredCapabilities named Capabilities and an instance of RemoteWebDriver named driver.
DesiredCapabilities as you might have inferred from the name allows us to state what we want the target node to have when running the test. For instance, we stated we want the browser Firefox:
capabilities.SetCapability(CapabilityType.BrowserName, "firefox");
On the ‘platform’ (or OS) Windows:
capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows));
These are two examples of CapabilityType – experiment yourself with the other proerties. This will dictate which node on the grid the hub chooses to assign the test to.
The RemoteWebDriver class invokes a webdriver, locates a hub on a selenium grid to use and feeds the the hub the properties:
driver = new RemoteWebDriver(new Uri("https://localhost:4444/wd/hub"), capabilities);
Conclusion:
Now you can run your test and will see that you hub will invoke a proper node and run tests on that node and will return the status at the end of execution to the scripts.
Senior Full Stack Developer
8 年informative
IT Consultant at Remote Estimation LLC | CTO at MAYIMTECH | Ph.D. Scholar
8 年Shams Hussain Sharjeel KhawajaRaheel Afzal