Getting started with selenium Grid

1

This post is intended to explain how a selenium distributed test environment can be configured and tests are distributed among the nodes by using selenium grid2.

Prerequisite:

Basic scripting knowledge on selenium webdriver is must. (java in this case)

Terms used:

For better understanding, in few places the selenium hub is referred as server, selenium node is referred as node.

Table of Content:

  1. What is selenium gird?
  2. Downloading and installing selenium grid
  3. Preparing the hub
  4. Preparing node
  5. Declaring the remote webdriver object
  6. Running the grid

1.      What is Selenium grid?

Selenium Grid is a part of the Selenium Suite of software that help us to run the multiple tests across different browsers, machines and different operating systems, in a simultaneous way.  This is called distributed test execution. The distributed test environment is much similar to a client server architecture (though technically it may be differing, this analogy is for sake of understanding).

  • A hub is a server machine which contains the test codes.
  • A node is a client machine which listens to the server.

Whereas, a server contains the test scripts and node contains just a browser setup to execute the test.

Note: Same machine can act as a hub and node.

2.      Downloading and installing selenium grid

2.1 Downloading the grid packages.

       The selenium grid is buddle with selenium server standalone (jar) package which can be downloaded from this link http://code.google.com/p/selenium/downloads/list . At the time of preparing this document the latest version is 2.38.0.You can use the latest always, but make sure the all the jar versions are in sync.

2.2   Installing

There is no separate installation or path setting required for grid2.

3.    Preparing the hub :

Preparing the hub is the process of starting the Grid 2 server (hub). The following is the command.

java -jar selenium-server-standalone-2.38.0.jar -role hub

 

hubSS

The above screen illustrates that the Grid hub is started, we can ensure the same by visiting the URL http://localhost:4444/grid/console. This is will lead us to the hub’s console page.

nodeScreen

4.      Preparing node :

Preparing the node is the process of starting the Grid 2 client and points it towards the server. The following is the command.

java -jar selenium-server-standalone-2.38.0.jar -role node  -hub http://localhost:4444/grid/register

 The below command prompt screen illustrates that the Grid node is started.

node

We can ensure the same by visiting the URL http://localhost:4444/grid/console. This is will lead us to the hub’s console page.

hubScreen

Similarly, we can create n-number of nodes per hub. There are lot of configuration available in creating node. You can choose yours. It’s advisable to create a single node per machine.

Note: In the above example both hub and node is running in a same machine. In case, if you want to register a node from a different machine, you need to give the machine’s exact IP in which the hub is running instead of localhost in the above command.

5.      Declaring the remote webdriver object:

Once the setup is done, it’s time to declare the remote driver objects in order to create distributed tests across nodes. In the test script, we need to create remote webdriver objects instead of typical webdriver object.

The syntax,

WebDriver driver = new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), capability);

That’s all with Grid setup. Whenever we run these test scripts, the server will manage nodes to execute them.

6.      Running the grid

 To run a grid scripts, create a testing suite that can run all your test scripts in parallel. [1].Whenever your entire test script requests the hub/server simultaneously; server will manage with the help of available nodes. If the number of scripts running in parallel is more than no of available nodes, the server will maintain these requests in a queue. Whenever a node becomes idle, the remaining test will be assigned.

Very Well, you are done with Grid2 configurations.  Happy testing.

References: 

[1] TestNG official documentation to run methods in parallel.

 http://testng.org/doc/documentation-main.html#parallel-tests

Boost up your selenium tests with video recording capability.

0

RecordYourTests

Usually, in Automation testing we can’t figure out if the test stopped abruptly in the middle. Though we have some screenshot capabilities native with WebDirver, it is sometimes required to record the whole execution.

Here comes a solution/Workaround for the problem of hanging without knowing the problem with your last step of hour long tests.

Again, I have to make a note on it; we have no native support for doing it. But everything is possible in the world of JAVA.

Recently I had a chance to check out the monte library.(Found it via another blog, forgot the source. I am really bad @ memories).

Download the Libraries from here and add it your project class path. Then Create a Utility class and add the following code.

class TestRecorder

{

public void startRecording() throws Exception

{

GraphicsConfiguration gc = GraphicsEnvironment

.getLocalGraphicsEnvironment()

.getDefaultScreenDevice()

.getDefaultConfiguration();

this.screenRecorder = new ScreenRecorder(gc,

new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI),

new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,

CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,

DepthKey, 24, FrameRateKey, Rational.valueOf(15),

QualityKey, 1.0f,

KeyFrameIntervalKey, 15 * 60),

new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, “black”,

FrameRateKey, Rational.valueOf(30)),

null);

this.screenRecorder.start();

}

public void stopRecording() throws Exception

{

this.screenRecorder.stop();

}

}

Now, call the startRecording method in BeforeTest block and similarly call stopRecording method in  afterTest block.

That’s it. Run your test and it will run as usual and you can find your execution video under My videos folder of current user directory. Well, as per my tries, the outcome of this execution works only with VLC player(though an AVI extension). So you may need a VLC player to play this.

Caution: The file size may eat your HD space. (Of course it is,. Because it records screen fully 😛 )

I will try to post you with necessary screenshots ASAP.

Thanks for reading  🙂

Scroll down to end of the page in Selenium web driver

2

Hi folks,

While dealing with long web pages in our automation testing we use to get the following exception.

“The point at which the driver is attempting to click on the element was not scrolled into the viewport.”

.As I usually need to click a button in the end of the page, I need to scroll down to the end. The following code could help.

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("window.scrollBy(0,500)");

But How about dealing with pages whose length is unknown or dynamic? Here I found a solution to do it. We can calculate the height of the page during running time and then feed it to the above scrollBy method.


JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("var body = document.body,html                                 document.documentElement;var height = Math.max( body.scrollHeight,
body.offsetHeight,html.clientHeight, html.scrollHeight, html.offsetHeight );         window.scrollBy(0,height)");

Finally I was able to dynamic scroll down to  end of the page.

UPDATE:

You can also use the following code.

This will help you to scroll to the exact location instead scrolling down fully.


JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].scrollIntoView(true);",element);
element.click();

Where element is the WebElement that is being clicked.

Thanks for reading.  🙂

Clicking an invisible Element in Selenium Webdriver

2
Looking for an invisible Element

Looking for an invisible Element ?

There are some scenarios, where the elements needed to be handled goes invisible. In this scenario, the driver.findElement ().isDisplayed ? will return true, but the click operation would return a cannot click the element exception.

In such situation we can make the following workaround.

WebElement tmpElement= driver.findElement(ElementLocator);
JavascriptExecutor executor = JavascriptExecutor)driver;
executor.executeScript(“arguments[0].click();”, tmpElement);

This is just worked perfectly for me. If any one out there have a better option, you are appreciated to share your opinion.

Thanks for your time.