Thursday, 4 February 2016

How to find total no of radio buttons in a page and disabled,enabled radio buttons in selenium webdriver

How to find total no of radio buttons in a page and disabled,enabled radio buttons in selenium web driver:
=======================================================================

package blog;

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

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class LinksDemo {

WebDriver webDriver;
Actions actions;

//declare variables for count sake
int enabledRadioButtonss;
int disabledRadioButtons;

@BeforeSuite
public void launchingIEBrowser() {

System.setProperty("webdriver.chrome.driver", "D:\\Ramesh\\ChromeDriver.exe");
   webDriver = new InternetExplorerDriver();
//webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

}

@Test
public void testScript() throws InterruptedException {
webDriver.get("https://google.com");//facebook.com
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati")).click();

  //get all the links in a page
List<WebElement> links = webDriver.findElements(By.xpath("//*[@type='radion']"));

Iterator<WebElement> iterator = links.iterator();
while (iterator.hasNext()) {

WebElement link = (WebElement)iterator.next();

if (link.isDisplayed()&&link.isEnabled()) {

enabledRadioButtonss++;
}
else
{
disabledRadioButtons++;
}
}

System.out.println("total number of working radio buttons are :" +enabledRadioButtonss);
System.out.println("total number of disabled radio buttons are :" +disabledRadioButtons);

}

@AfterSuite
public void closeBrowser() {
webDriver.quit();

}


}

1 comment:

  1. I was looking for the exact requirement. Thanks a ton ....

    ReplyDelete