Google+

306. Using click( ) - predefined method to select a radio button







click( ) is a predefined method of Selenium WebDriver Class which can also be used to select Radio buttons.

Test Description:

Lets implement the following Test as explained using the comments on the below screenshot:




Lets Implement This:

Pre-requisites:

1. Create a new Java Project say 'WebDriver-Project5' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package5' under the newly created project.
4. Create a Java Class file say 'Class5' under the newly created package as shown below:




Actual Steps:

1. Write the following code into the newly created Java Class file as shown below and make sure that you resolve all the errors before going to next step:




2. The above code will create a Selenium WebDriver object, Launch a new Firefox Browser session, opens the http://book.theautomatedtester.com/chapter1 page in the Browser window and finally it maximizes the Browser window as shown below:



3. Now lets write Selenium WebDriver code for select the radio button in chapter1 page as explained in the above Test Description by following the below steps:

4. Using FireBug options, Inspect the radio button and identify the locator after observing the below code in the below screenshot:






































5. After seeing the above html code in the screenshot, its very clear that we've to identify the radio button using ID locator i.e. using id=radiobutton  in this case.

6. Also we've to use the above ID locator in the Selenium WebDriver Command which is used to select the specified radio button. _driver.findElement(By.id("Locator ID")).click();  is the syntax we've to use for selecting the specified locator.

Lets understand the _driver.findElement(By.id("ID LOCATOR")).click();  syntax by breaking it as below:

  • _driver - is the WebDriver object
  • findElement( )  - is used for locating the elements on which we have to perform the operations
  • By.id("ID LOCATOR") By.id informs the findElement( ) to find the elements using ID LOCATOR
  • Locator - is the actual locator we want to find (i.e. Radio button  in this case )
  • click( ) - is the WebDriver command which can also be used for selecting the radio button


7. So after understanding the things in step5 and step6, its very clear that we've to write _driver.findElement(By.id("radiobutton")).click();  to select the radio button as shown below:



8. Run the Test using JUnit Test as shown below:



9. After the test has run completely, observe that the radio button got selected as shown below:





Watch the below video:

Click here to watch the video.

Download this Project:

Click here to download this project and import into Eclipse IDE  on your machine.



Please comment below to feedback or ask questions.

Using click( ) predefined method for selecting a check box option will be explained in the next post.



No comments: