Monday, February 15, 2016

How to work with CheckBox's

To interact with Check box

Go to -> http://www.wufoo.com/html5/types/13-checkbox.html"

Find the number of check box
Get its name
Check whether check box is selected or not 
Click on the check box on "indeterminate" and the quit. 




package com.session3;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class CheckBox {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","yourpath/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.wufoo.com/html5/types/13-checkbox.html");
Thread.sleep(6000);
//driver.findElement(By.xpath("//input[@name='not_checked']")).click();
// click on on all check box's...
// gt dynamic counts of check box...
List<WebElement> cb = driver.findElements(By.xpath("//input[@type='checkbox']"));
System.out.println(cb.size());
// 3 check b...List [0,1,2]
// /* for block..
/* for(int i=0; i<cb.size();i++){
// click on all check box..
cb.get(i).click();
} */
for (int i=0;i<cb.size();i++){
// get the attribute
String val = cb.get(i).getAttribute("name");
System.out.println(val);
boolean a = cb.get(i).isSelected();
System.out.println(a);
// i want to click only on the last checkbox...
// property file cb2=in;
String exp="not_checked";
if(val.equals(exp)){
System.out.println("am in condition will click on indeterminate-->");
cb.get(i).click();
break;
}
}
// how to know whether the chek box is selected or not..
// return true or false...
//boolean a = driver.findElement(By.xpath("//input[@name='checked']")).isSelected();
// System.out.println(a);
// close the browser
driver.close();
}
}
view raw CheckBox.java hosted with ❤ by GitHub

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.