开发者

selenium code to select radio button

Am doing automation tesing using selenium, i need help in regardin开发者_如何学Gog how to select radio button.If possible help me with selenium java code.


Assuming you have selenium set up its just:

selenium.click('radio button locator');

You may want to look at the selenium javadoc http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html


click > xpath=(//input[@type='checkbox'])[position()=1]
click > xpath=(//input[@type='checkbox'])[position()=2]
click > xpath=(//input[@type='checkbox'])[position()=3]
click > xpath=(//input[@type='checkbox'])[position()=4]

etc ... use this commands to select random and any radio button


public class radioExamJavaScr {

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

     WebDriver driver = new FirefoxDriver();
     EventFiringWebDriver  dr = new EventFiringWebDriver(driver);
     dr.get("http://www.makemytrip.com/");
     dr.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

     ((JavascriptExecutor)dr).executeScript("document.getElementById('roundtrip_r').click();");

     WebElement one_way = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('oneway_r') ;");

     System.out.println(one_way.isSelected());

     WebElement round_trip = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('roundtrip_r') ;");

     System.out.println(round_trip.isSelected());

      }
 }

In the above example I am selecting radio button with "ROUND TRIP" using "JavaScript".

The last four lines are to verify and see whether the expected radio button is selected in the page or not.

NOTE: I am giving simple easy solution to solve a problem (selecting a radio) in the choosen webpage. A better code can be written. (user can write a method to accept radio ID and loop through all the existing radio button to see which one of them is selected).


I use this method:

String radioButtonId = "radioButtonId";
selenium.focus("id=" + radioButtonId);
selenium.click("id=" + radioButtonId, "concreteRadioButtonValue");


What you can do is this:

Create a method with a WebElement return type, and use the Method findElement(). Example:

WebDriver test;
test = new FirefoxDriver();

public static WebElement checkAndGet(By b) throws Exception {
    return test.findElement(b);
    }

Store the WebElement and use the Method click(). Example:

WebElement radiobutton = checkAndGet(By.xpath("//span[@class='label ng-binding']");
radiobutton.click();

Hope this helps!


Test Scenario : Select Gender(Female) radio button Steps:

  1. Launch new Browser
  2. Open URL http://toolsqa.wpengine.com/automation-practice-form/

  3. Select the Radio button (female) by Value ‘Female’

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

    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver(); // Step 1 - Launch Browser
    driver.get("http://toolsqa.com/automation-practice-form"); // Step 2 - Open Test URL
    List<WebElement> rdBtn_Sex = driver.findElements(By.name("sex")); // 
    int size = rdBtn_Sex.size();
    System.out.println("Total no of radio button :"+size);
    for (int i=0; i< size; i++)
    {
        String sValue = rdBtn_Sex.get(i).getAttribute("value"); // Step 3 - 3.  Select the Radio button (female) by Value ‘Female’ 
        System.out.println("Radio button Name "+sValue);
        if (sValue.equalsIgnoreCase("Female"))
        {
            rdBtn_Sex.get(i).click();

        }
    }


What you should do all the time is provide a locator for every object within a page and then use a method.

Like

driver.findElement(By.xpath(//CLASS[contains(., 'what you are looking for')])).click();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜