Technical Project Lead @ Huawei Technologies

My photo
Bangalore/Hyderabad, Karnataka/Andhra Pradesh, India
Have an overall 13 + 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,Manual Testing

Followers

Monday, October 11, 2010

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");
              
            }
        }
      
    }
  
}

5 comments:

  1. Hi Sir ,

    Ur blog is really help .. i have a doubt in counting the checkbox ...

    Wat does the Xpath //form[@name='langform']/table[5]/tbody/tr[2]/td[3]
    /table/tbody/tr/td/font[1]/label/input").intValue();

    stand for in the wensite ???

    ReplyDelete
  2. hi ,

    this is gousia,I am trying to learning selenium on my own,but at some point i am struck,please help me out if there is any helpful material or you can provide guidance....

    Let me know

    ReplyDelete
  3. right now I have a scenario,
    that is
    there is a checkbox with label "show only my items"when we check the check box,it should show the widgets created by the current user.
    if we uncheck it should show all the other user widgets also
    I was trying by Xpath ,i am not getting ,
    please help me out

    ReplyDelete
  4. Hi your blog is very good.I have one doubt is it possible to apply automated testing using java selenium for a wordpress page having images graphs,pie charts?
    Please reply soon if possible post some related example
    thank you

    ReplyDelete
    Replies
    1. Yes, You should be able to automate word press

      Delete