The Issue
Much like textboxes, radio buttons are a commonality when testing browsers. Used in similar circumstances as checkboxes, Radio buttons are most often utilized to make someone select only one option, instead of multiple. We’ll take a look at two basic radio button interactions utilize Ruby & Watir Webdriver/Selenium commands.
The Answer
1 2 3 4 5 |
# select button browser.radio(:id => "radio").set # is button selected? browser.radio(:id => "radio").set? |
These Watir Webdriver commands will select a radio button, and check if it is validated. Lets check these out in action.
The Code
We’ll be testing the radio buttons on the Watir Webdriver demo page.
1 2 3 4 5 6 |
#testradio.rb require 'rubygems' require 'watir-webdriver' browser = Watir::Browser.new :chrome browser.goto "bit.ly/watir-webdriver-demo" browser.radio(:value,"A gem").set puts "Radio Button Set?: #{browser.radio(:value,"A gem").set?} |
The Result
We can save the file (testradio.rb) and run it from the command line, with the following result:
- Browser opens
- Browser navigates to the Watir Webdriver demo page
- Checks radio button for “A gem
- Returns the current status of the radio button
The Takeaway
Radio buttons are an important element to know how to deal with; using this pair of Ruby & Watir Webdriver/Selenium commands, you can start validating them on your website/app.