Get the actual value of a boolean attribute

南笙酒味 提交于 2020-01-06 01:30:23

问题


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:


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")


来源:https://stackoverflow.com/questions/30983017/get-the-actual-value-of-a-boolean-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!