Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: brynary/webrat
...
head fork: foeken/webrat
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 10 commits
  • 11 files changed
  • 0 commit comments
  • 2 contributors
View
3  .gitignore
@@ -1,5 +1,6 @@
+*.svn
coverage
pkg
doc
ri
-email.txt
+email.txt
View
66 Manifest.txt
@@ -1,41 +1,49 @@
-.gitignore
History.txt
MIT-LICENSE.txt
Manifest.txt
README.txt
Rakefile
TODO.txt
-coverage/index.html
-coverage/lib-webrat-field_rb.html
-coverage/lib-webrat-form_rb.html
-coverage/lib-webrat-label_rb.html
-coverage/lib-webrat-link_rb.html
-coverage/lib-webrat-logging_rb.html
-coverage/lib-webrat-page_rb.html
-coverage/lib-webrat-redirect_actions_rb.html
-coverage/lib-webrat-select_option_rb.html
-coverage/lib-webrat_rb.html
init.rb
install.rb
lib/webrat.rb
-lib/webrat/field.rb
-lib/webrat/form.rb
-lib/webrat/label.rb
-lib/webrat/link.rb
-lib/webrat/logging.rb
-lib/webrat/page.rb
-lib/webrat/redirect_actions.rb
-lib/webrat/select_option.rb
-spec/attaches_file_spec.rb
-spec/checks_spec.rb
-spec/chooses_spec.rb
-spec/clicks_button_spec.rb
-spec/clicks_link_spec.rb
-spec/fills_in_spec.rb
+lib/webrat/core.rb
+lib/webrat/core/field.rb
+lib/webrat/core/flunk.rb
+lib/webrat/core/form.rb
+lib/webrat/core/label.rb
+lib/webrat/core/link.rb
+lib/webrat/core/logging.rb
+lib/webrat/core/popups.rb
+lib/webrat/core/scope.rb
+lib/webrat/core/select_option.rb
+lib/webrat/core/session.rb
+lib/webrat/mechanize.rb
+lib/webrat/mechanize/mechanize_session.rb
+lib/webrat/rails.rb
+lib/webrat/rails/rails_session.rb
+lib/webrat/rails/redirect_actions.rb
+lib/webrat/rails/session.rb
+lib/webrat/selenium.rb
+lib/webrat/selenium/selenium_session.rb
+mechanize_spike.rb
+selenium_spike.rb
+spec/api/attaches_file_spec.rb
+spec/api/checks_spec.rb
+spec/api/chooses_spec.rb
+spec/api/clicks_button_spec.rb
+spec/api/clicks_link_spec.rb
+spec/api/fills_in_spec.rb
+spec/api/reloads_spec.rb
+spec/api/save_and_open_spec.rb
+spec/api/selects_spec.rb
+spec/api/submits_form_spec.rb
+spec/api/visits_spec.rb
+spec/api/within_spec.rb
+spec/fakes/test_session.rb
+spec/integration/rails_spec.rb
spec/rcov.opts
-spec/reloads_spec.rb
-spec/save_and_open_page_spec.rb
-spec/selects_spec.rb
spec/spec.opts
spec/spec_helper.rb
-spec/visits_spec.rb
+spec/webrat/core/logging_spec.rb
+spec/webrat/core/session_spec.rb
View
21 README.txt
@@ -1,11 +1,9 @@
-Webrat
-======
+= Webrat
- [Code on GitHub](http://github.com/brynary/webrat)
- [Tickets on Lighthouse](http://webrat.lighthouseapp.com/)
-Description
------------
+== Description
Webrat (_Ruby Acceptance Testing for Web applications_)
lets you quickly write robust and thorough acceptance tests for a Ruby
@@ -23,8 +21,7 @@ run your tests much faster and more frequently.
Initial development was sponsored by [EastMedia](http://www.eastmedia.com).
-Synopsis
---------
+== Synopsis
def test_sign_up
visits "/"
@@ -62,8 +59,7 @@ tests to break unnecessarily as your application evolves:
A test written with Webrat can handle these changes to these without any modifications.
-Install
--------
+== Install
To install the latest release:
@@ -75,23 +71,20 @@ In your stories/helper.rb:
You could also unpack the gem into vendor/plugins.
-Requirements
-------------
+== Requirements
- Rails >= 1.2.6
- Hpricot >= 0.6
- Rails integration tests in Test::Unit _or_
- RSpec stories (using an RSpec version >= revision 2997)
-Authors
--------
+== Authors
- Maintained by [Bryan Helmkamp](mailto:bryan@brynary.com)
- Original code written by [Seth Fitzsimmons](mailto:seth@mojodna.net)
- Many other contributors. See attributions in History.txt
-License
--------
+== License
Copyright (c) 2007 Bryan Helmkamp, Seth Fitzsimmons.
See MIT-LICENSE.txt in this directory.
View
58 lib/webrat/core/form.rb
@@ -1,7 +1,7 @@
module Webrat
class Form
attr_reader :element
-
+
def initialize(session, element)
@session = session
@element = element
@@ -10,13 +10,13 @@ def initialize(session, element)
def find_field(id_or_name_or_label, *field_types)
possible_fields = fields_by_type(field_types)
-
+
find_field_by_id(possible_fields, id_or_name_or_label) ||
find_field_by_name(possible_fields, id_or_name_or_label) ||
find_field_by_label(possible_fields, id_or_name_or_label) ||
nil
end
-
+
def find_select_option(option_text)
select_fields = fields_by_type([SelectField])
@@ -30,83 +30,87 @@ def find_select_option(option_text)
def find_button(value = nil)
return fields_by_type([ButtonField]).first if value.nil?
-
+
possible_buttons = fields_by_type([ButtonField])
-
+
possible_buttons.each do |possible_button|
return possible_button if possible_button.matches_value?(value)
end
-
+
nil
end
def fields
return @fields if @fields
-
+
@fields = []
-
+
(@element / "button, input, textarea, select").each do |field_element|
@fields << Field.class_for_element(field_element).new(self, field_element)
end
-
+
@fields
end
-
+
+ def onsubmit
+ @element["onsubmit"]
+ end
+
def submit
@session.request_page(form_action, form_method, params)
end
protected
-
+
def find_field_by_id(possible_fields, id)
possible_fields.each do |possible_field|
return possible_field if possible_field.matches_id?(id)
end
-
+
nil
end
-
+
def find_field_by_name(possible_fields, name)
possible_fields.each do |possible_field|
return possible_field if possible_field.matches_name?(name)
end
-
+
nil
end
-
- def find_field_by_label(possible_fields, label)
+
+ def find_field_by_label(possible_fields, label)
matching_fields = []
-
+
possible_fields.each do |possible_field|
matching_fields << possible_field if possible_field.matches_label?(label)
end
-
+
matching_fields.sort_by { |f| f.label_text.length }.first
end
-
+
def fields_by_type(field_types)
fields.select { |f| field_types.include?(f.class) }
end
-
+
def params
all_params = {}
-
+
fields.each do |field|
next if field.to_param.nil?
merge(all_params, field.to_param)
end
-
+
all_params
end
-
+
def form_method
@element["method"].blank? ? :get : @element["method"].downcase
end
-
+
def form_action
@element["action"].blank? ? @session.current_url : @element["action"]
end
-
+
def merge(all_params, new_param)
new_param.each do |key, value|
case all_params[key]
@@ -119,7 +123,7 @@ def merge(all_params, new_param)
end
end
end
-
+
def merge_hash_values(a, b) # :nodoc:
a.keys.each do |k|
if b.has_key?(k)
@@ -135,6 +139,6 @@ def merge_hash_values(a, b) # :nodoc:
end
a.merge!(b)
end
-
+
end
end
View
34 lib/webrat/core/link.rb
@@ -13,6 +13,16 @@ def click(method = nil, options = {})
options[:javascript] = true if options[:javascript].nil?
if options[:javascript]
+
+ unless options[:confirm_popup]
+ confirm_match = onclick.match(/confirm\(['"](.*)['"]\)/i) if onclick
+ if confirm_match
+ confirm_text = confirm_match[1]
+ @session.show_confirm_popup(confirm_text,self,method)
+ return
+ end
+ end
+
@session.request_page(absolute_href, method, data)
else
@session.request_page(absolute_href, :get, {})
@@ -25,6 +35,14 @@ def matches_text?(link_text)
html =~ matcher || title =~ matcher
end
+ def matches_href?(href_text)
+ href.downcase == href_text.downcase
+ end
+
+ def matches_method?(method=:get)
+ http_method == method
+ end
+
def text
@element.innerHTML
end
@@ -66,7 +84,7 @@ def onclick
end
def http_method
- if !onclick.blank? && onclick.include?("f.submit()")
+ if !onclick.blank? && ( onclick.include?("f.submit()") || onclick.include?("Ajax.Request") )
http_method_from_js_form
else
:get
@@ -76,11 +94,25 @@ def http_method
def http_method_from_js_form
if onclick.include?("m.setAttribute('name', '_method')")
http_method_from_fake_method_param
+ elsif onclick.include?("Ajax.Request")
+ http_method_from_ajax_request_hash
else
:post
end
end
+ def http_method_from_ajax_request_hash
+ if onclick.include?("method:'delete'")
+ :delete
+ elsif onclick.include?("method:'post'")
+ :post
+ elsif onclick.include?("method:'put'")
+ :put
+ else
+ raise "No HTTP method for method param in #{onclick.inspect}"
+ end
+ end
+
def http_method_from_fake_method_param
if onclick.include?("m.setAttribute('value', 'delete')")
:delete
View
52 lib/webrat/core/popups.rb
@@ -0,0 +1,52 @@
+module Webrat
+
+ class Popup
+ BUTTON_OK = "OK"
+ BUTTON_CANCEL = "CANCEL"
+ end
+
+ class Confirm < Popup
+
+ attr_reader :message
+ attr_reader :link
+ attr_reader :method
+
+ def initialize(message, link, method)
+ @message = message
+ @link = link
+ @method = method
+ end
+
+ def press_button button=BUTTON_OK
+ case button
+ when BUTTON_OK
+ link.click(method, :confirm_popup => true)
+ when BUTTON_CANCEL
+ return
+ else
+ flunk("Cannot find that button")
+ end
+ end
+ end
+
+ class FormConfirm < Confirm
+
+ attr_reader :form
+ def initialize(message, form)
+ @form = form
+ end
+
+ def press_button button=BUTTON_OK
+ case button
+ when BUTTON_OK
+ form.submit
+ when BUTTON_CANCEL
+ return
+ else
+ flunk("Cannot find that button")
+ end
+ end
+
+ end
+
+end
View
152 lib/webrat/core/scope.rb
@@ -4,13 +4,32 @@ module Webrat
class Scope
include Logging
include Flunk
-
+
+ attr_reader :visible_popup
+ attr_reader :pending_form
+
def initialize(session, html, selector = nil)
@session = session
@html = html
@selector = selector
end
-
+
+ # Returns true if a popup is in the way
+ def blocked_by_popup?
+ !!(@visible_popup || @pending_form)
+ end
+
+ # Show a popup of type confirm
+ def show_confirm_popup( message, link, method )
+ @visible_popup = Confirm.new(message, link, method)
+ end
+
+ # Show a popup of type confirm
+ def show_form_confirm_popup( message, form )
+ @pending_form = FormConfirm.new(message, form)
+ end
+
+
# Verifies an input field or textarea exists on the current page, and stores a value for
# it which will be sent when the form is submitted.
#
@@ -22,44 +41,48 @@ def initialize(session, html, selector = nil)
# <tt>field</tt> can be either the value of a name attribute (i.e. <tt>user[email]</tt>)
# or the text inside a <tt><label></tt> element that points at the <tt><input></tt> field.
def fills_in(id_or_name_or_label, options = {})
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
find_field(id_or_name_or_label, TextField, TextareaField, PasswordField).set(options[:with])
end
alias_method :fill_in, :fills_in
-
+
# Verifies that an input checkbox exists on the current page and marks it
# as checked, so that the value will be submitted with the form.
#
# Example:
# checks 'Remember Me'
def checks(id_or_name_or_label)
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
find_field(id_or_name_or_label, CheckboxField).check
end
alias_method :check, :checks
-
+
# Verifies that an input checkbox exists on the current page and marks it
# as unchecked, so that the value will not be submitted with the form.
#
# Example:
# unchecks 'Remember Me'
def unchecks(id_or_name_or_label)
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
find_field(id_or_name_or_label, CheckboxField).uncheck
end
alias_method :uncheck, :unchecks
-
+
# Verifies that an input radio button exists on the current page and marks it
# as checked, so that the value will be submitted with the form.
#
# Example:
# chooses 'First Option'
def chooses(label)
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
find_field(label, RadioField).choose
end
alias_method :choose, :chooses
-
+
# Verifies that a an option element exists on the current page with the specified
# text. You can optionally restrict the search to a specific select list by
# assigning <tt>options[:from]</tt> the value of the select list's name or
@@ -70,11 +93,12 @@ def chooses(label)
# selects "February", :from => "event_month"
# selects "February", :from => "Event Month"
def selects(option_text, options = {})
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
find_select_option(option_text, options[:from]).choose
end
alias_method :select, :selects
-
+
# Verifies that an input file field exists on the current page and sets
# its value to the given +file+, so that the file will be uploaded
# along with the form. An optional <tt>content_type</tt> may be given.
@@ -83,15 +107,16 @@ def selects(option_text, options = {})
# attaches_file "Resume", "/path/to/the/resume.txt"
# attaches_file "Photo", "/path/to/the/image.png", "image/png"
def attaches_file(id_or_name_or_label, path, content_type = nil)
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
find_field(id_or_name_or_label, FileField).set(path, content_type)
end
alias_method :attach_file, :attaches_file
-
+
# Issues a request for the URL pointed to by a link on the current page,
# follows any redirects, and verifies the final page load was successful.
#
- # clicks_link has very basic support for detecting Rails-generated
+ # clicks_link has very basic support for detecting Rails-generated
# JavaScript onclick handlers for PUT, POST and DELETE links, as well as
# CSRF authenticity tokens if they are present.
#
@@ -102,51 +127,56 @@ def attaches_file(id_or_name_or_label, path, content_type = nil)
#
# clicks_link "Sign up", :javascript => false
def clicks_link(link_text, options = {})
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
find_link(link_text).click(nil, options)
end
alias_method :click_link, :clicks_link
-
+
# Works like clicks_link, but forces a GET request
- #
+ #
# Example:
# clicks_get_link "Log out"
def clicks_get_link(link_text)
- find_link(link_text).click(:get)
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
+ find_link(link_text, :get).click(:get)
end
alias_method :click_get_link, :clicks_get_link
-
+
# Works like clicks_link, but issues a DELETE request instead of a GET
- #
+ #
# Example:
# clicks_delete_link "Log out"
def clicks_delete_link(link_text)
- find_link(link_text).click(:delete)
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
+ find_link(link_text, :delete).click(:delete)
end
alias_method :click_delete_link, :clicks_delete_link
-
+
# Works like clicks_link, but issues a POST request instead of a GET
- #
+ #
# Example:
# clicks_post_link "Vote"
def clicks_post_link(link_text)
- find_link(link_text).click(:post)
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
+ find_link(link_text, :post).click(:post)
end
alias_method :click_post_link, :clicks_post_link
-
+
# Works like clicks_link, but issues a PUT request instead of a GET
- #
+ #
# Example:
# clicks_put_link "Update profile"
def clicks_put_link(link_text)
- find_link(link_text).click(:put)
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
+ find_link(link_text, :put).click(:put)
end
alias_method :click_put_link, :clicks_put_link
-
+
# Verifies that a submit button exists for the form, then submits the form, follows
# any redirects, and verifies the final page was successful.
#
@@ -157,25 +187,63 @@ def clicks_put_link(link_text)
# The URL and HTTP method for the form submission are automatically read from the
# <tt>action</tt> and <tt>method</tt> attributes of the <tt><form></tt> element.
def clicks_button(value = nil)
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
find_button(value).click
end
alias_method :click_button, :clicks_button
-
+
+ def submits_form(form_id = nil) # :nodoc:
+
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if blocked_by_popup?
+
+ forms.each do |form|
+ confirm_match = form.onsubmit.match(/confirm\(['"](.*)['"]\)/i) if form.onsubmit
+ if confirm_match
+ confirm_text = confirm_match[1]
+ @session.show_form_confirm_popup(confirm_text, form)
+ return
+ end
+
+ if !form_id || form.element["id"] == form_id
+ form.submit
+ return
+ end
+ end
+
+ flunk("Could not find form to submit")
+ end
+
+ alias_method :submit_form, :submits_form
+
+ def dismisses_popup( with_button=Popup::BUTTON_OK )
+ if @visible_popup
+ @visible_popup.press_button(with_button)
+ @visible_popup = nil
+ elsif @pending_form
+ @pending_form.press_button(with_button)
+ @pending_form = nil
+ else
+ flunk("No popup to dismiss")
+ end
+ end
+
+ alias_method :dismiss_popup, :dismisses_popup
+
def dom # :nodoc:
return @dom if defined?(@dom) && @dom
@dom = Hpricot(@html)
-
+
if @selector
html = (@dom / @selector).first.to_html
@dom = Hpricot(html)
end
-
+
return @dom
end
-
+
protected
-
+
def find_select_option(option_text, id_or_name_or_label)
if id_or_name_or_label
field = find_field(id_or_name_or_label, SelectField)
@@ -186,55 +254,57 @@ def find_select_option(option_text, id_or_name_or_label)
return result if result
end
end
-
+
flunk("Could not find option #{option_text.inspect}")
end
-
+
def find_button(value)
forms.each do |form|
button = form.find_button(value)
return button if button
end
-
+
flunk("Could not find button #{value.inspect}")
end
- def find_link(text, selector = nil)
+ def find_link(text, method = :get, selector = nil)
matching_links = []
-
+
links_within(selector).each do |possible_link|
- matching_links << possible_link if possible_link.matches_text?(text)
+ if possible_link.matches_text?(text) || ( possible_link.matches_href?(text) && possible_link.matches_method?(method) )
+ matching_links << possible_link
+ end
end
-
+
if matching_links.any?
matching_links.sort_by { |l| l.text.length }.first
else
flunk("Could not find link with text #{text.inspect}")
end
end
-
+
def find_field(id_or_name_or_label, *field_types)
forms.each do |form|
result = form.find_field(id_or_name_or_label, *field_types)
return result if result
end
-
+
flunk("Could not find #{field_types.inspect}: #{id_or_name_or_label.inspect}")
end
-
+
def links_within(selector)
(dom / selector / "a[@href]").map do |link_element|
Link.new(@session, link_element)
end
end
-
+
def forms
return @forms if @forms
-
+
@forms = (dom / "form").map do |form_element|
Form.new(@session, form_element)
end
end
-
+
end
-end
+end
View
12 lib/webrat/core/session.rb
@@ -83,6 +83,7 @@ def current_scope
# Example:
# reloads
def reloads
+ flunk("Popup with message: '#{@visible_popup.message}' is in the way!") if @scope && @scope.blocked_by_popup?
request_page(@current_url, @http_method, @data)
end
@@ -106,6 +107,7 @@ def within(selector)
end
def visits(url = nil, http_method = :get, data = {})
+ flunk("A popup with message: #{@current_page.visible_popup.message} is in the way!") if @scope && @scope.blocked_by_popup?
request_page(url, http_method, data)
end
@@ -119,7 +121,7 @@ def rewrite_css_and_image_references(response_html) # :nodoc
return response_html unless doc_root
response_html.gsub(/"\/(stylesheets|images)/, doc_root + '/\1')
end
-
+
def_delegators :current_scope, :fill_in, :fills_in
def_delegators :current_scope, :check, :checks
def_delegators :current_scope, :uncheck, :unchecks
@@ -132,6 +134,14 @@ def rewrite_css_and_image_references(response_html) # :nodoc
def_delegators :current_scope, :click_post_link, :clicks_post_link
def_delegators :current_scope, :click_put_link, :clicks_put_link
def_delegators :current_scope, :click_button, :clicks_button
+ def_delegators :current_scope, :submit_form, :submits_form
+ def_delegators :current_scope, :dismiss_popup, :dismisses_popup
+
+ def_delegators :current_scope, :blocked_by_popup?
+ def_delegators :current_scope, :show_confirm_popup
+ def_delegators :current_scope, :show_form_confirm_popup
+ def_delegators :current_scope, :visible_popup
+ def_delegators :current_scope, :pending_form
end
end
View
18 lib/webrat/selenium/selenium_session.rb
@@ -2,16 +2,22 @@ module Webrat
class SeleniumSession < Session
def initialize(selenium_driver)
- super
+ super()
@selenium = selenium_driver
define_location_strategies
end
+ def selenium
+ @selenium
+ end
+
def visits(url)
+ flunk("Popup with message: '#{browser.popup_message}' is in the way!") if blocked_by_popup?
@selenium.open(url)
end
def fills_in(field_identifier, options)
+ flunk("Popup with message: '#{browser.popup_message}' is in the way!") if blocked_by_popup?
locator = "webrat=#{Regexp.escape(field_identifier)}"
@selenium.type(locator, "#{options[:with]}")
end
@@ -21,18 +27,21 @@ def response_body
end
def clicks_button(button_text = nil, options = {})
+ flunk("Popup with message: '#{browser.popup_message}' is in the way!") if blocked_by_popup?
button_text, options = nil, button_text if button_text.is_a?(Hash) && options == {}
- button_text ||= '*'
+ button_text ||= '*'
@selenium.click("button=#{button_text}")
wait_for_result(options[:wait])
end
def clicks_link(link_text, options = {})
+ flunk("Popup with message: '#{browser.popup_message}' is in the way!") if blocked_by_popup?
@selenium.click("webratlink=#{link_text}")
wait_for_result(options[:wait])
end
def clicks_link_within(selector, link_text, options = {})
+ flunk("Popup with message: '#{browser.popup_message}' is in the way!") if blocked_by_popup?
@selenium.click("webratlinkwithin=#{selector}|#{link_text}")
wait_for_result(options[:wait])
end
@@ -65,6 +74,7 @@ def wait_for_ajax_and_effects
end
def selects(option_text, options = {})
+ flunk("Popup with message: '#{browser.popup_message}' is in the way!") if blocked_by_popup?
id_or_name_or_label = options[:from]
if id_or_name_or_label
@@ -76,11 +86,13 @@ def selects(option_text, options = {})
end
def chooses(label_text)
+ flunk("Popup with message: '#{browser.popup_message}' is in the way!") if blocked_by_popup?
@selenium.click("webrat=#{label_text}")
end
def checks(label_text)
- @selenium.check("webrat=#{label_text}")
+ flunk("Popup with message: '#{browser.popup_message}' is in the way!") if blocked_by_popup?
+ @selenium.click("webrat=#{label_text}")
end
protected
View
77 spec/api/clicks_link_spec.rb
@@ -20,7 +20,7 @@
@session.should_receive(:get).with("/page", {})
@session.clicks_get_link "Link text"
end
-
+
it "should click delete links" do
@session.response_body = <<-EOS
<a href="/page">Link text</a>
@@ -28,8 +28,8 @@
@session.should_receive(:delete).with("/page", {})
@session.clicks_delete_link "Link text"
end
-
-
+
+
it "should click post links" do
@session.response_body = <<-EOS
<a href="/page">Link text</a>
@@ -37,7 +37,7 @@
@session.should_receive(:post).with("/page", {})
@session.clicks_post_link "Link text"
end
-
+
it "should click put links" do
@session.response_body = <<-EOS
<a href="/page">Link text</a>
@@ -45,7 +45,34 @@
@session.should_receive(:put).with("/page", {})
@session.clicks_put_link "Link text"
end
-
+
+ it "should click links with confirmation" do
+ @session.response_body = <<-EOS
+ <a href="/page" onclick="confirm('boo');">Link with confirmation</a>
+ EOS
+ @session.should_receive(:get).with("/page", {})
+ @session.clicks_link "Link with confirmation"
+ @session.blocked_by_popup?.should be_true
+
+ @session.dismisses_popup
+ @session.blocked_by_popup?.should be_false
+
+ @session.clicks_link "Link with confirmation"
+ @session.blocked_by_popup?.should be_true
+
+ lambda {
+ @session.dismisses_popup("non existant")
+ }.should raise_error
+
+ @session.blocked_by_popup?.should be_true
+ @session.dismisses_popup(Webrat::Popup::BUTTON_CANCEL)
+ @session.blocked_by_popup?.should be_false
+
+ lambda {
+ @session.dismisses_popup(Webrat::Popup::BUTTON_CANCEL)
+ }.should raise_error
+ end
+
it "should click rails javascript links with authenticity tokens" do
@session.response_body = <<-EOS
<a href="/posts" onclick="var f = document.createElement('form');
@@ -64,7 +91,7 @@
@session.should_receive(:post).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62")
@session.clicks_link "Posts"
end
-
+
it "should click rails javascript delete links" do
@session.response_body = <<-EOS
<a href="/posts/1" onclick="var f = document.createElement('form');
@@ -83,7 +110,7 @@
@session.should_receive(:delete).with("/posts/1", {})
@session.clicks_link "Delete"
end
-
+
it "should click rails javascript post links" do
@session.response_body = <<-EOS
<a href="/posts" onclick="var f = document.createElement('form');
@@ -97,7 +124,7 @@
@session.should_receive(:post).with("/posts", {})
@session.clicks_link "Posts"
end
-
+
it "should click rails javascript post links without javascript" do
@session.response_body = <<-EOS
<a href="/posts" onclick="var f = document.createElement('form');
@@ -111,7 +138,7 @@
@session.should_receive(:get).with("/posts", {})
@session.clicks_link "Posts", :javascript => false
end
-
+
it "should click rails javascript put links" do
@session.response_body = <<-EOS
<a href="/posts" onclick="var f = document.createElement('form');
@@ -130,7 +157,7 @@
@session.should_receive(:put).with("/posts", {})
@session.clicks_link "Put"
end
-
+
it "should fail if the javascript link doesn't have a value for the _method input" do
@session.response_body = <<-EOS
<a href="/posts/1" onclick="var f = document.createElement('form');
@@ -145,12 +172,12 @@
f.submit();
return false;">Link</a>
EOS
-
+
lambda {
@session.clicks_link "Link"
}.should raise_error
end
-
+
it "should assert valid response" do
@session.response_body = <<-EOS
<a href="/page">Link text</a>
@@ -158,17 +185,17 @@
@session.response_code = 404
lambda { @session.clicks_link "Link text" }.should raise_error
end
-
+
it "should fail is the link doesn't exist" do
@session.response_body = <<-EOS
<a href="/page">Link text</a>
EOS
-
+
lambda {
@session.clicks_link "Missing link"
}.should raise_error
end
-
+
it "should not be case sensitive" do
@session.response_body = <<-EOS
<a href="/page">Link text</a>
@@ -176,7 +203,7 @@
@session.should_receive(:get).with("/page", {})
@session.clicks_link "LINK TEXT"
end
-
+
it "should match link substrings" do
@session.response_body = <<-EOS
<a href="/page">This is some cool link text, isn't it?</a>
@@ -184,7 +211,7 @@
@session.should_receive(:get).with("/page", {})
@session.clicks_link "Link text"
end
-
+
it "should work with elements in the link" do
@session.response_body = <<-EOS
<a href="/page"><span>Link text</span></a>
@@ -192,7 +219,7 @@
@session.should_receive(:get).with("/page", {})
@session.clicks_link "Link text"
end
-
+
it "should match the first matching link" do
@session.response_body = <<-EOS
<a href="/page1">Link text</a>
@@ -201,26 +228,26 @@
@session.should_receive(:get).with("/page1", {})
@session.clicks_link "Link text"
end
-
+
it "should choose the shortest link text match" do
@session.response_body = <<-EOS
<a href="/page1">Linkerama</a>
<a href="/page2">Link</a>
EOS
-
+
@session.should_receive(:get).with("/page2", {})
@session.clicks_link "Link"
end
-
+
it "should treat non-breaking spaces as spaces" do
@session.response_body = <<-EOS
<a href="/page1">This&nbsp;is&nbsp;a&nbsp;link</a>
EOS
-
+
@session.should_receive(:get).with("/page1", {})
@session.clicks_link "This is a link"
end
-
+
it "should click link within a selector" do
@session.response_body = <<-EOS
<a href="/page1">Link</a>
@@ -228,7 +255,7 @@
<a href="/page2">Link</a>
</div>
EOS
-
+
@session.should_receive(:get).with("/page2", {})
@session.clicks_link_within "#container", "Link"
end
@@ -250,7 +277,7 @@
@session.should_receive(:get).with("/page/sub", {})
@session.clicks_link "Jump to sub page"
end
-
+
it "should follow fully qualified local links" do
@session.response_body = <<-EOS
<a href="http://www.example.com/page/sub">Jump to sub page</a>
View
81 spec/api/submits_form_spec.rb
@@ -0,0 +1,81 @@
+require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
+
+describe "submissal of forms" do
+ before do
+ @session = Webrat::TestSession.new
+ end
+
+ it "should work with simple forms" do
+ @session.response_body = <<-EOS
+ <form method="post" action="/login">
+ <label for="user_text">User Text</label>
+ <textarea id="user_text" name="user[text]">filling text area</textarea>
+ <input type="submit" />
+ </form>
+ EOS
+ @session.should_receive(:request_page).with("/login", "post", "user" => {"text" => "filling text area"})
+ @session.submits_form
+ end
+
+ it "should work with multiple forms" do
+ @session.response_body = <<-EOS
+ <form method="post" action="/login" id="form1">
+ <label for="user_text">User Text</label>
+ <textarea id="user_text" name="user[text]">filling text area 1</textarea>
+ <input type="submit" />
+ </form>
+ <form method="post" action="/login" id="form2">
+ <label for="user_text">User Text</label>
+ <textarea id="user_text" name="user[text]">filling text area 2</textarea>
+ <input type="submit" />
+ </form>
+ EOS
+ @session.should_receive(:request_page).with("/login", "post", "user" => {"text" => "filling text area 1"})
+ @session.submits_form("form1")
+
+ @session.should_receive(:request_page).with("/login", "post", "user" => {"text" => "filling text area 2"})
+ @session.submits_form("form2")
+
+ lambda {
+ @session.submits_form("form3")
+ }.should raise_error
+ end
+
+ it "should block on forms with confirmation cancelled" do
+ @session.response_body = <<-EOS
+ <form method="post" action="/login" onsubmit="confirm('Are you sure?')">
+ <label for="user_text">User Text</label>
+ <textarea id="user_text" name="user[text]">filling text area</textarea>
+ <input type="submit" />
+ </form>
+ EOS
+ @session.submits_form
+ @session.blocked_by_popup?.should be_true
+
+ lambda {
+ @session.dismisses_popup("Weird button")
+ }.should raise_error
+
+ @session.should_not_receive(:request_page).with("/login", "post", "user" => {"text" => "filling text area"})
+ @session.dismisses_popup(Webrat::Popup::BUTTON_CANCEL)
+ end
+
+ it "should block on forms with confirmation confirmed" do
+ @session.response_body = <<-EOS
+ <form method="post" action="/login" onsubmit="confirm('Are you sure?')">
+ <label for="user_text">User Text</label>
+ <textarea id="user_text" name="user[text]">filling text area</textarea>
+ <input type="submit" />
+ </form>
+ EOS
+ @session.submits_form
+ @session.blocked_by_popup?.should be_true
+
+ lambda {
+ @session.dismisses_popup("Weird button")
+ }.should raise_error
+
+ @session.should_receive(:request_page).with("/login", "post", "user" => {"text" => "filling text area"})
+ @session.dismisses_popup(Webrat::Popup::BUTTON_OK)
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.