Meaghan Lewis demonstrates how to automate radio buttons and check boxes in WebDriver test scripts. Radio buttons and check boxes are often used in websites when selecting options.
- [Instructor] Let's take a minute to talk about radio buttons. They are used commonly in forms for selecting options. Since radio buttons are used widely in websites and forums, you inevitably need to automate them as well. Radio buttons can be located using a variety of strategies, depending on what HTML is defined for that component. On this page, there are three radio buttons available. I'm gonna walk through examples of using three different strategies to automate these radio buttons. When I inspect these three radio buttons, I find that each button is defined in an input tag and there are various attributes that define each button.
I want to start writing a test now to automate these three radio buttons. I'll go over to IntelliJ and from the welcome menu, I will open the exercise file for this video. I'll start by building the project and once that is successful, I want to focus on the test class, radiobuttons.java. So far, this test class creates a new instance of the ChromeDriver on line 11, navigates to the radio button page on line 13 and then quits the driver.
So, now I wanna start filling in the steps to automate the radio buttons. I'll first focus on the first radio button. I'm going back to the test application, I'll inspect that button, and I notice that this is the only button that has an ID. So I'll start with the strategy to automate this radio button based on its ID. Back in the test on line 15, I'll define a new web element called radioButton1, and I will tell the driver to find this element by the ID of radio-button-1 and then go ahead and add a semi-colon at the end of that line.
On line 16, I wanna click on that radio button, so to do that, I'll type radioButton1.click. Next, I wanna focus on the second radio button, so let's go back to the test application and inspect radio button 2. So for this radio button, there are no IDs, classes, and the name that is shown is not unique and shared between all the other radio buttons. So for this, I'll go ahead and select to use the value to locate radio button 2.
Going back to IntelliJ, I will define a new web element called WebElement, radioButton2 and tell the driver to find this element By.cssSelector and for the cssSelector, I will type input and within brackets I can type value is equal to the value name of option2. And add a semicolon at the end of that line.
And then on my 19, I will want to call radioButton2.click in order to now click on the second radio button. Now I'm gonna go back to the test application and inspect the third radio button. I see that this HTML is pretty much the same as with the second radio button, but for the sake of this example, let's just say that there is nothing else to uniquely identify the radio button and I wanna go ahead and use XPath.
As I've mentioned earlier on in the course, this is not the most recommended locator strategy, but sometimes might be one of the only options. So to find the XPath for an element, what I can do is right-click on it, select copy and then copy Xpath to copy that XPath to my clipboard. Going back to the test on line 21, I will define a new WebElement called radioButton3 and set that equal to driver.findElement By.xpath and then go ahead and paste that XPath in there.
And what this XPath shows is the path of all the tag names used to get to that element, radio button 3. On line 22, I can say radioButton3.click in order to then click on that third radio button. So there are three different ways that you might go about automating radio buttons. Again, it all depends on the application and the locators that are available. This just goes to show that a radio button can be found in a variety of ways, using a variety of locator strategies.
Now that the test is complete, I'll go ahead right-click and select to run Radiobuttons.main(), and the browser will pop up, navigate to the page, and click all three of the radio buttons. I see that the test finished successfully. Checkboxes can also be automated the same way as radio buttons using variety of locator strategies that I showed throughout this video. If you're interested in automating checkboxes, I encourage you to go to the checkbox page in the test application and write a test to automate the three checkboxes there.
If you're interested in automating checkboxes, I encourage you to go to the checkbox page in the test application and write a test to automate the three checkboxes there.
- Using browser drivers and WebDriver
- Inspecting elements
- Selecting a locator
- Combining multiple CSS attributes
- Using common components
- Implicit and explicit waits
- Automating workflows
- Testing behaviors
- Using page objects
- Connecting to GitHub
- Running on the grid
- Using cloud-based testing tools