Get the actual value of a boolean attribute

646 views Asked by At

I have the span:

<span disabled="disabled">Edit Member</span>

When I try to get the value of the disabled attribute:

page.in_iframe(:id => 'MembersAreaFrame') do |frame|
  expect(page.span_element(:xpath => "//span[text()='Edit Member']", :frame => frame).attribute('disabled')).to eq("disabled")
end

I get:

expected: "disabled"
     got: "true"

How do I get the value of specified attribute instead of a boolean value?

1

There are 1 answers

0
Justin Ko On BEST ANSWER

The Page-Object gem's attribute method does not do any formatting of the attribute value. It simply returns what is returned from Selenium-WebDriver (or Watir-Webdriver).

In the case of boolean attributes, this means that true or false will be returned. From the Selenium-WebDriver#attribute documentation:

The following are deemed to be “boolean” attributes, and will return either “true” or “false”:

async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, spellcheck, truespeed, willvalidate

As you can see, the "disabled" attribute is included in this list and thus returns a boolean.

If you really want to check the actual attribute value, you will have to parse the HTML. Unless the HTML is simple, I would suggest using Nokogiri (or other HTML parser) rather than writing your own. In Nokogiri:

require 'nokogiri'

# Get the HTML of the span
span_html = page.in_iframe(:id => 'MembersAreaFrame') do |frame|
  page.span_element(:xpath => "//span[text()='Edit Member']", :frame => frame).html
end

# Parse the span
doc = Nokogiri::HTML.fragment(span_html)
root_element = doc.at_css('*')

# Check the disabled attribute of the root element (ie the span)
expect(root_element['disabled']).to eq("disabled")