Skip to content

Instantly share code, notes, and snippets.

@Dishwasha
Created August 14, 2009 15:14
Show Gist options
  • Save Dishwasha/167887 to your computer and use it in GitHub Desktop.
Save Dishwasha/167887 to your computer and use it in GitHub Desktop.
<h2 class="title"><%=h @alert.title %></h2>
<div class="details">
<%=simple_format h(@alert.message), :class => 'message' %>
<p><strong>Short Message:</strong> <%= h(@alert.short_message) %></p>
<p class="jurisdictions"><strong>Jurisdictions:</strong> <%=h @alert.jurisdictions.blank? ? 'None' : @alert.jurisdictions.map(&:name).to_sentence %></p>
<p class="roles"><strong>Roles:</strong> <%=h @alert.roles.blank? ? 'None' : @alert.roles.map(&:name).to_sentence %></p>
<p class="organizations"><strong>Organizations:</strong> <%=h @alert.organizations.blank? ? 'None' : @alert.organizations.map(&:name).to_sentence %></p>
<p class="people"><strong>People:</strong> <%=h @alert.users.blank? ? 'No additional people selected' : @alert.users.map(&:name).to_sentence %></p>
</div>
<div class="aside">
<h5>Severity</h5>
<p class="severity"><%=h @alert.severity %></p>
<h5>Status</h5>
<p class="status"><%=h @alert.status %></p>
<h5>Acknowledge</h5>
<p class="acknowledge"><%=h @alert.acknowledge? ? 'Yes' : 'No' %></p>
<h5>Sensitive</h5>
<p class="sensitive"><%=h @alert.sensitive? ? 'Yes' : 'No' %></p>
<h5>Delivery Time</h5>
<p class="delivery_time"><%=h @alert.human_delivery_time %></p>
<h5>Methods</h5>
<p class="communication_methods"><%=h @alert.device_types.map{|d| d.constantize.display_name }.to_sentence %></p>
</div>
Feature: Acknowledging an alert
In order to report that I read an alerts
As a user
I can acknowledge an alert
Background:
Given the following users exist:
| Martin Fowler | martin@example.com | Health Official | Dallas County |
And the role "Health Official" is an alerter
And a sent alert with:
| title | Piggy Pox |
| message | the world is on fire |
| status | Actual |
| severity | Moderate |
| acknowledge | Yes |
| from_jurisdiction | Dallas County |
| jurisdictions | Dallas County |
And I am logged in as "martin@example.com"
Scenario: A user acknowledging an alert via the alert show page
When I am on the alert log
And I click "View" on "Piggy Pox"
Then I can see the alert summary for "Piggy Pox"
When I press "Acknowledge"
Then I have acknowledged the alert for "Piggy Pox"
When I go to the alert log
And I click "View" on "Piggy Pox"
Then I should not see an "Acknowledge" button
But the "acknowledge" class selector should contain "Yes"
require "webrat/core_extensions/detect_mapped"
require "webrat/core/locators/locator"
module Webrat
module Locators
class ClassSelectorElementLocator < Locator # :nodoc:
def locate
matching_elements.any? && matching_elements.detect_mapped { |element| element.text }
end
def matching_elements
matching_elements.sort_by do |element|
text(element).length
end.map do |element|
Element.load(@session, element)
end
end
def matching_elements
Webrat::XML.xpath_search(@dom, ".//*[@class='#{@value}']")
end
def error_message
"Could not find class selector #{@value.inspect}"
end
def text(element)
str = Webrat::XML.all_inner_text(element)
str.gsub!("\n","")
str.strip!
str.squeeze!(" ")
str
end
end
# Locates a form field based on a <tt>label</tt> element in the HTML source.
# This can be useful in order to verify that a field is pre-filled with the
# correct value.
#
# Example:
# field_labeled("First name").value.should == "Bryan"
def class_selector_element(label, *field_types)
ClassSelectorElementLocator.new(@session, dom, label, *field_types).locate!
end
end
end
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
Then /^the "([^\"]*)" class selector should contain "([^\"]*)"$/ do |field, value|
class_selector_element(field).should =~ /#{value}/
end
require "webrat/core/locators/area_locator"
require "webrat/core/locators/button_locator"
require "webrat/core/locators/field_labeled_locator"
require "webrat/core/locators/label_locator"
require "webrat/core/locators/field_named_locator"
require "webrat/core/locators/field_by_id_locator"
require "webrat/core/locators/select_option_locator"
require "webrat/core/locators/link_locator"
require "webrat/core/locators/field_locator"
require "webrat/core/locators/form_locator"
require "webrat/core/locators/select_multiple_options_locator"
require "webrat/core/locators/class_selector_element_locator" #Added this
module Webrat
module Locators
def field_by_xpath(xpath)
Field.load(@session, Webrat::XML.xpath_at(dom, xpath))
end
end
end
module Webrat
module Methods #:nodoc:
def self.delegate_to_session(*meths)
meths.each do |meth|
self.class_eval <<-RUBY
def #{meth}(*args, &blk)
webrat_session.#{meth}(*args, &blk)
end
RUBY
end
end
def webrat
webrat_session
end
def webrat_session
if Webrat.configuration.mode == :rack_test
@_webrat_session ||= ::Webrat::RackTestSession.new(rack_test_session)
else
@_webrat_session ||= ::Webrat.session_class.new(self)
end
end
# all of these methods delegate to the @session, which should
# be created transparently.
#
# Note that when using Webrat, #request also uses @session, so
# that #request and webrat native functions behave interchangably
delegate_to_session \
:visits, :visit,
:within,
:header, :http_accept, :basic_auth,
:save_and_open_page,
:fills_in, :fill_in,
:checks, :check,
:unchecks, :uncheck,
:chooses, :choose,
:selects, :select,
:attaches_file, :attach_file,
:current_page,
:current_url,
:clicks_link, :click_link,
:clicks_area, :click_area,
:clicks_button, :click_button,
:reload, :reloads,
:clicks_link_within, :click_link_within,
:field_labeled,
:select_option,
:set_hidden_field, :submit_form,
:request_page, :current_dom,
:response_body,
:selects_date, :selects_time, :selects_datetime,
:select_date, :select_time, :select_datetime,
:field_by_xpath,
:field_with_id,
:selenium,
:simulate, :automate,
:field_named,
:select_multiple,
:class_selector_element #Added this
end
end
require "forwardable"
require "ostruct"
require "webrat/core/mime"
require "webrat/core/save_and_open_page"
module Webrat
# A page load or form submission returned an unsuccessful response code (500-599)
class PageLoadError < WebratError
end
class InfiniteRedirectError < WebratError
end
def self.session_class
case Webrat.configuration.mode
when :rails
RailsSession
when :merb
MerbSession
when :selenium
SeleniumSession
when :rack
RackSession
when :sinatra
SinatraSession
when :mechanize
MechanizeSession
when :rack_test
RackTestSession
else
raise WebratError.new(<<-STR)
Unknown Webrat mode: #{Webrat.configuration.mode.inspect}
Please ensure you have a Webrat configuration block that specifies a mode
in your test_helper.rb, spec_helper.rb, or env.rb (for Cucumber).
This configure block supercedes the need to require "webrat/<framework>".
For example:
Webrat.configure do |config|
config.mode = :rails
end
STR
end
end
class Session
extend Forwardable
include Logging
include SaveAndOpenPage
attr_reader :current_url
attr_reader :elements
def initialize(context = nil) #:nodoc:
@http_method = :get
@data = {}
@default_headers = {}
@custom_headers = {}
@context = context
reset
end
def current_dom #:nodoc:
current_scope.dom
end
# For backwards compatibility -- removing in 1.0
def current_page #:nodoc:
warn "current_page is deprecated and will be going away in the next release. Use current_url instead."
page = OpenStruct.new
page.url = @current_url
page.http_method = @http_method
page.data = @data
page
end
def doc_root #:nodoc:
nil
end
def header(key, value)
@custom_headers[key] = value
end
def http_accept(mime_type)
header('Accept', Webrat::MIME.mime_type(mime_type))
end
def basic_auth(user, pass)
encoded_login = ["#{user}:#{pass}"].pack("m*")
header('HTTP_AUTHORIZATION', "Basic #{encoded_login}")
end
def headers #:nodoc:
@default_headers.dup.merge(@custom_headers.dup)
end
def request_page(url, http_method, data) #:nodoc:
h = headers
h['HTTP_REFERER'] = @current_url if @current_url
debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}"
process_request(http_method, url, data, h)
save_and_open_page if exception_caught? && Webrat.configuration.open_error_files?
raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?
reset
@current_url = url
@http_method = http_method
@data = data
if internal_redirect?
check_for_infinite_redirects
request_page(response_location, :get, {})
end
return response
end
def check_for_infinite_redirects
if current_url == response_location
@_identical_redirect_count ||= 0
@_identical_redirect_count += 1
end
if infinite_redirect_limit_exceeded?
raise InfiniteRedirectError.new("#{Webrat.configuration.infinite_redirect_limit} redirects to the same URL (#{current_url.inspect})")
end
end
def infinite_redirect_limit_exceeded?
Webrat.configuration.infinite_redirect_limit &&
(@_identical_redirect_count || 0) > Webrat.configuration.infinite_redirect_limit
end
def success_code? #:nodoc:
(200..499).include?(response_code)
end
def redirect? #:nodoc:
(response_code / 100).to_i == 3
end
def internal_redirect?
return false unless redirect?
#should keep internal_redirects if the subdomain changes
current_host_domain = current_host.split('.')[-2..-1].join('.') rescue current_host
response_location_host_domain = response_location_host.split('.')[-2..-1].join('.') rescue response_location_host
current_host_domain == response_location_host_domain
end
#easy helper to pull out where we were redirected to
def redirected_to
redirect? ? response_location : nil
end
def exception_caught? #:nodoc:
response_body =~ /Exception caught/
end
def current_scope #:nodoc:
scopes.last || page_scope
end
# Reloads the last page requested. Note that this will resubmit forms
# and their data.
def reload
request_page(@current_url, @http_method, @data)
end
webrat_deprecate :reloads, :reload
# Works like click_link, but only looks for the link text within a given selector
#
# Example:
# click_link_within "#user_12", "Vote"
def click_link_within(selector, link_text)
within(selector) do
click_link(link_text)
end
end
webrat_deprecate :clicks_link_within, :click_link_within
def within(selector)
scopes.push(Scope.from_scope(self, current_scope, selector))
ret = yield(current_scope)
scopes.pop
return ret
end
# Issues a GET request for a page, follows any redirects, and verifies the final page
# load was successful.
#
# Example:
# visit "/"
def visit(url = nil, http_method = :get, data = {})
request_page(url, http_method, data)
end
webrat_deprecate :visits, :visit
# Subclasses can override this to show error messages without html
def formatted_error #:nodoc:
response_body
end
def scopes #:nodoc:
@_scopes ||= []
end
def page_scope #:nodoc:
@_page_scope ||= Scope.from_page(self, response, response_body)
end
def dom
page_scope.dom
end
def xml_content_type?
false
end
def simulate
return if Webrat.configuration.mode == :selenium
yield
end
def automate
return unless Webrat.configuration.mode == :selenium
yield
end
def_delegators :current_scope, :fill_in, :fills_in
def_delegators :current_scope, :set_hidden_field
def_delegators :current_scope, :submit_form
def_delegators :current_scope, :check, :checks
def_delegators :current_scope, :uncheck, :unchecks
def_delegators :current_scope, :choose, :chooses
def_delegators :current_scope, :select, :selects
def_delegators :current_scope, :select_datetime, :selects_datetime
def_delegators :current_scope, :select_date, :selects_date
def_delegators :current_scope, :select_time, :selects_time
def_delegators :current_scope, :attach_file, :attaches_file
def_delegators :current_scope, :click_area, :clicks_area
def_delegators :current_scope, :click_link, :clicks_link
def_delegators :current_scope, :click_button, :clicks_button
def_delegators :current_scope, :field_labeled
def_delegators :current_scope, :field_by_xpath
def_delegators :current_scope, :field_with_id
def_delegators :current_scope, :select_option
def_delegators :current_scope, :field_named
def_delegators :current_scope, :select_multiple
def_delegators :current_scope, :class_selector_element #Added this
private
def process_request(http_method, url, data, headers)
if headers.empty?
send "#{http_method}", url, data || {}
else
send "#{http_method}", url, data || {}, headers
end
end
def response_location
response.headers["Location"]
end
def current_host
URI.parse(current_url).host || @custom_headers["Host"] || "www.example.com"
end
def response_location_host
URI.parse(response_location).host || "www.example.com"
end
def reset
@elements = {}
@_scopes = nil
@_page_scope = nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment