Move Capybara.string to capybara.rb

Makes it more officially part of Capybara, instead
of being an afterthought
This commit is contained in:
Jonas Nicklas 2010-11-21 14:07:56 +01:00
parent 87d010394f
commit 81fb593f0c
4 changed files with 29 additions and 6 deletions

View File

@ -129,6 +129,33 @@ module Capybara
end
end
##
#
# Wraps the given string, which should contain an HTML document or fragment
# in a {Capybara::StringNode} which exposes all {Capybara::Node::Matchers} and
# {Capybara::Node::Finders}. This allows you to query any string containing
# HTML in the exact same way you would query the current document in a Capybara
# session. For example:
#
# node = Capybara.string <<-HTML
# <ul>
# <li id="home">Home</li>
# <li id="projects">Projects</li>
# </ul>
# HTML
#
# node.find('#projects').text # => 'Projects'
# node.has_selector?('li#home', :text => 'Home')
# node.has_selector?(:projects)
# node.find('ul').find('li').text # => 'Home'
#
# @param [String] html An html fragment or document
# @return [Capybara::StringNode] A node which has Capybara's finders and matchers
#
def string(html)
StringNode.new(html)
end
def run_default_server(app, port)
begin
require 'rack/handler/thin'
@ -148,6 +175,7 @@ module Capybara
autoload :Server, 'capybara/server'
autoload :Session, 'capybara/session'
autoload :Node, 'capybara/node'
autoload :StringNode, 'capybara/util/string'
autoload :Document, 'capybara/node'
autoload :Element, 'capybara/node'
autoload :Selector, 'capybara/selector'

View File

@ -1,4 +1,3 @@
require 'capybara/util/string'
require 'rack/test'
require 'rack/utils'
require 'mime/types'

View File

@ -1,8 +1,4 @@
module Capybara
def self.string(html)
StringNode.new(Nokogiri::HTML(html))
end
class StringNode
include Capybara::Node::Finders
include Capybara::Node::Matchers
@ -10,6 +6,7 @@ module Capybara
attr_reader :native
def initialize(native)
native = Nokogiri::HTML(native) if native.is_a?(String)
@native = native
end

View File

@ -1,5 +1,4 @@
require 'spec_helper'
require 'capybara/util/string'
describe Capybara do
describe '.string' do