Method: Capybara.string

Defined in:
lib/capybara.rb

.string(html) ⇒ Capybara::Node::Simple

Wraps the given string, which should contain an HTML document or fragment in a Capybara::Node::Simple which exposes all Capybara::Node::Matchers, Capybara::Node::Finders and Capybara::Node::DocumentMatchers. This allows you to query any string containing HTML in the exact same way you would query the current document in a Capybara session.

Examples:

A single element

node = Capybara.string('<a href="foo">bar</a>')
anchor = node.first('a')
anchor[:href] #=> 'foo'
anchor.text #=> 'bar'

Multiple elements

node = Capybara.string "  <ul>\n    <li id=\"home\">Home</li>\n    <li id=\"projects\">Projects</li>\n  </ul>\n"

node.find('#projects').text # => 'Projects'
node.has_selector?('li#home', text: 'Home')
node.has_selector?('#projects')
node.find('ul').find('li:first-child').text # => 'Home'

Parameters:

  • html (String)

    An html fragment or document

Returns:



240
241
242
# File 'lib/capybara.rb', line 240

def string(html)
  Capybara::Node::Simple.new(html)
end