Showing posts with label Selenium Grid. Show all posts
Showing posts with label Selenium Grid. Show all posts

Tuesday, October 25, 2011

Selenium Grid with WebDriver

I had earlier covered in my blog on how to execute your RC cases in selenium grid. But with the commencement of Selenium-2/Webdriver, grid setup has been changed. In the following blog I will cover how to set-up your grid and the changes that will be required for easy execution of cases.


Following things are required:
1. Selenium 2.xx version server jar and Java library. The latest one can be downloaded from the link: Selenium-2
2. Java 1.6 and above
3. TestNg jar . You can download it from the link: TestNG
4. Eclipse with TestNG plugin installed(optional)
5. ChromeDriver. Can be downloaded from: ChromeDriver


The Test Code
Following is an example of test-class that have a test case to search google for "testing" and verifying after clicking it on a link.

In the above class I am using the TestNG "Parameter" property to provide different data set to the "BeforeClass" method "beforeClass". The beforeClass method accepts two properties "browser" and a "port".
These values are used for initialization of driver and in-turn for initialization of selenium object. In the above code I am using the "WebDriverBackedSelenium" class for creation of the selenium object, so that its easy for guys who had worked on Selenium-1 to understand the code. If you want the code to be purely WebDriver, you can directly use the "driver" object for defining your test-cases.
The main part in this test case is how the driver object is being created:
DesiredCapabilities capability= new DesiredCapabilities();
capability.setBrowserName(browser);
WebDriver driver= new RemoteWebDriver(new URL("http://localhost:".concat(port).concat("/wd/hub")), capability);
The above code creates an object of DesiredCapability and then set the browser value to it. Now using this "capability" object I am creating the webdriver object using the "RemoteWebDriver" class. This tells the selenium on which browser the test-case needs to run and where the server is located. In this example I am assuming the server to be running locally. In case it is on different system the "localhost" needs to be re-placed with the ip of the said system.

TestNG configuration
For parallel execution you need to use the TestNG configuration. Following is an "testng.xml" file for the above said test class. The said configuration executes the test-cases across different browser.


<suite name="Selenium TestNG Suite" parallel="tests"
    thread-count="5">

    <test name="Selenium TestNG - 1">
        <parameter name="browser" value="firefox" />
        <parameter name="port" value="4444" />
        <classes>
            <class name="com.test.testng.Google" />
        </classes>
    </test>
    <test name="Selenium TestNG - 2">
        <parameter name="browser" value="chrome" />
        <parameter name="port" value="4444" />
        <classes>
            <class name="com.test.testng.Google" />
        </classes>
    </test>

</suite>


In the above configuration, I am configuring TestNG to run "tests" in parallel. Also there are two different tests inside a suite. For each test a different "browser" parameter value has been configured.

Selenium-Grid server
Now start your grid using the following commands. Run each command in a seperate terminal or command prompt by going to the directory containing your selenium-server-standalone-2.x.x.jar. In the following example I am using the 2.7.0 version of selenium.

For Hub:

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


For a firefox based node:

java -jar selenium-server-standalone-2.7.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=firefox


For google-chrome based node:

java -Dwebdriver.chrome.driver=/path/to/chromedriver -jar selenium-server-standalone-2.7.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=chrome

Before running  the above command  you need to provide the chrome-driver path to the property "-Dwebdriver.chrome.driver".

Now run your testng.xml from eclipse by selecting it -> Right click -> Run as -> TestNG suite(this will work only if you have TestNG plugin installed in your eclipse.)
Or you can choose other ways to execute "testng.xml" like from command prompt, using ant or maven.
Once you run the above testng.xml. TestNG will execute the cases from the Google class on grid across different browsers firefox and google-chrome as configured.


You can do configurations for environment like OS (Linux,Windows), browser, browser-version and all and then you can run you cases on particular type of environment by configuring them accordingly. More details can be found at the following URL:
http://code.google.com/p/selenium/wiki/Grid2

Friday, May 7, 2010

Using TestNG for parallel execution on Selenium Grid

Following I will mention how to integrate TestNG with Selenium-Grid code and use it for parallel execution of test cases.
For this you create a Base class where you define the following two functions.


protected
void startSession(String seleniumHost, int seleniumPort, String browser, String webSite)
throws Exception {


startSeleniumSession(seleniumHost, seleniumPort, browser, webSite);

session().setTimeout(TIMEOUT);

}

protected void closeSession() throws Exception {

closeSeleniumSession();

}

The above two functions are for the starting and closing the session in Selenium Grid. The references of it can be found in my earlier posts.

Now import the testng annotation class “org.testng.annotations.*” onto the Base Class.

Add @BeforeClass before the “startSession” method and @AfterClass before the “closeSession” method.

Now write your test cases in a way that all dependent test cases are in one class.


So whenever you execute the testNG it will call the “startSession” and “closeSession” before and after each test class execution. The “startSession” will create a Selenium session for test execution and return it to the test class. Where as the “closeSession” will close the session.

For parallel execution define inside the testng.xml file the attributes “suite” tag as “parallel="classes" thread-count="2" ”.
This will tell TestNG that parallel execution needs to be done for classes and max. Thread count for it is “2”.

Define 2 different test classes both extending the Base Class which defines the “startSession” and “closeSession” methods. Start a Selenium Grid Hub and register two Selenium remote control to the Hub. Now try running the package containing these classes using the TestNG.

You will see 2 different tests running in parallel in Selenium Grid each for Selenium Remote Control registered to the Hub.
The “startSeleniumSession” inside the “startSession” method create a unique selenium session for each of the test class and use that session for test class execution. Creating a session,registering the session with a Remote control and maintaining the session is done by Selenium Grid where as using TestNG framework we define how many sessions we need to create and how the test execution should be. So using the features of both TestNG and Selenium Grid we achieve Parallel execution of our automation testcases.

Currently here I had suggested to use @Before/AfterClass annotations in Base Class. You can use any of the other available @Before/After annotations available in TestNG depending upon your tests.

Saturday, April 17, 2010

Integrate Selenium-RC to Selenium Grid

Most of us develop our cases in Selenium RC and test it . If we want to run our cases in parallel we need to use Selenium Grid. Selenium Grid gives us the advantage of running the cases in parallel and in different environments.

I will tell in this blog what you need to do for setting up Selenium Grid For Parallel Execution or for changing your existing code to be able to be used with Selenium Grid.First you need to get the “selenium-grid-tools-standalone-1.0.4.jar” from the lib folder under the Selenium Grid folder that you had downloaded. Import this jar to the reference Libraries under you selenium RC code.

For java you need to change the way you create the selenium object. Most of us extends the SeleneseTestCase class or create the object using "new DefaultSelenium", for Grid we need to change that with following methods.


First import the following classes into your code.


import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.closeSeleniumSession;

import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.session;

import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.startSeleniumSession;


This will import the Selenium Grid class.

Then you need to define methods as mentioned below.


protected void startSession(String seleniumHost, int seleniumPort, String browser, String webSite) throws Exception {

startSeleniumSession(seleniumHost, seleniumPort, browser, webSite);

session().setTimeout(TIMEOUT);

}


protected void closeSession() throws Exception {

closeSeleniumSession();

}


The method “startSession” will create a session using the “seleniumHost”, “seleniumPort” & “browser” value onto the Selenium remote control using the Selenium Grid.

This session object is to be used for executing your selenium commands.

Following is an example on how to use the “session()” for test execution in Selenium Grid.


public void login() {

session().open("/webpage/");

session().type("txtUserName", "admin");

session().type("txtPassword", "admin");

session().click("Submit");

}

A simplest way to use the “session()” can be,by assigning it to a Selenium object and using the selenium object in your test execution. For ex.


Selenium selenium = session();


This way you don't have to replace the “selenium” object in your Selenium RC code with “session()” as mentioned in the “login()” method example.


Also for parallel execution you need to integrate TestNG with your Selenium Code. TestNG supports multi-threaded execution. I will be writing about how to use TestNG with Selenium in my coming blogs.

Thursday, March 11, 2010

Defining your own Environment and Using it in Selenium Grid


If you want to use an environment something like “Firefox on Windows” or “Firefox on Linux”.
You can use that by starting the Remote control with the said environment name. This will register the Remote control to the hub with the said environment name.
For ex. Start the hub normally but while starting the Remote control replace the -Denvironment value with your own particular name.

ant -Dport=5555 -Dhost=192.168.1.0 -DhubURL=http://192.168.1.2:4444 -Denvironment=”Firefox on Linux” launch-remote-control


If you check the Hub console it will look something like


Also if you want to use such an environment for executing your Test you need to pass the environment name to the selenium object in your code.
For Java you need to use the following function while creating the selenium object.


new DefaultSelenium("localhost", 4444, “Firefox on Windows”, 'http://google.com');

Now if you go through the above things you will think about how to use the above function and define the selenium object since it is already been declared and defined when you get it from selenium IDE.
This happens because when you copy the code from the Selenium Ide, the class that is defined in the code extends the “SeleneseTestCase” class where this “selenium ” object has already been defined.


For using the custom Environment with the Selenium Grid do the following thing:

1.Remove the “SeleneseTestCase” class which is extended by the Test class.

2.Define your own Selenese object as shown below

Selenium selenium;
selenium =new DefaultSelenium("localhost", 4444, " ", "http://www.google.com");


3.Start the selenium using the below command.

selenium.start();

This will start your selenium session. Then using this selenium object you run your test cases.

4.After your test is done use the following command to stop the selenium.


selenium.stop();

When you call the selenium grid by giving some particular Environment name. The Selenium Hub will make sure to provide the test with a remote control that registered with this specific Hub environment.

Friday, March 5, 2010

Setting up Selenium-Grid on your System

In this blog I will tell you how to set-up Selenium Grid on your system.


  1. Check that Java is installed in your system by using the command “java -version”. If Java is installed it will give the output about Java Version that is installed in your system. Else download the Java from Java website (http://www.java.com/en/download/index.jsp).

  2. Check ANT is installed in your system by using the command “ant -version”.If Ant is installed it will give the output about Ant Version that is installed in your system. Else download the Apache Ant from Apache website (http://ant.apache.org/).

  3. Download Selenium Grid from Selenium Grid website(http://selenium-grid.seleniumhq.org/download.html) and unpack it.

  4. Go to the Unpacked directory of the Selenium grid using command prompt and type the command “ant launch-hub”. This will launch the Selenium Grid Hub on a default port “4444” to check whether the selenium grid is running. Type the URL “http://localhost:4444/console ” on your browser and check that you are able to see the selenium hub console on the browser.

  5. Now go to the same Selenium Grid directory using command prompt and type the command “ant launch-remote-control”. This will start the Selenium Grid Remote control on default port “5555” and environment “*firefox” and register it to the Hub which you had earlier started.

  6. Now after starting the selenium remote control we will test the example provided with Selenium Grid for sequence testing in single machine. For this go to the Selnium grid folder from command prompt and type the command “ant run-in-demo-sequence”. This will execute the test example provided with selenium grid to be executed in sequence.


Now you may had noticed in the setting up steps that I am running the test in sequence. You may think in your mind that, what's special in that? The same things can be achieved when we use a Selenium RC.Then here comes the important part of Selenium Grid.


Setting up Parallel execution on a single machine

  1. Start the hub and one remote control in the same way as in the earlier setting up steps.

  2. Now go to the Selenium Grid folder using command prompt and type the command

“ant -Dport =5556 launch-remote-control”

This will start another remote control on port “5556” and register it to the local hub. If you want to register more remote control to the Hub. You can do that by starting multiple remote control but with different ports.

  1. To know the no. of registered remote control onto a hub go to the URL “http://localhost:4444/console”. It will give you a view as follows:


  1. Now to run the test in parallel. Go to the selenium grid directory using command prompt and type the command “ant run-demo-in-parallel”. This will run the example tests given with selenium grid in parallel.


Setting up parallel execution for different machines and different environments

In this case start the Hub normally as you do in any of the system.

For starting the remote control use the following command.


ant -Dport=<port> -Dhost=<hostname> -DhubURL=<hub> -Denvironment=<environment> launch-remote-control


Here

“-Dport” should be used to set the port of the remote control.

“-Dhost” should be used to set the host(where the remote control has been started) IP. This is the IP which the hub will use to talk to the remote control.

“-DhubURL” should be used to set the Hub Url where the current remote control will register itself.

“-Denvironment” should be used to set an environment name that remote control represents to the hub.

Now start 4 different remote controls from 2 different machines and register them to the hub.

After this check the Hub console. It will look similar to the table as shown below depending upon the remote control that are registered to the Hub.


Then to verify your set-up go to the Selenium Grid folder from the command prompt and type the command “ant run-demo-in-parallel”


This will run an example test accompanied with the Selenium Grid to run in parallel.


Advantages of Selenium Grid

  1. Remote Execution of the selenium tests can be done using Selenium Grid without actually copying your test code to to remote system.

  2. Selenium Grid can be used to save time by executing independent testcases in parallel.

  3. Load Testing can be done if we register a lot of remote control to a single hub and execute testcase in parallel to simulate multiple transactions at a single timeperiod.