December 9, 2010

Check boxes count, Checked and Uncecked

This is the script ...which will count number of check boxes in a page and number of checked check boxes and unchecked check boxes




package test;

import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;

public class Checkboxcount {
public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox","http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testDefaultTNG() throws Exception {

selenium.open("http://browsershots.org/");
selenium.windowMaximize();

//Count of check boxes
Number c  =selenium.getXpathCount("//input[@type['checkbox']]");
System.out.println("Count of check boxes " +c);
//Number of check boxes checked
Number d = selenium.getXpathCount("//input[@type='checkbox' and @checked]");
System.out.println("Count of Checked check boxes " +d);
//Number of check boxes unchecked
Number e = selenium.getXpathCount("//input[@type='checkbox' and not(@checked)]");
System.out.println("Count of Checked check boxes " +e);
}

@AfterClass
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();

}
}


If you uncheck some check boxes and try the above script it will not work....bcos if you check and uncheck this wouldn't change any thing in their html....


3 comments:

  1. Could you please tell me how to delete particular check box out of 10 check box.

    Mail=info.bhanupratap@gmail.com

    ReplyDelete
  2. Hi thats great work.......
    I need your help...
    When i used ur code
    //Number of check boxes checked
    Number d = selenium.getXpathCount("//input[@type='checkbox' and @checked]");
    System.out.println("Count of Checked check boxes " +d);
    //Number of check boxes unchecked
    Number e = selenium.getXpathCount("//input[@type='checkbox' and not(@checked)]");
    System.out.println("Count of Checked check boxes " +e);


    Iam getting output as
    Count of check boxes 12
    Count of Checked check boxes 0
    Count of UnChecked check boxes 0

    Can u help me with this

    Thanks in advance:)

    ReplyDelete
  3. Hi,
    I get the same output as Sindhu :( Been trying a lot... :(

    No Solution :(

    ReplyDelete