Six steps for complete test automation with selenium grid

 

The earlier article “Crucial aspects of browser compatibilty testing with Selenium Grid” gives insight into browser compatibility testing andthe architecture of one of the most popular browser compatibility testing tools, selenium grid. Nowlet‘sget our hands dirty with test automation using Selenium Grid.

 

Pre-requisites:

The practice exercise in this article requires Eclipse, TestNG, ant, Java, Selenium RC and Selenium Grid. You can execute the code on your favorite platform. For this demo I am using ubuntu as platform.


Installation of Selenium Grid:

Please refer the documentation in seleniumhq for selenium grid download and installation instructions

http://selenium-grid.seleniumhq.org/step_by_step_installation_instructions_for_windows.html
http://selenium-grid.seleniumhq.org/get_started.html

Unzip the Selenium Grid folder and execute the below command in the Selenium Grid root directory to check whether it is installed correctly or not:

ant sanity-check

The output should be BUILD SUCCESSFUL

Now let’s get started.


 

Step 1. Launch of selenium hub:

Open a terminal and launch the selenium hub using the below command:

ant launch-hub

The hub is where the selenium remote controls providing the different environments like chrome on ubuntu or IE on Windows register themselves. We can view the list of remote controls registered with the hub at http://localhost:4444/console

Now the available remote controls section in the hub console displays nothing  since we did not start any remote control yet.

 

Step 2. Launch of selenium remote controls:

To start a remote control open a new terminal and issue the below command:

ant launch-remote-control

Then, open the hub console http://localhost:4444/console . The selenium grid hub snapshot displays a remote control on port 5555 for firefox. This is the default environment if we do not specify our own.

We can configure more than one selenium remote control with the same port but more than one remote control cannot run tests on the same port. The reason behind this is simple. For example, more than one person can call the same phone number but cannot talk over the phone to the same person at the same time. Only one call can be answered on a single phone at a time. Similarly it is impossible to run tests in parallel on two selenium remote controls configured with the same port number. We need to configure each remote control with unique port numbers. How do we do this?

If we want to specify our own environment we need to use the Dport and Denvironment options as below:

ant -Dport=5556 -Denvironment=*chrome launch-remote-control

 

Step 3. Automatic environment setup:

Let’s create a bash script for automatically opening up new terminals and launching several remote controls with the desired environment. If you are working on windows you need to create a batch file instead of a bash script. The below script works only on ubuntu platform.

Navigate to the selenium grid directory and create the below bash script. Let’s call it run.sh

#!/bin/bash
gnome-terminal --title="Hub" -x bash -c "ant launch-hub"
sleep 150
echo "10 seconds wait over"
gnome-terminal --tab --title="Chrome" -x bash -c "ant -Dport=5555 -Denvironment=*chrome launch-remote-control" &
gnome-terminal --tab --title="Firefox" -x bash -c "ant -Dport=5556 -Denvironment=*firefox launch-remote-control" &
gnome-terminal --tab --title="Opera" -x bash -c "ant -Dport=5557 -Denvironment=*opera launch-remote-control"

The bash script opens a new gnome terminal called “Hub” and launches the selenium hub. It sleeps for 150 seconds waiting for the hub to get started. Depending on the time taken you can change the sleep period. The script then opens three other new terminals and registers three remote controls with the respective environments specified. On execution of the script the below output is got on http://localhost:4444/console page.

Now the environment is setup for selenium grid automation. Let’s write a simple selenium grid program.

 

 

Step 4. Creating a java project called giftShop:

 

Suppose you would like to cheer up your friend on her birthday and give her the best birthday party of all time. You are planning  to celebrate your friend’s birthday with balloons, cakes, ice creams and of course a birthday gift!. You are going to surprise your friend with “I have got something for you” from www.gifts.com in such a way that no one can ever put a price tag on the friendship. Let’s write a selenium program to test the display of birthday gifts page on www.gifts.com website on multiple browsers.

 

  1. In Eclipse create a new java project called “giftShop”. Click on Next button and click on the “Libraries” tab. Click on “Add External JARs” button. Select the testng and selenium server JAR files as below and click on finish button.

 

 

2. Now create a new TestNG class inside the giftShop project. In the source folder field, browse to /giftShop/src. Give the package name as giftShop and class name as giftBox. Click on finish button.

Step 5. Enable testing in parallel mode in multiple browsers:


Right click on the giftShop project. Click on “Convert into TestNG” option. “Generate testng.xml” window will get displayed. This option will be available only if the TestNG plugin for eclipse is installed. Select “tests” in parallel mode dropdown and click on Finish button.

 

To organize the project structure create a folder called config and place the generated testng.xml file in it.

In the testng.xml file we just created, specify the browser and port names with which we have configured our selenium remote control as in the below piece of code.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
	<test name="Chrome">
		<parameter name="selenium.host" value="localhost"/>
		<parameter name="selenium.port" value="5555"/>
		<parameter name="selenium.browser" value="*chrome"/>
		<parameter name="selenium.url" value="http://www.gifts.com/"/>
		<parameter name="bdayLink" value="//li[@id='nav-oc']/div/ul[3]/li[3]/a/b"/>
		<classes>
			<class name="giftShop.giftBox" />
		</classes>
	</test>
	<test name="Firefox">
		<parameter name="selenium.host" value="localhost"/>
		<parameter name="selenium.port" value="5556"/>
		<parameter name="selenium.browser" value="*firefox"/>
		<parameter name="selenium.url" value="http://www.gifts.com/"/>
		<parameter name="bdayLink" value="//li[@id='nav-oc']/div/ul[3]/li[3]/a/b"/>
		<classes>
			<class name="giftShop.giftBox" />
		</classes>
	</test>
	<test name="Opera">
		<parameter name="selenium.host" value="localhost"/>
		<parameter name="selenium.port" value="5557"/>
		<parameter name="selenium.browser" value="*opera"/>
		<parameter name="selenium.url" value="http://www.gifts.com/"/>
		<parameter name="bdayLink" value="//li[@id='nav-oc']/div/ul[3]/li[3]/a/b"/>
		<classes>
			<class name="giftShop.giftBox" />
		</classes>
	</test>
</suite>

Step 6: Writing selenium grid program:

This selenium grid program will start selenium on chrome, firefox and opera. It would open up the www.gifts.com website and click on birthday as occasion. It asserts whether the landing page displays the birthday gifts in all the three browsers as expected.

 

package giftShop;
 
import org.testng.annotations.Test;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
 
public class giftBox 
{
	Selenium selenium;
 
	//Declaration of variables used in this program
	String strTimeout="30000";
	String bdayTitle="Birthday Gifts - Birthday Gift Ideas from Gifts.com";
	int intsleepTime=15000;
 
    @BeforeClass
    //Reads the values of host, port, browser and url "www.gifts.com" from testng.xml
    @Parameters({ "selenium.host", "selenium.port", "selenium.browser", "selenium.url" })
    public void beforeClass(String host, String port, String browser, String url)
    {
        selenium = new DefaultSelenium(host, Integer.parseInt(port),browser, url);
        selenium.start();
        selenium.open("");
        selenium.windowMaximize();
        selenium.windowFocus();
        selenium.deleteAllVisibleCookies();
    } 
 
    @Test
    @Parameters({ "bdayLink" })
    public void testB(String bDayGift)
    {
        try
        {
        	//Click on birthday as occasion on www.gifts.com website
        	selenium.click(bDayGift);
        	selenium.waitForPageToLoad(strTimeout);
 
        	//Verify that the birthday gift items page is displayed
        	Assert.assertEquals(bdayTitle, selenium.getTitle());
            Thread.sleep(intsleepTime);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 
 
    @AfterClass
    public void afterClass() 
    {
        selenium.close();
        selenium.stop();
    } 
}

Running the test case:

Please do the following to run the selenium grid test case (after the hub and RCs are launched):
1. Right click on the testng.xml file
2. Click on Run as “TestNG Suite”

Please feel free to post any comments or ask questions.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks

Comments

  1. Wonderful presentation… Thanks for sharing.

    • It’s my pleasure . Nice to hear that you found this article useful.

      Thanks again for your interest .

      Regards,
      Aruna

    • yogesh deshmukh says:

      Can you tell me how to stop the RC.

      I am not getting any output except in eclipase i get

      RemoteTestNG starting
      RemoteTestNG finishing: 26332 ms

  2. Hi Aruna,
    Can you give a windows example?.
    Currently I am able to setup selenium grid, launch the hub, run multiple RCs. But when i run the test cases, I am getting blank screen in firefox and thats it…. it does not move ahead.

    • Hi,

      Now I know the cause of “the blank firefox window” issue you are facing. We cannot run the test case directly since the parameters are being passed through TestNG.xml file. We need to run it as a suite using TestNG.xml file. Sorry, I missed to explain “how to run” in this article.

      Please do the following to run the selenium grid test case (after the hub and RCs are launched):
      1. Right click on testng.xml file
      2. Click on Run as “TestNG Suite”

      Hope this resolves the issue. I have updated this article with “how to run”.

      Thanks,
      Aruna

  3. Hi,

    This code worked on windows platform successfully. There are two points in which windows and ubuntu differ.

    1. The installation part differs in windows, which I believe is successfully done in your case.
    2. The other point of difference is bash script to automatically lanuch RCs. This works on ubuntu. In windows we usually create a batch script with .bat extension. The easiest way is to give the command directly in a new command prompt as below without using any script:
    ant -Dport=5556 -Denvironment=*firefox launch-remote-control
    Also the terminologies differ. In windows we call it as command prompt and in ubuntu its called terminal.

    I suppose even launching of RCs has been done in your case.

    Are you able to see the three RCs in the hub console http://localhost:4444/console?

    If you see, check for spelling mistakes since it takes anything you specify as environment. Does the blank screen close automatically or does it stay open?

    Regards,
    Aruna

  4. Thanks for good example and explanation. did you only try this with only this browsers?

  5. Yes, I have tried only with firefox, chrome and opera. It should also work with other browsers like safari and internet explorer.

    Regards,
    Aruna

  6. Hi Aruna

    I try run your example code on Windows7. I found same problem as ksk that afer launch hub and run RCs. And I run by right click on testng.xml and click run testng.xml (I have used Itellij), the blank page firefox is open and not occur anythings. please share your step that run work on windows7

    Thanks

  7. Hi Aruna,

    After I repeat your 6 steps again, now I have finished run script concurrently Yeh!
    I fix problem by update build.xml in selenium grid folder in field id=”remote-control.classpath” to point to new version of selenium server (this case I use selenium-server-standalone-2.5.0.jar) It can run concurrently.

    But I found new problem when I test HTTPS that the start browser (firefox and chrome) will show warining page that I have not found when use selenium RC alone. Is it the port problem? Thanks in advance.

  8. Hi,

    I try run your example code on Windows7. I found same problem as that after launch hub and run RCs. And I run by right click on testng.xml and click run testng.xml, the blank page firefox is open and not occur anythings. please share your step that run work on windows7
    I have run same script in GoogleChrome and Safari(unable to scripts)

    In google chrome got this error
    “Error 111 (net::ERR_TUNNEL_CONNECTION_FAILED): Unknown error.”

    Please help me.

    Thanks,
    Venky

  9. Hi Aruna,
    I am trying this example with Windows XP,launched hub and RC successfully,
    When I am running the testng.xml as testngSuite it is showing configuration failure as

    [TestNG] Running:
    C:\Prasanna Eclipse Work Space\GiftShop\testng.xml

    ===============================================
    Suite
    Total tests run: 2, Failures: 0, Skips: 2
    Configuration Failures: 2, Skips: 2
    ===============================================

    Please help me where Iam doing mistake,
    Thank u in advance
    Thanks,
    Prasanna

  10. Pratt G says:

    I have a test case recorded in selenium in HTML.When I type the command as :

    java -jar /usr/zephyr/selenium-server-standalone-2.25.0.jar -port 5556 -htmlSuite /usr/zephyr/*firefox “http://www.google.com/” “/usr/zephyr/new_login.html” “/usr/zephyr/results.html”

    It gives an ERROR of firefox:

    5 Mar, 2013 9:31:16 AM org.openqa.grid.selenium.GridLauncher main
    INFO: Launching a standalone server
    09:31:16.566 INFO – Java: Sun Microsystems Inc. 20.0-b12
    09:31:16.569 INFO – OS: Linux 3.0.0-12-server amd64
    09:31:16.587 INFO – v2.25.0, with Core v2.25.0. Built from revision 17482
    09:31:16.783 INFO – RemoteWebDriver instances should connect to: http://127.0.0.1:4443/wd/hub
    09:31:16.785 INFO – Version Jetty/5.1.x
    09:31:16.786 INFO – Started HttpContext[/selenium-server/driver,/selenium-server/driver]
    09:31:16.787 INFO – Started HttpContext[/selenium-server,/selenium-server]
    09:31:16.788 INFO – Started HttpContext[/,/]
    09:31:16.812 INFO – Started org.openqa.jetty.jetty.servlet.ServletHandler@423e5d1
    09:31:16.812 INFO – Started HttpContext[/wd,/wd]
    09:31:16.818 INFO – Started SocketListener on 0.0.0.0:4443
    09:31:16.819 INFO – Started org.openqa.jetty.jetty.Server@4d125127
    HTML suite exception seen:
    java.lang.RuntimeException: Browser not supported: /usr/zephyr/*firefox
    (Did you forget to add a *?)

    Supported browsers include:
    *firefox
    *mock
    *firefoxproxy
    *pifirefox
    *chrome
    *iexploreproxy
    *iexplore
    *firefox3
    *safariproxy
    *googlechrome
    *konqueror
    *firefox2
    *safari
    *piiexplore
    *firefoxchrome
    *opera
    *webdriver
    *iehta
    *custom

    at org.openqa.selenium.server.browserlaunchers.BrowserLauncherFactory.browserNotSupported(BrowserLauncherFactory.java:151)
    at org.openqa.selenium.server.browserlaunchers.BrowserLauncherFactory.getBrowserLauncher(BrowserLauncherFactory.java:127)
    at org.openqa.selenium.server.htmlrunner.HTMLLauncher.getBrowserLauncher(HTMLLauncher.java:78)
    at org.openqa.selenium.server.htmlrunner.HTMLLauncher.runHTMLSuite(HTMLLauncher.java:138)
    at org.openqa.selenium.server.htmlrunner.HTMLLauncher.runHTMLSuite(HTMLLauncher.java:196)
    at org.openqa.selenium.server.SeleniumServer.runHtmlSuite(SeleniumServer.java:612)
    at org.openqa.selenium.server.SeleniumServer.boot(SeleniumServer.java:286)
    at org.openqa.selenium.server.SeleniumServer.main(SeleniumServer.java:244)
    at org.openqa.grid.selenium.GridLauncher.main(GridLauncher.java:54)
    09:31:16.847 INFO – Shutting down…

    How can I overcome this problem ?

    Thanks in advance.

  11. Hi ,

    Can anyone please help me how to automate any webapplication with help selenium grid with steps and examples.

Speak Your Mind

*