Webdriver Script026 : Handling Radio Buttons

/*********************************************************************

Script Summary

Title  : Webdriver Script026 : Handling Radio Buttons
Author : Gaurav Khanna

1. Set the Base URL to "http://www.quackit.com/html/codes/html_radio_button.cfm"
2. Retreive the list of checkboxes present.
3. Click on checkbox

 *********************************************************************/


package com.gaurav.webdriver;

import java.util.List;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class s026_HandlingRadioButton {

    // Declaring variable 'webDriver' of WebDriver Type
    WebDriver webDriver;

    // Declaring variable 'baseUrl' of String Type
    String baseUrl;

    @BeforeMethod
    public void startUp() throws Exception {

        // Initializing FireFox Driver
        webDriver = new FirefoxDriver();

        // Assigning URL to variable 'baseUrl'
        baseUrl = "http://www.quackit.com/html/codes/html_radio_button.cfm";
    }

    @Test
    public void tests026_HandingRadioButton() throws Exception {

        webDriver.get(baseUrl);

        List<WebElement> radio_button = webDriver.findElements(By
                .name("preferred_color"));

        System.out.println("radio_button");

        System.out.println("Printing Value : "
                + radio_button.get(2).getAttribute("value"));

        System.out.println("Is Radio Button checked : "
                + radio_button.get(2).getAttribute("checked"));

        // Check the radio button if it is unchecked
        radio_button.get(2).click();

        System.out.println("Is Radio Button checked : "
                + radio_button.get(2).getAttribute("checked"));

    }

    @AfterMethod
    public void shutDown() throws Exception {

        // This will close the browser
        webDriver.quit();
    }

}