There was an error in this gadget

Technical Project Lead @ Huawei Technologies

My photo
Bangalore/Hyderabad, Karnataka/Andhra Pradesh, India
Have an overall 10 + yrs of experience Currently working as an Senior Architect.Expertise in design,implementation of Automation frameworks for Opensource automation tools like Selenium,Jmeter, Webdriver,Appium, Robotium Expertise in integrating Test & ALM Management tools like Rally,Qmetry,JIRA-Zephyr with Automation frameworks Expertise in design and developmenet Mobile Automation frameworks for tools like Appium, Robotium Remote Control,Cucumber BDD

Followers

Tuesday, October 26, 2010

Parallel distribution of RCTests using Selenium Grid and TestNG

Before getting into Selenium Grid, I would like to show some light on how to achieve distribution and Parallelism using Selenium Grid. Also its time to switch from Test NG over Junit because of TestNG flexibility over unit testing

What is Selenium Grid?
Selenium Grid is an extension of Selenium RC to distribute your tests across multiple platforms
saving you time by running tests in parallel.

TestNG in context to Selenium Grid.
TestNG comes into picture when you want to achieve parallel execution of Selenium RC tests . However there are different frameworks to achieve this parallelism. I found TestNG as a Simple and Powerful Testing Framework that can be integrated alongside of Selenium RC scripts or for Unit Testing purpose. Frameworks like Junit are bound to unit testing. There are times where might require to do an integration testing @ code level.Test Frameworks like Junit has this limitation.TestNG goes a bit beyond the Unit testing and even supports Integration testing too.



Configuring Test NG Plugin in Eclipse
Below are the simple steps on how to configure TestNG as a plug in to Eclipse

  1. Go Eclipse menu Help >> Install New Software, click on Add.. button
  2. Type some name ex TestNG.
  3. Enter this url http://beust.com/eclipse
  4. Click on Ok button.
  5. Now select TestNG from "Work With" drop down.
  6. Select the TestNG plugin
  7. Click on Next again Next .
  8. Accept the License Agreement
  9. Click on finish Button.
  10. Allow eclipse to download the plugin in background
  11. On Prompt, Restart the eclipse.
  12. video


Step by Step Configuring Selenium Grid Setup
  1. Download the latest selenium grid from the Official Selenium site >> http://selenium-grid.seleniumhq.org/download.html.  Latest one available at the time of writing this blog is 1.0.8
  2. You  Need to have some prerequisites in order to control and configure Grid. So before we go for configuring the grid we will see how to configure Ant. But a question might arise on why we require ant in Grid Context. He is the answer . Selenium Grid uses ant targets to start or launch the Grid. Also the package comes with ant targets to launch Remote Control on desired port
  3. Now download ant from http://ant.apache.org/bindownload.cgi
  4. Unpack the zip and add the ant bin path (ex: D:\Software\Automation\Tools\ant\Ant\apache-ant-1.8.1\bin) to the System Variable Path

4. Also ensure that your JAVA_HOME is added to user variable

 5.Now open the command prompt and type the command ant -version. You should be able the following if you ant is configured properly. Else follow the previous steps and ty again
you should be able to see the below output when u run the command $ ant -version
>> Apache Ant version 1.8.1 compiled on April 30 2010

6.Now the Ant configuration is complete
7.We just need to run few Ant commands to launch the Grid Hub and register multiple remote control to the Hub.
8.Let us consider a scenario where we have some tests that needs to be run against both firefox and as well as on chrome and how do we get these tests run paralley on both firefox and chrome at a time
9.Firstly we need to start the grid . Go to the directory where your grid is located. Open the command prompt and change the directory to the selenium grid(ex:E:\Softwares\Selenium\selenium-grid-1.0.7) where the build.xml exists
10.Now run the following command $ ant launch-hub
11.This will launch the Selenium Grid on 4444 port.
12.Now you need to register 2 remote controls to Grid. One RC for running Firefox tests and the other for Chrome tests
13.Open the command prompt from the Grid directory and run the following command
ant -Dport=5556 -Denvironment=*firefox launch-remote-control . This will start Remote Control instance on 5556 and will be listening to grid
14. Open another command prompt from the Grid directory and run the following command
ant -Dport=5557 -Denvironment=*googlechrome launch-remote-control . This will start Remote Control instance on 5557 and will be listening to grid.
15.Lets try to check whether the remote control really is registered to Grid.
16.Open the browser and run the following URL http://localhost:4444/console. On opening this url you should be able to see the following RCs registered to grid


17.Now our setup is ready what's next ?.................Next ?? We just need to run our scripts against these RC. So simple as this statement right ;)
18.Hmm Now How to run Test Scripts against multiple RCs parallely
19.Below steps shows how to create sample test script which will automate Google.com on Firefox as well as Chrome browsers concurrently.
20.From the eclipse create a New Java Project.
21.How are we going to write our tests ?? We will use Test NG to write our scripts.As in the initial statement, we discussed TestNG in context to Selenium Grid. This is where exactly TestNG is used
22. Here we are going to integrate TestNG alongside of Selenium RC scripts in order to achieve the distribution of tests among multiple browsers
23. We will now create a sample Selenium RC script with TestNG and we will see how the script runs on multiple browsers
24. Create an eclipse Java Project
25.Create a class of type TestNG class
26. Select the source folder , set package name, Test Class and select the @BeforeClass and @AfterClass annotations. These are exactly same as that of Junit 4 annotations


27.Provide the XML Suite file name as testng.xml . Click on Finish button
28.Now your eclipse should create a class as shown in figure Below
29.Open the testng.xml under src folder and copy/paste the below stuff


<suite name="Suite" parallel="tests">
    <test name="IexploreTests">
        <parameter name="selenium.host" value="localhost"></parameter>
        <parameter name="selenium.port" value="5556"></parameter>
        <parameter name="selenium.browser" value="*firefox"></parameter>
        <parameter name="selenium.url" value="http://www.google.com"></parameter>
        <parameter name="searchCriteria" value="IExploreTests"></parameter>
        <classes>
            <class name="com.mycompany.GoogleTest" />
        </classes>
    </test>
    <test name="FirefoxTests">
        <parameter name="selenium.host" value="localhost"></parameter>
        <parameter name="selenium.port" value="5557"></parameter>
        <parameter name="selenium.browser" value="*firefox"></parameter>
        <parameter name="selenium.url" value="http://www.google.com"></parameter>
        <parameter name="searchCriteria" value="FirefoxTests"></parameter>
        <classes>
            <class name="com.mycompany.GoogleTest" />
        </classes>
    </test>
</suite>
30. I had created 2 tests with name 'IexploreTests'  and 'FirefoxTests'
31.This is just to drive a single testcase on different browsers. If you want to run the test on Chrome, We will have to create another test parameter with different RC configurations.
32.Also parallel parameter should be set to tests inorder to run these test parallely on multiple browsers
33.Now open the testng class which I had created under step 26
34. Copy and Paste the below code into the testng class and Save it
35.In the below code, i had created one test which Im parameterizing with different sets of values from testng.xml

 package com.mycompany;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class GoogleTest {
    Selenium selenium;

    @BeforeClass
    @Parameters({ "selenium.host", "selenium.port", "selenium.browser",
            "selenium.url" })
    public void beforeClass(String host, String port, String browser, String url) {
        this.selenium = new DefaultSelenium(host, Integer.parseInt(port),
                browser, url);
        this.selenium.start();
        this.selenium.open("");
    }

    @AfterClass
    public void afterClass() {
        this.selenium.close();
        this.selenium.stop();
    }

    @Test
    @Parameters({ "searchCriteria" })
    public void testGoogleSearch(String criteria) {
        this.selenium.type("name=q", criteria);
        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }   
}



35.To run the tests, open the Testng.xml and right click >> Run as >> Run Configurations
36. Select TestNG from the left list
37. Click on Add (+) button on the topleft
38.Select the project
39.Select the TestSuite as src/testng.xml
40.Apply and run the settings
41.Now you should be able to run the tests on multiple browsers parallely

Wednesday, October 20, 2010

Selection of Dymanic Combo Values

There are 2 cases where the Combo Box goes dynamic
1.ComboBox it self is an ajax element
2.The Combovalues gets dynamically populated on some event trigger

Point 1 can be handled using selenium.waitForCondition()
How about point 2 ? Below is the code

public class DynamicComboElementHandling {
    static Selenium selenium;
    static SeleniumServer server;

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        selenium = new DefaultSelenium("localhost", 4444, "*firefox",
                "http://www.google.com");
        server = new SeleniumServer();
        server.start();
        selenium.start();
        //Click on Google Advanced Search Link
        selenium.open("/advanced_search?hl=en");
        //Wait Until the Drop appears on the screen
        selenium.waitForCondition("selenium.isElementPresent(\"name=num\");",
                "15000");
        boolean flag = true;
        while (flag) {
            //Get the count of the drop down values
            int optionCount = selenium.getSelectOptions("name=num").length;
            // if the dropdown is populated with values, come out of the while
            // loop
            // else continue looping until the count is > 0
            if (optionCount > 0) {
                break;
            }
            continue;
        }
        //This code will be executed only if dropdown has some values
        //else the execution control will be inside the while loop until the
        //drop down values gets populated
        String[] options = selenium.getSelectOptions("name=num");
        for (int i = 0; i < options.length; i++) {
            if (options[i].equals("50 results")) {
                selenium.select("name=num", options[i]);
                break;
            }
        }

        Thread.sleep(3000);
        selenium.close();
        selenium.stop();
        server.stop();

    }

}

Thursday, October 14, 2010

How to get the Width and Height of Images or Elements

 public class ImageTests {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub:
        Selenium selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.google.com");
        selenium.start();
        selenium.open("");
        //Print the Width of the Image
        System.out.println("Width Of Image is : " + selenium.getElementWidth("logo"));
       
        //Print the Height of the Image
        System.out.println("Height Of Image is : " + selenium.getElementHeight("logo"));   
       
        selenium.close();
        selenium.stop();
       
    }

}

Tuesday, October 12, 2010

How to do Data Driven Testing

Simplest way to drive the gui components using different sets of data is from a properties file. However we can use excel sheets as our data source. For small sets of data we can use the properties file

Here is the step by step Procedure which gives an idea on how to drive the Google search text box with different sets of data from a properties file

1.Create a Java Project in Eclipse.
2.Right click on the Source folder , go to File >> New >> File
3. Provide file name of your choice with an extension .properties . Ex give the file name as datapool.properties and click on Finish button
4. After clicking on Finish you should be able to see the file in the Project Explorer of eclipse as shown below
5.Open the datapool.properties, enter the key. Just copy paste the below line in the properties file & save the file .Refer below snapshot
Search_Criteria=SeleniumRC,SeleniumIDE,SeleniumGRID

6. Now that we have different sets of data in a resource called datapool.properties.
7.Next step is to read the properties file
8.Now create a Resource bundle object that reads the properties file
9.Now using the bundle object, Read the line of the properties file using the key Search_Criteria" 
10. bundle.getString("Search_Criteria") will return you the string of values for the specified key. 9.Now the String criteria will have value "SeleniumRC,SeleniumIDE,SeleniumGRID".
11.We will use the Java Utility called String Tokenizer to beak the above String into individual tokens identified by comma.
12.After Splitting for each token we will fill in the Google Search Text Box.
13.Copy the code from the below Image in your eclipse and run it

Below is the snapshot of the code

Monday, October 11, 2010

Selenium Automation Framework

**Coming Soon....

Continuous Integration using Hudson

***Coming Soon

How to get the count of Checkboxes and Select them all

Lets take a webpage having multiple check boxes. How do I get the count of all checkboxes and how do I select them
1.Go to the following link and you will find checkboxes under Search Language. Below Code Snippet instructs selenium to get the check box count and select all the checkboxes one by one

    http://www.google.co.in/preferences?hl=en


import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class AjaxTests {
    static SeleniumServer server;
    static Selenium selenium;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        server = new SeleniumServer();
        server.start();

        selenium = new DefaultSelenium("localhost", 4444, "*iexploreproxy",
                "http://www.google.com");
        selenium.start();
        selenium.open("/preferences?hl=en");
        selenium.windowMaximize();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        selenium.close();
        selenium.stop();
        server.stop();
    }
  

    @Test
    public void testAllCheckBoxes(){
         //Get the count of all available check boxes

         int checkBoxCount = selenium.getXpathCount("//form[@name='langform']/table[5]/tbody/tr[2]/td[3]
                                          /table/tbody/tr/td/font[1]/label/input").intValue();
        //Print the Check box count
        System.out.println("Total Number of Check box count is : " + checkBoxCount );
        int count = selenium.getXpathCount("//form[@name='langform']/table[5]/tbody/tr[2]/td[3]/table/tbody
                          /tr/td[1]/font[1]/label").intValue();
        System.out.println(count);
        for (int i = 1; i <=4; i++) {
            for (int j = 1; j <= count; j++) {
                if(i == 4 && j == 11){
                    break;
                }
                selenium.check("//form[@name='langform']/table[5]/tbody/tr[2]/td[3]/table/tbody/tr/td[" + i +
                              "]/font[1]/label"+"[" + j +"]/input");
              
            }
        }
      
    }
  
}