Merge branch 'brynary/master'
This commit is contained in:
commit
0dffbec8f4
|
@ -4,3 +4,7 @@ pkg
|
||||||
doc
|
doc
|
||||||
ri
|
ri
|
||||||
email.txt
|
email.txt
|
||||||
|
.svn
|
||||||
|
log
|
||||||
|
.project
|
||||||
|
.loadpath
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
* Minor enhancements
|
* Minor enhancements
|
||||||
|
|
||||||
|
* Allow clicking links by id and id regexp (gaffo)
|
||||||
* Raise Webrat::PageLoadError when a failure occurs so that application exceptions can be more accurately tested (Ryan Briones)
|
* Raise Webrat::PageLoadError when a failure occurs so that application exceptions can be more accurately tested (Ryan Briones)
|
||||||
|
|
||||||
== 0.3.2 / 2008-11-08
|
== 0.3.2 / 2008-11-08
|
||||||
|
|
2
init.rb
2
init.rb
|
@ -1,3 +1,3 @@
|
||||||
if RAILS_ENV == "test" || RAILS_ENV == "selenium"
|
if (RAILS_ENV =~ /^test/) || RAILS_ENV == "selenium"
|
||||||
require File.join(File.dirname(__FILE__), "lib", "webrat")
|
require File.join(File.dirname(__FILE__), "lib", "webrat")
|
||||||
end
|
end
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
puts IO.read(File.join(File.dirname(__FILE__), 'README'))
|
puts IO.read(File.join(File.dirname(__FILE__), 'README.txt'))
|
||||||
|
|
|
@ -13,16 +13,16 @@ module Webrat
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# We need Nokogiri's CSS to XPath support, even if using REXML
|
# We need Nokogiri's CSS to XPath support, even if using REXML and Hpricot for parsing and searching
|
||||||
require "nokogiri/css"
|
require "nokogiri/css"
|
||||||
|
|
||||||
# Require nokogiri and fall back on rexml
|
# Require nokogiri and fall back on rexml+Hpricot
|
||||||
begin
|
begin
|
||||||
require "nokogiri"
|
require "nokogiri"
|
||||||
require "webrat/core/nokogiri"
|
require "webrat/core/nokogiri"
|
||||||
rescue LoadError => e
|
rescue LoadError => e
|
||||||
|
require "hpricot"
|
||||||
require "rexml/document"
|
require "rexml/document"
|
||||||
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
require "webrat/core"
|
require "webrat/core"
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
require "webrat/core/xml"
|
||||||
require "webrat/core/nokogiri"
|
require "webrat/core/nokogiri"
|
||||||
require "webrat/core/logging"
|
require "webrat/core/logging"
|
||||||
require "webrat/core/flunk"
|
require "webrat/core/flunk"
|
||||||
|
|
|
@ -20,6 +20,8 @@ module Webrat
|
||||||
raise "Invalid field element: #{element.inspect}"
|
raise "Invalid field element: #{element.inspect}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
attr_reader :value
|
||||||
|
|
||||||
def initialize(form, element)
|
def initialize(form, element)
|
||||||
@form = form
|
@form = form
|
||||||
@element = element
|
@element = element
|
||||||
|
@ -111,7 +113,7 @@ module Webrat
|
||||||
end
|
end
|
||||||
|
|
||||||
unless id.blank?
|
unless id.blank?
|
||||||
@label_elements += @form.element.search("label[@for='#{id}']")
|
@label_elements += Webrat::XML.css_search(@form.element, "label[@for='#{id}']")
|
||||||
end
|
end
|
||||||
|
|
||||||
@label_elements
|
@label_elements
|
||||||
|
@ -241,6 +243,10 @@ module Webrat
|
||||||
set(@element["value"] || "on")
|
set(@element["value"] || "on")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def checked?
|
||||||
|
@element["checked"] == "checked"
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def other_options
|
def other_options
|
||||||
|
@ -311,8 +317,8 @@ module Webrat
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def default_value
|
def default_value
|
||||||
selected_options = @element.search(".//option[@selected='selected']")
|
selected_options = Webrat::XML.css_search(@element, "option[@selected='selected']")
|
||||||
selected_options = @element.search(".//option[position() = 1]") if selected_options.empty?
|
selected_options = Webrat::XML.css_search(@element, "option:first") if selected_options.empty?
|
||||||
|
|
||||||
selected_options.map do |option|
|
selected_options.map do |option|
|
||||||
return "" if option.nil?
|
return "" if option.nil?
|
||||||
|
@ -325,7 +331,7 @@ module Webrat
|
||||||
end
|
end
|
||||||
|
|
||||||
def option_elements
|
def option_elements
|
||||||
@element.search(".//option")
|
Webrat::XML.css_search(@element, "option")
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -39,7 +39,7 @@ module Webrat
|
||||||
def fields
|
def fields
|
||||||
return @fields if @fields
|
return @fields if @fields
|
||||||
|
|
||||||
@fields = (@element.search(".//button", ".//input", ".//textarea", ".//select")).collect do |field_element|
|
@fields = Webrat::XML.css_search(@element, "button", "input", "textarea", "select").collect do |field_element|
|
||||||
Field.class_for_element(field_element).new(self, field_element)
|
Field.class_for_element(field_element).new(self, field_element)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,7 +11,11 @@ module Webrat
|
||||||
end
|
end
|
||||||
|
|
||||||
def text
|
def text
|
||||||
@element.inner_text
|
str = @element.inner_text
|
||||||
|
str.gsub!("\n","")
|
||||||
|
str.strip!
|
||||||
|
str.squeeze!(" ")
|
||||||
|
str
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,7 +22,7 @@ module Webrat
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches_text?(link_text)
|
def matches_text?(link_text)
|
||||||
html = text.gsub(' ',' ')
|
html = text.gsub(' ',' ').gsub(' ', ' ')
|
||||||
|
|
||||||
if link_text.is_a?(Regexp)
|
if link_text.is_a?(Regexp)
|
||||||
matcher = link_text
|
matcher = link_text
|
||||||
|
@ -33,11 +33,22 @@ module Webrat
|
||||||
html =~ matcher || title =~ matcher
|
html =~ matcher || title =~ matcher
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def matches_id?(id_or_regexp)
|
||||||
|
if id_or_regexp.is_a?(Regexp)
|
||||||
|
(id =~ id_or_regexp) ? true : false
|
||||||
|
else
|
||||||
|
(id == id_or_regexp) ? true : false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def text
|
def text
|
||||||
@element.inner_html
|
@element.inner_html
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
def id
|
||||||
|
@element['id']
|
||||||
|
end
|
||||||
|
|
||||||
def data
|
def data
|
||||||
authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}
|
authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}
|
||||||
|
@ -54,7 +65,7 @@ module Webrat
|
||||||
def absolute_href
|
def absolute_href
|
||||||
if href =~ /^\?/
|
if href =~ /^\?/
|
||||||
"#{@session.current_url}#{href}"
|
"#{@session.current_url}#{href}"
|
||||||
elsif href !~ %r{^https?://www.example.com(/.*)} && (href !~ /^\//)
|
elsif href !~ %r{^https?://} && (href !~ /^\//)
|
||||||
"#{@session.current_url}/#{href}"
|
"#{@session.current_url}/#{href}"
|
||||||
else
|
else
|
||||||
href
|
href
|
||||||
|
|
|
@ -76,15 +76,15 @@ module Webrat
|
||||||
flunk("Could not find area with name #{area_name}")
|
flunk("Could not find area with name #{area_name}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_link(text) #:nodoc:
|
def find_link(text_or_title_or_id) #:nodoc:
|
||||||
matching_links = links.select do |possible_link|
|
matching_links = links.select do |possible_link|
|
||||||
possible_link.matches_text?(text)
|
possible_link.matches_text?(text_or_title_or_id) || possible_link.matches_id?(text_or_title_or_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
if matching_links.any?
|
if matching_links.any?
|
||||||
matching_links.min { |a, b| a.text.length <=> b.text.length }
|
matching_links.min { |a, b| a.text.length <=> b.text.length }
|
||||||
else
|
else
|
||||||
flunk("Could not find link with text #{text.inspect}")
|
flunk("Could not find link with text or title or id #{text_or_title_or_id.inspect}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,15 +7,7 @@ module Webrat
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches?(stringlike)
|
def matches?(stringlike)
|
||||||
if defined?(Nokogiri::XML)
|
@document = Webrat::XML.document(stringlike)
|
||||||
matches_nokogiri?(stringlike)
|
|
||||||
else
|
|
||||||
matches_rexml?(stringlike)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def matches_rexml?(stringlike)
|
|
||||||
@document = rexml_document(stringlike)
|
|
||||||
@element = @document.inner_text
|
@element = @document.inner_text
|
||||||
|
|
||||||
case @content
|
case @content
|
||||||
|
@ -25,39 +17,6 @@ module Webrat
|
||||||
@element.match(@content)
|
@element.match(@content)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches_nokogiri?(stringlike)
|
|
||||||
@document = Webrat.nokogiri_document(stringlike)
|
|
||||||
@element = @document.inner_text
|
|
||||||
|
|
||||||
case @content
|
|
||||||
when String
|
|
||||||
@element.include?(@content)
|
|
||||||
when Regexp
|
|
||||||
@element.match(@content)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def rexml_document(stringlike)
|
|
||||||
stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)
|
|
||||||
|
|
||||||
case stringlike
|
|
||||||
when REXML::Document
|
|
||||||
stringlike.root
|
|
||||||
when REXML::Node
|
|
||||||
stringlike
|
|
||||||
when StringIO, String
|
|
||||||
begin
|
|
||||||
REXML::Document.new(stringlike.to_s).root
|
|
||||||
rescue REXML::ParseException => e
|
|
||||||
if e.message.include?("second root element")
|
|
||||||
REXML::Document.new("<fake-root-element>#{stringlike}</fake-root-element>").root
|
|
||||||
else
|
|
||||||
raise e
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# ==== Returns
|
# ==== Returns
|
||||||
# String:: The failure message.
|
# String:: The failure message.
|
||||||
|
|
|
@ -45,7 +45,7 @@ module Webrat
|
||||||
@query = query
|
@query = query
|
||||||
end
|
end
|
||||||
|
|
||||||
@document = Webrat.nokogiri_document(stringlike)
|
@document = Webrat::XML.document(stringlike)
|
||||||
matched = @document.xpath(*@query)
|
matched = @document.xpath(*@query)
|
||||||
matched.any? && (!@block || @block.call(matched))
|
matched.any? && (!@block || @block.call(matched))
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,21 +23,21 @@ module Webrat
|
||||||
:within,
|
:within,
|
||||||
:header, :http_accept, :basic_auth,
|
:header, :http_accept, :basic_auth,
|
||||||
:save_and_open_page,
|
:save_and_open_page,
|
||||||
:fill_in,
|
:fills_in, :fill_in,
|
||||||
:check,
|
:checks, :check,
|
||||||
:uncheck,
|
:unchecks, :uncheck,
|
||||||
:choose,
|
:chooses, :choose,
|
||||||
:select,
|
:selects, :select,
|
||||||
:attach_file,
|
:attaches_file, :attach_file,
|
||||||
:cookies,
|
:cookies,
|
||||||
:response,
|
:response,
|
||||||
:current_page,
|
:current_page,
|
||||||
:current_url,
|
:current_url,
|
||||||
:click_link,
|
:clicks_link, :click_link,
|
||||||
:click_area,
|
:clicks_area, :click_area,
|
||||||
:click_button,
|
:clicks_button, :click_button,
|
||||||
:reload, :reloads,
|
:reload, :reloads,
|
||||||
:clicks_link_within,
|
:clicks_link_within, :click_link_within,
|
||||||
:field_labeled
|
:field_labeled
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -123,14 +123,22 @@ module Webrat
|
||||||
# Passing a :method in the options hash overrides the HTTP method used
|
# Passing a :method in the options hash overrides the HTTP method used
|
||||||
# for making the link request
|
# for making the link request
|
||||||
#
|
#
|
||||||
|
# It will try to find links by (in order of precedence):
|
||||||
|
# innerHTML, with simple handling
|
||||||
|
# title
|
||||||
|
# id
|
||||||
|
#
|
||||||
|
# innerHTML and title are matchable by text subtring or Regexp
|
||||||
|
# id is matchable by full text equality or Regexp
|
||||||
|
#
|
||||||
# Example:
|
# Example:
|
||||||
# click_link "Sign up"
|
# click_link "Sign up"
|
||||||
#
|
#
|
||||||
# click_link "Sign up", :javascript => false
|
# click_link "Sign up", :javascript => false
|
||||||
#
|
#
|
||||||
# click_link "Sign up", :method => :put
|
# click_link "Sign up", :method => :put
|
||||||
def click_link(link_text, options = {})
|
def click_link(text_or_title_or_id, options = {})
|
||||||
find_link(link_text).click(options)
|
find_link(text_or_title_or_id).click(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :clicks_link, :click_link
|
alias_method :clicks_link, :click_link
|
||||||
|
@ -166,13 +174,13 @@ module Webrat
|
||||||
|
|
||||||
def page_dom #:nodoc:
|
def page_dom #:nodoc:
|
||||||
return @response.dom if @response.respond_to?(:dom)
|
return @response.dom if @response.respond_to?(:dom)
|
||||||
dom = Webrat.nokogiri_document(@response_body)
|
dom = Webrat::XML.document(@response_body)
|
||||||
Webrat.define_dom_method(@response, dom)
|
Webrat.define_dom_method(@response, dom)
|
||||||
return dom
|
return dom
|
||||||
end
|
end
|
||||||
|
|
||||||
def scoped_dom #:nodoc:
|
def scoped_dom #:nodoc:
|
||||||
Webrat.nokogiri_document(@scope.dom.search(@selector).first.to_html)
|
Webrat::XML.document(@scope.dom.search(@selector).first.to_html)
|
||||||
end
|
end
|
||||||
|
|
||||||
def locate_field(field_locator, *field_types) #:nodoc:
|
def locate_field(field_locator, *field_types) #:nodoc:
|
||||||
|
@ -184,13 +192,13 @@ module Webrat
|
||||||
end
|
end
|
||||||
|
|
||||||
def areas #:nodoc:
|
def areas #:nodoc:
|
||||||
dom.search("area").map do |element|
|
Webrat::XML.css_search(dom, "area").map do |element|
|
||||||
Area.new(@session, element)
|
Area.new(@session, element)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def links #:nodoc:
|
def links #:nodoc:
|
||||||
dom.search("a[@href]").map do |link_element|
|
Webrat::XML.css_search(dom, "a[@href]").map do |link_element|
|
||||||
Link.new(@session, link_element)
|
Link.new(@session, link_element)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -198,7 +206,7 @@ module Webrat
|
||||||
def forms #:nodoc:
|
def forms #:nodoc:
|
||||||
return @forms if @forms
|
return @forms if @forms
|
||||||
|
|
||||||
@forms = dom.search("form").map do |form_element|
|
@forms = Webrat::XML.css_search(dom, "form").map do |form_element|
|
||||||
Form.new(@session, form_element)
|
Form.new(@session, form_element)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -152,8 +152,13 @@ module Webrat
|
||||||
|
|
||||||
alias_method :visits, :visit
|
alias_method :visits, :visit
|
||||||
|
|
||||||
def open_in_browser(path) #:nodoc
|
def open_in_browser(path) # :nodoc
|
||||||
`open #{path}`
|
platform = ruby_platform
|
||||||
|
if platform =~ /cygwin/ || platform =~ /win32/
|
||||||
|
`rundll32 url.dll,FileProtocolHandler #{path.gsub("/", "\\\\")}`
|
||||||
|
elsif platform =~ /darwin/
|
||||||
|
`open #{path}`
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def rewrite_css_and_image_references(response_html) #:nodoc
|
def rewrite_css_and_image_references(response_html) #:nodoc
|
||||||
|
@ -186,5 +191,11 @@ module Webrat
|
||||||
def_delegators :current_scope, :should_see
|
def_delegators :current_scope, :should_see
|
||||||
def_delegators :current_scope, :should_not_see
|
def_delegators :current_scope, :should_not_see
|
||||||
def_delegators :current_scope, :field_labeled
|
def_delegators :current_scope, :field_labeled
|
||||||
|
|
||||||
|
private
|
||||||
|
# accessor for testing
|
||||||
|
def ruby_platform
|
||||||
|
RUBY_PLATFORM
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
module Webrat
|
||||||
|
module XML
|
||||||
|
|
||||||
|
def self.document(stringlike)
|
||||||
|
if defined?(Nokogiri::XML)
|
||||||
|
Webrat.nokogiri_document(stringlike)
|
||||||
|
else
|
||||||
|
return stringlike.dom if stringlike.respond_to?(:dom)
|
||||||
|
|
||||||
|
if Hpricot::Doc === stringlike
|
||||||
|
stringlike
|
||||||
|
elsif Hpricot::Elements === stringlike
|
||||||
|
stringlike
|
||||||
|
elsif StringIO === stringlike
|
||||||
|
Hpricot(stringlike.string)
|
||||||
|
elsif stringlike.respond_to?(:body)
|
||||||
|
Hpricot(stringlike.body.to_s)
|
||||||
|
else
|
||||||
|
Hpricot(stringlike.to_s)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.css_search(element, *searches)
|
||||||
|
if defined?(Nokogiri::XML)
|
||||||
|
element.css(*searches)
|
||||||
|
else
|
||||||
|
searches.map do |search|
|
||||||
|
element.search(search)
|
||||||
|
end.flatten.compact
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,28 +1,42 @@
|
||||||
require "mechanize"
|
require "mechanize"
|
||||||
|
|
||||||
module Webrat
|
module Webrat
|
||||||
class MechanizeSession < Session #:nodoc:
|
class MechanizeSession < Session
|
||||||
|
|
||||||
|
attr_accessor :response
|
||||||
|
alias :page :response
|
||||||
|
|
||||||
def initialize(mechanize = WWW::Mechanize.new)
|
def initialize(mechanize = WWW::Mechanize.new)
|
||||||
super()
|
super()
|
||||||
@mechanize = mechanize
|
@mechanize = mechanize
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(url, data)
|
def get(url, data, headers_argument_not_used = nil)
|
||||||
@mechanize_page = @mechanize.get(url, data)
|
@response = @mechanize.get(url, data)
|
||||||
end
|
end
|
||||||
|
|
||||||
def post(url, data)
|
def post(url, data, headers_argument_not_used = nil)
|
||||||
@mechanize_page = @mechanize.post(url, data)
|
post_data = data.inject({}) do |memo, param|
|
||||||
|
case param.last
|
||||||
|
when Hash
|
||||||
|
param.last.each {|attribute, value| memo["#{param.first}[#{attribute}]"] = value }
|
||||||
|
else
|
||||||
|
memo[param.first] = param.last
|
||||||
|
end
|
||||||
|
memo
|
||||||
|
end
|
||||||
|
@response = @mechanize.post(url, post_data)
|
||||||
end
|
end
|
||||||
|
|
||||||
def response_body
|
def response_body
|
||||||
@mechanize_page.content
|
@response.content
|
||||||
end
|
end
|
||||||
|
|
||||||
def response_code
|
def response_code
|
||||||
@mechanize_page.code.to_i
|
@response.code.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def_delegators :@mechanize, :basic_auth
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
# Supports using the matchers in controller, helper, and view specs if you're
|
||||||
|
# using rspec-rails. Just add a require statement to spec/spec_helper.rb:
|
||||||
|
#
|
||||||
|
# require 'webrat/rspec-rails'
|
||||||
|
#
|
||||||
|
require "webrat/core/matchers"
|
||||||
|
|
||||||
|
Spec::Runner.configure do |config|
|
||||||
|
# rspec should support :type => [:controller, :helper, :view] - but until it does ...
|
||||||
|
config.include(Webrat::Matchers, :type => :controller)
|
||||||
|
config.include(Webrat::Matchers, :type => :helper)
|
||||||
|
config.include(Webrat::Matchers, :type => :view)
|
||||||
|
end
|
|
@ -109,7 +109,22 @@ module Webrat
|
||||||
def dragdrop(*args) #:nodoc:
|
def dragdrop(*args) #:nodoc:
|
||||||
@selenium.dragdrop(*args)
|
@selenium.dragdrop(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fire_event(field_identifier, event)
|
||||||
|
locator = "webrat=#{Regexp.escape(field_identifier)}"
|
||||||
|
@selenium.fire_event(locator, "#{event}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def key_down(field_identifier, key_code)
|
||||||
|
locator = "webrat=#{Regexp.escape(field_identifier)}"
|
||||||
|
@selenium.key_down(locator, key_code)
|
||||||
|
end
|
||||||
|
|
||||||
|
def key_up(field_identifier, key_code)
|
||||||
|
locator = "webrat=#{Regexp.escape(field_identifier)}"
|
||||||
|
@selenium.key_up(locator, key_code)
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def adjust_if_regexp(text_or_regexp) #:nodoc:
|
def adjust_if_regexp(text_or_regexp) #:nodoc:
|
||||||
|
|
|
@ -21,6 +21,14 @@ describe "click_link" do
|
||||||
@session.click_link "Link text", :method => :get
|
@session.click_link "Link text", :method => :get
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should click link on substring" do
|
||||||
|
@session.response_body = <<-EOS
|
||||||
|
<a href="/page">Link text</a>
|
||||||
|
EOS
|
||||||
|
@session.should_receive(:get).with("/page", {})
|
||||||
|
@session.clicks_link "ink tex", :method => :get
|
||||||
|
end
|
||||||
|
|
||||||
it "should click delete links" do
|
it "should click delete links" do
|
||||||
@session.response_body = <<-EOS
|
@session.response_body = <<-EOS
|
||||||
<a href="/page">Link text</a>
|
<a href="/page">Link text</a>
|
||||||
|
@ -54,6 +62,22 @@ describe "click_link" do
|
||||||
@session.click_link /link [a-z]/i
|
@session.click_link /link [a-z]/i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should click links by id" do
|
||||||
|
@session.response_body = <<-EOS
|
||||||
|
<a id="link_text_link" href="/page">Link text</a>
|
||||||
|
EOS
|
||||||
|
@session.should_receive(:get).with("/page", {})
|
||||||
|
@session.clicks_link "link_text_link"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should click links by id regexp" do
|
||||||
|
@session.response_body = <<-EOS
|
||||||
|
<a id="link_text_link" href="/page">Link text</a>
|
||||||
|
EOS
|
||||||
|
@session.should_receive(:get).with("/page", {})
|
||||||
|
@session.clicks_link /_text_/
|
||||||
|
end
|
||||||
|
|
||||||
it "should click rails javascript links with authenticity tokens" do
|
it "should click rails javascript links with authenticity tokens" do
|
||||||
@session.response_body = <<-EOS
|
@session.response_body = <<-EOS
|
||||||
<a href="/posts" onclick="var f = document.createElement('form');
|
<a href="/posts" onclick="var f = document.createElement('form');
|
||||||
|
@ -270,6 +294,15 @@ describe "click_link" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should follow fully qualified local links" do
|
it "should follow fully qualified local links" do
|
||||||
|
@session.stub!(:current_url).and_return("/page")
|
||||||
|
@session.response_body = <<-EOS
|
||||||
|
<a href="http://subdomain.example.com/page/sub">Jump to sub page</a>
|
||||||
|
EOS
|
||||||
|
@session.should_receive(:get).with("http://subdomain.example.com/page/sub", {})
|
||||||
|
@session.click_link "Jump to sub page"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should follow fully qualified local links to example.com" do
|
||||||
@session.response_body = <<-EOS
|
@session.response_body = <<-EOS
|
||||||
<a href="http://www.example.com/page/sub">Jump to sub page</a>
|
<a href="http://www.example.com/page/sub">Jump to sub page</a>
|
||||||
EOS
|
EOS
|
||||||
|
|
|
@ -108,4 +108,21 @@ describe "field_labeled" do
|
||||||
with_an_id_of "element_42", :for => "The Label"
|
with_an_id_of "element_42", :for => "The Label"
|
||||||
should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label"
|
should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "finding a field with it's label containing newlines" do
|
||||||
|
using_this_html <<-HTML
|
||||||
|
<form>
|
||||||
|
<label for="element_42">
|
||||||
|
A label with
|
||||||
|
<a>a link on it's own line</a>
|
||||||
|
</label>
|
||||||
|
<input type="text" id="element_42">
|
||||||
|
</form>
|
||||||
|
HTML
|
||||||
|
|
||||||
|
should_return_a Webrat::TextField, :for => "A label with a link on it's own line"
|
||||||
|
with_an_id_of "element_42", :for => "A label with a link on it's own line"
|
||||||
|
should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
||||||
|
|
||||||
|
require "merb"
|
||||||
|
|
||||||
describe "selects" do
|
describe "selects" do
|
||||||
before do
|
before do
|
||||||
@session = Webrat::TestSession.new
|
@session = Webrat::TestSession.new
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
--diff
|
||||||
|
--color
|
|
@ -3,13 +3,25 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
|
||||||
module Webrat
|
module Webrat
|
||||||
describe CheckboxField do
|
describe CheckboxField do
|
||||||
it "should say it is checked if it is" do
|
it "should say it is checked if it is" do
|
||||||
checkbox = CheckboxField.new(nil, (Webrat.nokogiri_document("<input type='checkbox' checked='checked'>")/'input').first)
|
checkbox = CheckboxField.new(nil, (Webrat::XML.document("<input type='checkbox' checked='checked'>").search('input')).first)
|
||||||
checkbox.should be_checked
|
checkbox.should be_checked
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should say it is not checked if it is not" do
|
it "should say it is not checked if it is not" do
|
||||||
checkbox = CheckboxField.new(nil, (Webrat.nokogiri_document("<input type='checkbox'>")/'input').first)
|
checkbox = CheckboxField.new(nil, (Webrat::XML.document("<input type='checkbox'>").search('input')).first)
|
||||||
checkbox.should_not be_checked
|
checkbox.should_not be_checked
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe RadioField do
|
||||||
|
it "should say it is checked if it is" do
|
||||||
|
radio_button = RadioField.new(nil, (Webrat::XML.document("<input type='radio' checked='checked'>").search('input')).first)
|
||||||
|
radio_button.should be_checked
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should say it is not checked if it is not" do
|
||||||
|
radio_button = RadioField.new(nil, (Webrat::XML.document("<input type='radio'>").search('input')).first)
|
||||||
|
radio_button.should_not be_checked
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
|
||||||
|
|
||||||
|
describe Webrat::Link do
|
||||||
|
# include Webrat::Link
|
||||||
|
|
||||||
|
before do
|
||||||
|
@session = mock(Webrat::TestSession)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should pass through relative urls" do
|
||||||
|
link = Webrat::Link.new(@session, {"href" => "/path"})
|
||||||
|
@session.should_receive(:request_page).with("/path", :get, {})
|
||||||
|
link.click
|
||||||
|
end
|
||||||
|
|
||||||
|
it "shouldnt put base url onto " do
|
||||||
|
url = "https://www.example.com/path"
|
||||||
|
@session.should_receive(:request_page).with(url, :get, {})
|
||||||
|
link = Webrat::Link.new(@session, {"href" => url})
|
||||||
|
link.click
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should matches_text? on regexp" do
|
||||||
|
link = Webrat::Link.new(@session, nil)
|
||||||
|
link.should_receive(:text).and_return("Link Text")
|
||||||
|
link.matches_text?(/link/i).should == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should matches_text? on link_text" do
|
||||||
|
link = Webrat::Link.new(@session, nil)
|
||||||
|
link.should_receive(:text).and_return("Link Text")
|
||||||
|
link.matches_text?("Link Text").should == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should matches_text? on substring" do
|
||||||
|
link = Webrat::Link.new(@session, nil)
|
||||||
|
link.should_receive(:text).and_return("Link Text")
|
||||||
|
link.matches_text?("nk Te").should_not be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not matches_text? on link_text case insensitive" do
|
||||||
|
link = Webrat::Link.new(@session, nil)
|
||||||
|
link.should_receive(:text).and_return("Link Text")
|
||||||
|
link.should_receive(:title).and_return(nil)
|
||||||
|
link.matches_text?("link_text").should == false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should match text including " do
|
||||||
|
link = Webrat::Link.new(@session, nil)
|
||||||
|
link.should_receive(:text).and_return("Link Text")
|
||||||
|
link.matches_text?("Link Text").should == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not matches_text? on wrong text" do
|
||||||
|
link = Webrat::Link.new(@session, nil)
|
||||||
|
link.should_receive(:text).and_return("Some Other Link")
|
||||||
|
link.should_receive(:title).and_return(nil)
|
||||||
|
link.matches_text?("Link Text").should == false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should matches_id? on exact matching id" do
|
||||||
|
link = Webrat::Link.new(@session, nil)
|
||||||
|
link.should_receive(:id).and_return("some_id")
|
||||||
|
link.matches_id?("some_id").should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not matches_id? on incorrect id" do
|
||||||
|
link = Webrat::Link.new(@session, nil)
|
||||||
|
link.should_receive(:id).and_return("other_id")
|
||||||
|
link.matches_id?("some_id").should == false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should matches_id? on matching id by regexp" do
|
||||||
|
link = Webrat::Link.new(@session, nil)
|
||||||
|
link.should_receive(:id).and_return("some_id")
|
||||||
|
link.matches_id?(/some/).should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
|
@ -18,15 +18,30 @@ describe Webrat::Session do
|
||||||
"<html></html>"
|
"<html></html>"
|
||||||
end
|
end
|
||||||
|
|
||||||
session.current_dom.should be_an_instance_of(Nokogiri::HTML::Document)
|
session.current_dom.should respond_to(:search)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should open the page in the browser" do
|
it "should open the page in the browser in MacOSX" do
|
||||||
session = Webrat::Session.new
|
session = Webrat::Session.new
|
||||||
|
session.should_receive(:ruby_platform).and_return 'darwin'
|
||||||
session.should_receive(:`).with("open path")
|
session.should_receive(:`).with("open path")
|
||||||
session.open_in_browser("path")
|
session.open_in_browser("path")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should open the page in the browser in cygwin" do
|
||||||
|
session = Webrat::Session.new
|
||||||
|
session.should_receive(:ruby_platform).and_return 'i386-cygwin'
|
||||||
|
session.should_receive(:`).with("rundll32 url.dll,FileProtocolHandler path\\to\\file")
|
||||||
|
session.open_in_browser("path/to/file")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should open the page in the browser in Win32" do
|
||||||
|
session = Webrat::Session.new
|
||||||
|
session.should_receive(:ruby_platform).and_return 'win32'
|
||||||
|
session.should_receive(:`).with("rundll32 url.dll,FileProtocolHandler path\\to\\file")
|
||||||
|
session.open_in_browser("path/to/file")
|
||||||
|
end
|
||||||
|
|
||||||
it "should provide a current_page for backwards compatibility" do
|
it "should provide a current_page for backwards compatibility" do
|
||||||
session = Webrat::Session.new
|
session = Webrat::Session.new
|
||||||
current_page = session.current_page
|
current_page = session.current_page
|
||||||
|
|
|
@ -12,4 +12,25 @@ describe Webrat::MechanizeSession do
|
||||||
@mech.headers.should == {}
|
@mech.headers.should == {}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "post" do
|
||||||
|
def url
|
||||||
|
'http://test.host/users'
|
||||||
|
end
|
||||||
|
|
||||||
|
def data
|
||||||
|
{:user => {:first_name => 'Nancy', :last_name => 'Callahan'}}
|
||||||
|
end
|
||||||
|
|
||||||
|
def flattened_data
|
||||||
|
{'user[first_name]' => 'Nancy', 'user[last_name]' => 'Callahan'}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should flatten model post data" do
|
||||||
|
mechanize = mock :mechanize
|
||||||
|
mechanize.should_receive(:post).with(url, flattened_data)
|
||||||
|
|
||||||
|
Webrat::MechanizeSession.new(mechanize).post(url, data)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
|
@ -1,75 +1,77 @@
|
||||||
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
||||||
|
|
||||||
describe "Nokogiri Extension" do
|
if defined?(Nokogiri::XML)
|
||||||
include Webrat::Matchers
|
describe "Nokogiri Extension" do
|
||||||
|
include Webrat::Matchers
|
||||||
|
|
||||||
def fail
|
def fail
|
||||||
raise_error(Spec::Expectations::ExpectationNotMetError)
|
raise_error(Spec::Expectations::ExpectationNotMetError)
|
||||||
end
|
end
|
||||||
|
|
||||||
before(:each) do
|
before(:each) do
|
||||||
@text_and_password = <<-HTML
|
@text_and_password = <<-HTML
|
||||||
<div>
|
<div>
|
||||||
<input type="text"/>
|
<input type="text"/>
|
||||||
<input type="password"/>
|
<input type="password"/>
|
||||||
<span type="text"/>
|
<span type="text"/>
|
||||||
</div>
|
</div>
|
||||||
HTML
|
HTML
|
||||||
|
|
||||||
@text_only = <<-HTML
|
@text_only = <<-HTML
|
||||||
<div>
|
<div>
|
||||||
<input type="text" disabled="disabled" />
|
<input type="text" disabled="disabled" />
|
||||||
</div>
|
</div>
|
||||||
HTML
|
HTML
|
||||||
|
|
||||||
@password_only = <<-HTML
|
@password_only = <<-HTML
|
||||||
<div>
|
<div>
|
||||||
<input type="password"/>
|
<input type="password"/>
|
||||||
<div>
|
<div>
|
||||||
HTML
|
HTML
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ":text" do
|
describe ":text" do
|
||||||
it "passes have_selector(:text) if a node with type=text exists" do
|
it "passes have_selector(:text) if a node with type=text exists" do
|
||||||
@text_and_password.should have_selector(":text")
|
@text_and_password.should have_selector(":text")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "passes not have_selector(:text) if no node with text=text exists" do
|
it "passes not have_selector(:text) if no node with text=text exists" do
|
||||||
@password_only.should_not have_selector(":text")
|
@password_only.should_not have_selector(":text")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails have_selector(:text) if no node with type=text exists" do
|
it "fails have_selector(:text) if no node with type=text exists" do
|
||||||
lambda { @password_only.should have_selector(":text") }.should fail
|
lambda { @password_only.should have_selector(":text") }.should fail
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails not have_selector(:text) if a node with type=text exists" do
|
it "fails not have_selector(:text) if a node with type=text exists" do
|
||||||
lambda { @text_only.should_not have_selector(":text") }.should fail
|
lambda { @text_only.should_not have_selector(":text") }.should fail
|
||||||
end
|
end
|
||||||
|
|
||||||
it "works together with other selectors" do
|
it "works together with other selectors" do
|
||||||
@text_and_password.should have_selector("input:text[type*='te']")
|
@text_and_password.should have_selector("input:text[type*='te']")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
describe ":password" do
|
describe ":password" do
|
||||||
it "passes have_selector(:password) if a node with type=password exists" do
|
it "passes have_selector(:password) if a node with type=password exists" do
|
||||||
@text_and_password.should have_selector(":password")
|
@text_and_password.should have_selector(":password")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "passes not have_selector(:text) if no node with text=text exists" do
|
it "passes not have_selector(:text) if no node with text=text exists" do
|
||||||
@text_only.should_not have_selector(":password")
|
@text_only.should_not have_selector(":password")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails have_selector(:password) if no node with type=password exists" do
|
it "fails have_selector(:password) if no node with type=password exists" do
|
||||||
lambda { @text_only.should have_selector(":password") }.should fail
|
lambda { @text_only.should have_selector(":password") }.should fail
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails not have_selector(:password) if a node with type=password exists" do
|
it "fails not have_selector(:password) if a node with type=password exists" do
|
||||||
lambda { @password_only.should_not have_selector(":password") }.should fail
|
lambda { @password_only.should_not have_selector(":password") }.should fail
|
||||||
end
|
end
|
||||||
|
|
||||||
it "works together with other selectors" do
|
it "works together with other selectors" do
|
||||||
@text_and_password.should have_selector("input:password[type*='pa']")
|
@text_and_password.should have_selector("input:password[type*='pa']")
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -57,10 +57,11 @@ describe Webrat::RailsSession do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "the URL is https://" do
|
context "the URL is https://" do
|
||||||
it "should call #https! with true before the request" do
|
it "should call #https! with true before the request and just pass on the path" do
|
||||||
integration_session = mock("integration session", :request_via_redirect => nil)
|
integration_session = mock("integration session")
|
||||||
rails_session = Webrat::RailsSession.new(integration_session)
|
rails_session = Webrat::RailsSession.new(integration_session)
|
||||||
integration_session.should_receive(:https!).with(true)
|
integration_session.should_receive(:https!).with(true)
|
||||||
|
integration_session.should_receive(:request_via_redirect).with(:get, "/url", "data", "headers")
|
||||||
rails_session.get("https://www.example.com/url", "data", "headers")
|
rails_session.get("https://www.example.com/url", "data", "headers")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue