How to Test Checkboxes
Intro
Checkboxes are an often used element in web applications. This guide will show you how to work with them in your Selenium tests.
Intuitively you may reach for a method that has the word 'checked' in it -- like .checked or .is_checked but
this doesn't exist in Selenium.
A Solution
There are two ways to approach this issue -- by seeing if an element has a checked attribute (a.k.a. performing
an attribute lookup), or by asking an element if it has been selected.
We're going to go through each of these approaches to see their pros and cons.
Example
For reference, here is the markup from the page we will be testing against (an example from the-internet).
<form id="checkboxes">
<input type="checkbox"> checkbox 1<br>
<input type="checkbox" checked> checkbox 2
</form>
In both tests we load the page, find all the checkboxes, and output what Selenium gives us for each of them. Then we assert that the first checkbox is not checked and the last one is.
With the attribute lookup (the first test) we get the value of the checked attribute in the markup: nothing (e.g.,
null, None, or nil) when the checkbox doesn't have the attribute, and a string when it does (an empty one in
this case, since the attribute has no value). Whereas asking if the checkbox is selected (the second test) gives us a
boolean (true or false) response.
There is one more difference. The attribute reflects the markup, so it doesn't change when the user checks or unchecks the checkbox. Being selected reflects the current state of the checkbox.
Expected Behavior
- Open the browser
- Visit the page
- Find all checkboxes on the page
- Assert that the first checkbox is not checked and the last one (the one that is supposed to be checked on initial page load) is
- Close the browser
Summary
Attribute lookups are generally meant for pulling information out of the page for review, and they can tell you whether a checkbox started checked. But there is a method built for this use case that is more readable, has a predictable set of return values, and reflects the current state of the checkbox. Checking if the checkbox is selected should be the thing you reach for.
Happy Testing!
Special Thanks

Authors

