Friday 5 September 2014

Distributed Testing with Selenium Grid



Setting up Selenium Grid Server for parallel execution Adding Windows/IE node to Selenium Hub
Adding Mac/Firefox node to Selenium Hub Adding iPhone/iWebDriver node to Selenium Hub Adding Android node to Selenium Hub Creating and executing Selenium script in parallel with TestNG Creating and executing Selenium script in parallel with Python Setting up Selenium with Jenkins CI for parallel execution
Organizations are adopting virtualization and cloud-based technologies to reduce costs and increase efficiency. Virtualization and cloud-based infrastructure can also be used to scale the test automation by reducing investment in physical hardware needed to set up the test environment. Using these technologies, web applications can be tested on a variety of browser and operating system combinations. Selenium WebDriver has unmatched support for testing applications in virtual environment, executing tests in parallel, reducing costs, and increasing speed and coverage. This chapter will cover recipes to configure and execute Selenium WebDriver tests for parallel or distributed execution.

Distributed Testing with Selenium Grid
Running tests in parallel requires two things: an infrastructure to distribute the tests and a framework that will run these tests in parallel in the given infrastructure. In this chapter, we will first create a distributed infrastructure and then create some tests, which will be executed in this distributed test environment.
Selenium Grid transparently distributes our tests across multiple physical or virtual machines so that we can run them in parallel, cutting down the time required for running tests. This dramatically speeds up testing, giving us quick and accurate feedback.
With Selenium Grid, we can leverage our existing computing infrastructure. It allows us to easily run multiple tests in parallel, on multiple nodes or clients, in a heterogeneous
environment where we can have mixture of OS and Browser support. Here is an example of the Selenium Grid architecture having capabilities to run tests on Linux, Windows, Mac, iOS, and Android platforms:

Selenium Grid allows us to run multiple instances of WebDriver or Selenium Remote Control in parallel. It makes all these nodes appear as a single instance, so tests do not have to worry about the actual infrastructure. Selenium Grid cuts down on the time required to run
Selenium tests to a fraction of the time that a single instance of Selenium would take to run and it is very easy to set up and use.
The selenium-server-standalone package includes the Hub, WebDriver, and Selenium RC needed to run the grid.

In this chapter, we need a testing framework that supports parallel runs of our tests. We will use TestNG (http://testng.org/) for running parallel tests with Selenium WebDriver Java bindings. TestNG has a threading model to support running multiple instances of a same test using an XML-based configuration. TestNG is pretty much similar to JUnit.
We will also use a raw method to run tests in parallel for Python bindings using the subprocess module. However, you can also use nose for parallel execution in Python.
For running test in parallel with .NET bindings, you can use PUnit or MSTEST, and for Ruby, you can use DeepTest.

For running Selenium WebDriver tests in parallel, we need to set up the Selenium Grid Server as a hub. This hub will provide the available configurations or capabilities to the Selenium WebDriver tests. The slave machines, also called as node, connect to hub for parallel execution. Selenium WebDriver tests use the JSON wire protocol to talk to the Hub |for executing Selenium commands.
The Hub acts like the central point, which will receive the entire test request and distribute it to the right nodes.
In this recipe, we will set up a Selenium Grid Server and then add nodes with different OS and browser combinations, as shown in the following figure. We will then use this setup to run tests in parallel using TestNG.
Download the latest Selenium Server standalone JAR file from  http://code.google.  com/p/selenium/downloads/list. For this recipe, selenium-server-standalone-2.25.0 version is used.

Setting up a Hub is a fairly simple job. Launch the Selenium Server using the following command:
java -jar selenium-server-standalone-2.25.0.jar -port 4444 -role hub -nodeTimeout 600
This command will start the Selenium Server in Hub role with the following output on the command prompt:

When we launch the Selenium Standalone Server in Hub role, it starts listening to nodes and Selenium tests on port 4444. If you browse to http://localhost:4444/grid/console on the Hub machine, it will display the following information in the browser:

After clicking on the view config link, it will display the configuration details of the Hub,

For running tests with Selenium Grid, Selenium WebDriver tests need to use the instance of the RemoteWebDriver and DesiredCapabilities classes to define which browser, version, and platform tests will be executed on. Based on preferences set in the
DesiredCapabilities instance, the Hub will point the test to a node that matches these preferences. In this example, Hub will point the test to a node running on the Windows operating system and Firefox browser with the specified version:
DesiredCapabilities cap = new DesiredCapabilities(); cap.setBrowserName("firefox"); cap.setVersion("9.0.1"); cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);

These preferences are set using the setBrowserName(), setVersion(), and setPlatform() methods of the DesiredCapabillities class.
An Instance of the DesiredCapabilities class is passed to RemoteWebDriver:
driver = new RemoteWebDriver(new URL("http://192.168.1.100:4444/wd/hub"),cap);
In this example, the test is connecting to the Hub running on  http://192.168.1.100:4444/  wd/hub with the instance of DesiredCapabilities named cap.

For running tests in parallel, we will now add nodes to the Selenium Hub that we set up in the previous recipe. It is recommended we test our web applications with major OS platforms and browsers.
With Microsoft Windows being a widely used OS along with Internet Explorer, let us add a node with Microsoft Windows and Internet Explorer capabilities to the Hub, so that tests can provision this combination and test the application.

We can add a Windows/Internet Explorer node to the Hub in two ways. First, supplying all the options as command line parameters described as follows:

1.     To start and register a node to the Hub, use the following command by specifying browser type, version, platform, hostname, and port as arguments:

java -jar selenium-server-standalone-2.25.0.jar -role webdriver -browser "browserName=internet explorer,version=8,maxinstance=1,pl atform=WINDOWS" -hubHost localhost –port 8989

  1. Alternatively, a node can be added by creating a configuration file in JSON format and then using the command:
{

"class": "org.openqa.grid.common.RegistrationRequest", "capabilities": [
{

"seleniumProtocol": "WebDriver", "browserName": "internet explorer", "version": "8",
"maxInstances": 1, "platform" : "WINDOWS"
}
],

"configuration": { "port": 8989, "register": true,
"host": "192.168.1.100",
"proxy": "org.openqa.grid.selenium.proxy. DefaultRemoteProxy",
"maxSession": 2, "hubHost": "192.168.1.100", "role": "webdriver", "registerCycle": 5000,
"hub": "http://192.168.1.100:4444/grid/register", "hubPort": 4444,
"remoteHost": "http://192.168.1.101:8989"
}
}
  1. We can now pass the selenium-node-win-ie8.cfg.json configuration file through command-line arguments shown as follows:
java -jar selenium-server-standalone-2.25.0.jar -role webdriver -nodeConfig selenium-node-win-ie8.cfg.json

An instance of Selenium Standalone Server is launched with WebDriver protocol on the port specified in the configuration; in this example node, we will start running on
http://192.168.1.101:8989.
After a successful start, the node registers itself to the Hub. If you navigate to the Grid's console on the hub, that is, http://localhost:4444/grid/console, you will see Internet Explorer in the list of available nodes, as shown

To register a node to the Hub, we need to supply browser details such as name of the browser, version, OS platform, port of the node machine, and so on.
When the node is launched, it connects the Hub on port 4444 providing the capabilities based on the configuration. For Internet Explorer, restrict the maxInstance property to 1 only.

In this example, we registered the node supporting the WebDriver protocol by using the role parameter. We can also register this node to the Hub as a legacy Selenium RC node by specifying the value node for the role parameter:
java -jar selenium-server-standalone-2.25.0.jar -role node -browser "browserName=internet explorer,version=8,maxinstance=1,platform=WINDO WS" -hubHost localhost –port 8989
Similar to the Windows and IE mix, we will add a node combination by using Mac OS X and Firefox and register this to the Hub.
The steps are pretty much similar to what we did in the previous recipe.

Similar to Windows and Internet Explorer, a node for Mac OS X and Firefox can be added to the Hub in two ways. First, supplying all the options as command line parameters as described in the following steps:
1.     To start and register the node to the Hub, use the following command-line options:
java -jar selenium-server-standalone-2.25.0.jar -role webdriver -browser "browserName=firefox,version=9.0.1,maxinstance=1,platform =MAC" -hubHost localhost –port 8989
  1. In another way a node can be added by creating a configuration file in JSON format and then using the command:{
"class": "org.openqa.grid.common.RegistrationRequest", "capabilities": [
{
"seleniumProtocol": "WebDriver", "browserName": "firefox", "version": "9.0.1",

"maxInstances": 1, "platform" : "MAC"
}
], "configuration": {
"port": 8989, "register": true, "host": "192.168.1.100",
"proxy": "org.openqa.grid.selenium.proxy. DefaultRemoteProxy",
"maxSession": 2, "hubHost": "192.168.1.100", "role": "webdriver", "registerCycle": 5000,
"hub": "http://192.168.1.100:4444/grid/register", "hubPort": 4444,
"remoteHost": "http://192.168.1.102:8989"
}
}
  1. We can now pass the selenium-node-mac-ff901.cfg.json configuration file through the following command arguments:
java -jar selenium-server-standalone-2.25.0.jar -role webdriver -nodeConfig selenium-node-mac-ff901.cfg.json
 After a successful start, the node registers itself to the Hub. Navigate to Grid's console on the Hub. An entry for Firefox will appear in the list of available nodes along with the Internet
Explorer node added in the previous recipe


Donate:

Please Donate the some money (anything do you want) for my
blog if you beneficial for this, I will provide more real example for the
latest technique for whom who wants to make a carrier in IT field or solved
some problem, My Name is – RITESH KUMAR SINGH A/C number- 913010044116345 AXIS
Bank LTD :- Vaishali NCR, India-001
 

No comments:

Post a Comment