Skip to main content

_ruby

Example

For reference, here is the markup from the page we will be testing against (an example from the-internet).

<form>
<input type="checkbox"> unchecked<br>
<input type="checkbox" checked=""> checked
</form>

We kick things off by requiring our dependent libraries (e.g., selenium-webdriver to drive the browser, and rspec/expectations & RSpec::Matchers to handle our assertions) and wire up some simple setup, teardown, and run methods to abstract our test configuration.

# filename: checkboxes.rb

require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers

def setup
@driver = Selenium::WebDriver.for :firefox
end

def teardown
@driver.quit
end

def run
setup
yield
teardown
end

Before we write any tests, let's walk through both checkbox approaches to see what Selenium gives us.

To do that we'll want to grab all checkboxes on the page, and iterate through them. Once using an attribute lookup, and again asking if the element is selected -- each time outputting the return value we get from Selenium.

run do
@driver.get 'http://the-internet.herokuapp.com/checkboxes'
checkboxes = @driver.find_elements(css: 'input[type="checkbox"]')

puts "With .attribute('checked')"
checkboxes.each { |checkbox| puts checkbox.attribute('checked').inspect }

puts "\nWith .selected?"
checkboxes.each { |checkbox| puts checkbox.selected?.inspect }
end

When we save our file and run it (e.g., ruby checkboxes.rb from the command-line), here is the output we'll see.

With .attribute('checked')
nil
"true"

With .selected?
false
true

With the attribute lookup, depending on the state of the checkbox, we receive either a nil or a string with the value "true". Whereas with .selected? we get a boolean (true or false) response.

Let's see what these approaches look like when put to use in a test.

run do
@driver.get 'http://the-internet.herokuapp.com/checkboxes'
checkboxes = @driver.find_elements(css: 'input[type="checkbox"]')
expect(checkboxes.last.attribute('checked')).not_to be_nil
# alternatively
expect(checkboxes.last.attribute('checked')).to eql("true")
end

With an attribute lookup, the simplest thing to do is to assert that the return value is not nil. Alternatively we could have checked for the value "true". Let's see what the other approach looks like.

run do
@driver.get 'http://the-internet.herokuapp.com/checkboxes'
checkboxes = @driver.find_elements(css: 'input[type="checkbox"]')
expect(checkboxes.last.selected?).to eql true
end

When checking to see if a checkbox has been selected, it's a straightforward matter of checking for a boolean value.

Expected Behavior

When you save and run the file (e.g., ruby checkboxes.rb from the command-line) here is what will happen:

  • Open the browser
  • Visit the page
  • Find all checkboxes on the page
  • Assert that the last checkbox (the one that is supposed to be checked on initial page load) is checked
  • Close the browser

Summary

Attribute lookups are generally meant for pulling information out of the page for review, however, in this case they lend themselves to seeing if a checkbox is checked. There is a method which was built for this use case that is more readable and has a predictable set of return values. isSelected should be the thing you reach for, knowing that an attribute lookup is there as a solid backup if you find you need it.