Showing posts with label Checkbox. Show all posts
Showing posts with label Checkbox. Show all posts

August 3, 2015

Check all check boxes and verify status of check box

August 03, 2015 1
One more example from dave haeffner's weekly tip...

The Problem

Checkboxes, an often used element in web applications. But how do you work with them in your Selenium tests? Intuitively you may reach for a method that has the word 'checked' in it -- like .checked? or .isChecked. But this doesn't exist in Selenium. So how do you do it?

A Solution

There are two ways to approach this -- through an element's 'checked' attribute (a.k.a. an attribute lookup), or by asking the element if it is selected.
boolean org.openqa.selenium.WebElement.isSelected()

  • isSelected

    boolean isSelected()
    Determine whether or not this element is selected or not. This operation only applies to input elements such as checkboxes, options in a select and radio buttons.
    Returns:
    True if the element is currently selected or checked, false otherwise.
Below example shows you how to check all check boxes and verify the status of check boxes.




package sample;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Checkboxes_Test {
 public WebDriver driver;

 @Test
 public void testCheckboxes_Test() throws Exception {

  driver.get("http://the-internet.herokuapp.com");
  driver.findElement(By.linkText("Checkboxes")).click();
  //get all the check boxes in web page
  List<WebElement> cBox=driver.findElements(By.cssSelector("input[type='checkbox']"));
  for (int i = 0; i < cBox.size(); i++) {
   //verify check box status, if it is not checked then perform click operation
   if (!cBox.get(i).isSelected()) {
    cBox.get(i).click();
    System.out.println("Check box "+ i + " status is :"+cBox.get(i).isSelected());
   }
   else
   {
    System.out.println("Check box "+ i + " is already selected and status is :"+cBox.get(i).isSelected());
   }
  }
 }

 @BeforeClass
 public void beforeClass() {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }

 @AfterClass
 public void afterClass() throws Exception {
  driver.quit();
 }

}

December 9, 2010

Check boxes count, Checked and Uncecked

December 09, 2010 3
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....


November 24, 2010

How to verify the check box is checked or not?

November 24, 2010 15
How to verify the check box is checked or not?
Here i am writing steps how to verify the check box is checked or not?



import com.thoughtworks.selenium.*;

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


public class Checkbox {
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 testCheckbox()throws Exception {

selenium.open("http://www.google.com");
selenium.windowMaximize();
selenium.click("link=Search settings");
selenium.waitForPageToLoad("50000");
/*Here i am verifying the check box for English. English check
box is already checked so if case will be executed*/
//here pen is the id of the check box
if(selenium.isChecked("pen"))
{
System.out.println("check box already checked");
}
else
{
System.out.println("Check box not checked");
}
}
 @AfterClass
 public void tearDown()throws Exception {
selenium.stop();
seleniumserver.stop();

 }
 }