获取布尔属性的实际值

Get the actual value of a boolean attribute

我有跨度:

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

当我尝试获取 disabled 属性的值时:

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

我得到:

expected: "disabled"
     got: "true"

如何获取指定属性的值而不是布尔值?

Page-Object gem 的 attribute 方法不对属性值进行任何格式化。它只是 returns 从 Selenium-WebDriver(或 Watir-Webdriver)返回的内容。

对于布尔属性,这意味着将返回 true 或 false。来自 Selenium-WebDriver#attribute 文档:

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

如您所见,"disabled" 属性包含在此列表中,因此 returns 是一个布尔值。

如果你真的想检查实际的属性值,你将不得不解析HTML。除非 HTML 很简单,否则我建议使用 Nokogiri(或其他 HTML 解析器)而不是自己编写。在诺科吉里:

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