Wednesday, 30 December 2015

Find the Radio Button count and get its text using selenium / how to work on radio buttons using selenium


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;
public class Radiobuttontotal_Pass {

 public static void main(String[] args) throws InterruptedException {
 WebDriver myTestDriver = new FirefoxDriver();
 myTestDriver.get("http://tinyurl.com/cn5pum8");
 myTestDriver.manage().window().maximize();
 myTestDriver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

List<WebElement> RadioNamelist = myTestDriver
 .findElements(By
 .xpath("//label[contains(@class,'z-radio-content')]"));

 System.out.println(RadioNamelist.size());

 for(int i=0;i<RadioNamelist.size();i++){
 //System.out.println(RadioNamelist.get(i).getAttribute("name"));
     System.out.println(RadioNamelist.get(i).getText());
   
 }
 myTestDriver.quit();
 }
}

+++++++++++++++++++++++++


package pageFactory_POM;

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

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ToolsQA_Practice_Page {
static WebDriver driver;
@BeforeTest
public void setUp()
{
    System.setProperty("webdriver.chrome.driver","D:\\selenium_NEW_PRACTICE\\softwares\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void verifyRadioBtnFunctionality()
{
    driver.get("http://toolsqa.com/automation-practice-form/?firstname=&lastname=&sex=Female&photo=&continents=Asia&submit=");
    WebElement userName=driver.findElement(By.cssSelector("input[name=firstname]"));
    userName.sendKeys("pabbu");
    WebElement lastname=driver.findElement(By.cssSelector("input[name=lastname]"));
    lastname.sendKeys("reddy");
    //get radio button count
    //List<WebElement> radioBtnCount= driver.findElements(By.cssSelector("input[name=sex]")); //it also works fine
    List<WebElement> radioBtnCount= driver.findElements(By.cssSelector("input[name=sex][type=radio]"));
    System.out.println("print radio button count ::  " + radioBtnCount.size());
    int count=radioBtnCount.size();
    Assert.assertEquals(count, 2, " :: verify radio button count should be 2");
    //get radio button names
    for(int i=0;i<radioBtnCount.size();i++)
    {
        String radioValue = radioBtnCount.get(i).getAttribute("value");
        // Select the Check Box it the value of the Check Box is same what you are looking for
                if (radioValue.equalsIgnoreCase("Male")){
                    radioBtnCount.get(i).click();
                // This will take the execution out of for loop
                break;
                }
    }
    boolean val= driver.findElement(By.cssSelector("input[name=sex][type=radio]")).isSelected();
    Assert.assertTrue(true, "verify male check box is checked: ");
}
@AfterTest
public void tearDown()
{
    driver.close();
}
}
 

No comments:

Post a Comment