_ruby
Example
For this example we will use a dropdown list from the-internet. In this list there a few options to select, one which should be disabled. Let's find this element and assert that it is disabled.
First let's require our dependent libraries (e.g., selenium-webdriver
to control the browser and rspec/expectations
and RSpec::Matchers
for our assertion) and wire up some simple setup
, teardown
, and run
methods.
# filename: dropdown_disabled.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
Now let's wire up our test.
run do
@driver.get 'http://the-internet.herokuapp.com/dropdown'
dropdowns = @driver.find_elements(tag_name: 'option')
item_of_interest = dropdowns.find { |dropdown| dropdown.text == 'Please select an option' }
expect(item_of_interest.enabled?).to eql false
end
After loading the page, we find all the elements that have an option tag (which are all the items in the dropdown list). This will return an array, so we iterate over the collection and find the item we want based on a text comparison.
Once we have the element we want we see if it's enabled (with .enabled?
) and assert based on the response.
Expected Behavior
If you save this file and run it (e.g., ruby dropdown_disabled.rb
from the command-line) here is what will happen:
- Open a browser
- Visit the page
- Grab all dropdown list elements and find the one we want by its text
- Assert that the element is not enabled
- Close the browser
Summary
Hopefully this tip has helped make the task of seeing whether an element is enabled or disabled more approachable.
Happy Testing!