Saturday, 18 February 2012

Working with CheckBoxes using webdriver for selenium automation testing


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class dadabhagwan_LearnSeleniumWithJasmine {

public static void main(String[] args) throws InterruptedException {

WebDriver myDriver = new FirefoxDriver();
myDriver.get("http://egroup.dadabhagwan.org/Register.aspx");

myDriver.findElement(By.xpath("//*[@id='ctl00_cpMaster_chkNews_0']")).click();
System.out.println(myDriver.findElement(By.xpath("//*[@id='ctl00_cpMaster_chkNews_1']")).getAttribute("checked"));
System.out.println(myDriver.findElement(By.xpath("//*[@id='ctl00_cpMaster_chkNews_0']")).getAttribute("checked"));

String xpath_SubscriptionInformation_front = "//*[@id='ctl00_cpMaster_chkNews_";
String xpath_SubscriptionInformation_rear = "']";

for(int i=0;i<=9;i++){
System.out.print(myDriver.findElement(By.xpath(xpath_SubscriptionInformation_front+i+xpath_SubscriptionInformation_rear)).getAttribute("name")+"---------");
System.out.println(myDriver.findElement(By.xpath(xpath_SubscriptionInformation_front+i+xpath_SubscriptionInformation_rear)).getAttribute("checked"));
myDriver.findElement(By.xpath(xpath_SubscriptionInformation_front+i+xpath_SubscriptionInformation_rear)).click();
System.out.print(myDriver.findElement(By.xpath(xpath_SubscriptionInformation_front+i+xpath_SubscriptionInformation_rear)).getAttribute("name")+"---------");
System.out.println(myDriver.findElement(By.xpath(xpath_SubscriptionInformation_front+i+xpath_SubscriptionInformation_rear)).getAttribute("checked"));
}

}

}