Class: Webrat::Scope
- Defined in:
- lib/webrat/core/scope.rb
Constant Summary collapse
- DATE_TIME_SUFFIXES =
{ :year => '1i', :month => '2i', :day => '3i', :hour => '4i', :minute => '5i' }
Instance Attribute Summary collapse
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Class Method Summary collapse
-
.from_page(session, response, response_body) ⇒ Object
:nodoc:.
-
.from_scope(session, scope, selector) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#attach_file(field_locator, path, content_type = nil) ⇒ Object
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. -
#check(field_locator) ⇒ Object
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.
-
#choose(field_locator) ⇒ Object
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.
- #click_area(area_name) ⇒ Object
-
#click_button(value = nil) ⇒ Object
Verifies that a submit button exists for the form, then submits the form, follows any redirects, and verifies the final page was successful.
-
#click_link(text_or_title_or_id, options = {}) ⇒ Object
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.
-
#dom ⇒ Object
:nodoc:.
-
#fill_in(field_locator, options = {}) ⇒ Object
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.
-
#initialize(session, &block) ⇒ Scope
constructor
:nodoc:.
-
#select(option_text, options = {}) ⇒ Object
Verifies that a an option element exists on the current page with the specified text.
-
#select_date(date_to_select, options = {}) ⇒ Object
Verifies that date elements (year, month, day) exist on the current page with the specified values.
-
#select_datetime(time_to_select, options = {}) ⇒ Object
Verifies and selects all the date and time elements on the current page.
-
#select_time(time_to_select, options = {}) ⇒ Object
Verifies that time elements (hour, minute) exist on the current page with the specified values.
- #set_hidden_field(field_locator, options = {}) ⇒ Object
- #submit_form(id) ⇒ Object
-
#uncheck(field_locator) ⇒ Object
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.
Methods included from Locators
#field, #field_by_xpath, #field_labeled, #field_named, #field_with_id, #find_area, #find_button, #find_link, #select_option
Methods included from Logging
Constructor Details
#initialize(session, &block) ⇒ Scope
:nodoc:
30 31 32 33 |
# File 'lib/webrat/core/scope.rb', line 30 def initialize(session, &block) #:nodoc: @session = session instance_eval(&block) if block_given? end |
Instance Attribute Details
#session ⇒ Object (readonly)
Returns the value of attribute session.
28 29 30 |
# File 'lib/webrat/core/scope.rb', line 28 def session @session end |
Class Method Details
.from_page(session, response, response_body) ⇒ Object
:nodoc:
14 15 16 17 18 19 |
# File 'lib/webrat/core/scope.rb', line 14 def self.from_page(session, response, response_body) #:nodoc: new(session) do @response = response @response_body = response_body end end |
.from_scope(session, scope, selector) ⇒ Object
:nodoc:
21 22 23 24 25 26 |
# File 'lib/webrat/core/scope.rb', line 21 def self.from_scope(session, scope, selector) #:nodoc: new(session) do @scope = scope @selector = selector end end |
Instance Method Details
#attach_file(field_locator, path, content_type = nil) ⇒ Object
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 content_type
may be given.
Example:
attaches_file "Resume", "/path/to/the/resume.txt"
attaches_file "Photo", "/path/to/the/image.png", "image/png"
205 206 207 |
# File 'lib/webrat/core/scope.rb', line 205 def attach_file(field_locator, path, content_type = nil) locate_field(field_locator, FileField).set(path, content_type) end |
#check(field_locator) ⇒ Object
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:
check 'Remember Me'
63 64 65 |
# File 'lib/webrat/core/scope.rb', line 63 def check(field_locator) locate_field(field_locator, CheckboxField).check end |
#choose(field_locator) ⇒ Object
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:
choose 'First Option'
85 86 87 |
# File 'lib/webrat/core/scope.rb', line 85 def choose(field_locator) locate_field(field_locator, RadioField).choose end |
#click_area(area_name) ⇒ Object
211 212 213 |
# File 'lib/webrat/core/scope.rb', line 211 def click_area(area_name) find_area(area_name).click end |
#click_button(value = nil) ⇒ Object
Verifies that a submit button exists for the form, then submits the form, follows any redirects, and verifies the final page was successful.
Example:
"Login"
The URL and HTTP method for the form submission are automatically read from the action
and method
attributes of the <form>
element.
256 257 258 |
# File 'lib/webrat/core/scope.rb', line 256 def (value = nil) (value).click end |
#click_link(text_or_title_or_id, options = {}) ⇒ Object
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.
click_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.
Javascript imitation can be disabled by passing the option :javascript => false
Passing a :method in the options hash overrides the HTTP method used 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:
click_link "Sign up"
click_link "Sign up", :javascript => false
click_link "Sign up", :method => :put
241 242 243 |
# File 'lib/webrat/core/scope.rb', line 241 def click_link(text_or_title_or_id, = {}) find_link(text_or_title_or_id).click() end |
#dom ⇒ Object
:nodoc:
266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/webrat/core/scope.rb', line 266 def dom # :nodoc: return @dom if @dom if @selector @dom = scoped_dom else @dom = page_dom end return @dom end |
#fill_in(field_locator, options = {}) ⇒ Object
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.
Examples:
fill_in "Email", :with => "[email protected]"
fill_in "user[email]", :with => "[email protected]"
The field value is required, and must be specified in options[:with]
. field
can be either the value of a name attribute (i.e. user[email]
) or the text inside a <label>
element that points at the <input>
field.
45 46 47 48 49 |
# File 'lib/webrat/core/scope.rb', line 45 def fill_in(field_locator, = {}) field = locate_field(field_locator, TextField, TextareaField, PasswordField) field.raise_error_if_disabled field.set([:with]) end |
#select(option_text, options = {}) ⇒ Object
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 options[:from]
the value of the select list’s name or a label. Stores the option’s value to be sent when the form is submitted.
Examples:
select "January"
select "February", :from => "event_month"
select "February", :from => "Event Month"
100 101 102 |
# File 'lib/webrat/core/scope.rb', line 100 def select(option_text, = {}) select_option(option_text, [:from]).choose end |
#select_date(date_to_select, options = {}) ⇒ Object
Verifies that date elements (year, month, day) exist on the current page with the specified values. You can optionally restrict the search to a specific date’s elements by assigning options[:from]
the value of the date’s label. Selects all the date elements with date provided. The date provided may be a string or a Date/Time object.
Rail’s convention is used for detecting the date elements. All elements are assumed to have a shared prefix. You may also specify the prefix by assigning options[:id_prefix]
.
Examples:
select_date "January 23, 2004"
select_date "April 26, 1982", :from => "Birthday"
select_date Date.parse("December 25, 2000"), :from => "Event"
select_date "April 26, 1982", :id_prefix => 'birthday'
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/webrat/core/scope.rb', line 129 def select_date(date_to_select, ={}) date = date_to_select.is_a?(Date) || date_to_select.is_a?(Time) ? date_to_select : Date.parse(date_to_select) id_prefix = locate_id_prefix() do year_field = FieldByIdLocator.new(@session, dom, /(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/).locate raise NotFoundError.new("No date fields were found") unless year_field && year_field.id =~ /(.*?)_1i/ $1 end select date.year, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:year]}" select date.strftime('%B'), :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:month]}" select date.day, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:day]}" end |
#select_datetime(time_to_select, options = {}) ⇒ Object
Verifies and selects all the date and time elements on the current page. See #select_time and #select_date for more details and available options.
Examples:
select_datetime "January 23, 2004 10:30AM"
select_datetime "April 26, 1982 7:00PM", :from => "Birthday"
select_datetime Time.parse("December 25, 2000 15:30"), :from => "Event"
select_datetime "April 26, 1982 5:50PM", :id_prefix => 'birthday'
187 188 189 190 191 192 193 194 |
# File 'lib/webrat/core/scope.rb', line 187 def select_datetime(time_to_select, ={}) time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select) [:id_prefix] ||= ([:from] ? FieldByIdLocator.new(@session, dom, [:from]).locate : nil) select_date time, select_time time, end |
#select_time(time_to_select, options = {}) ⇒ Object
Verifies that time elements (hour, minute) exist on the current page with the specified values. You can optionally restrict the search to a specific time’s elements by assigning options[:from]
the value of the time’s label. Selects all the time elements with date provided. The time provided may be a string or a Time object.
Rail’s convention is used for detecting the time elements. All elements are assumed to have a shared prefix. You may specify the prefix by assigning options[:id_prefix]
.
Note: Just like Rails’ time_select helper this assumes the form is using 24 hour select boxes, and not 12 hours with AM/PM.
Examples:
select_time "9:30"
select_date "3:30PM", :from => "Party Time"
select_date Time.parse("10:00PM"), :from => "Event"
select_date "10:30AM", :id_prefix => 'meeting'
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/webrat/core/scope.rb', line 164 def select_time(time_to_select, ={}) time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select) id_prefix = locate_id_prefix() do hour_field = FieldByIdLocator.new(@session, dom, /(.*?)_#{DATE_TIME_SUFFIXES[:hour]}$/).locate raise NotFoundError.new("No time fields were found") unless hour_field && hour_field.id =~ /(.*?)_4i/ $1 end select time.hour.to_s.rjust(2,'0'), :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:hour]}" select time.min.to_s.rjust(2,'0'), :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:minute]}" end |
#set_hidden_field(field_locator, options = {}) ⇒ Object
53 54 55 56 |
# File 'lib/webrat/core/scope.rb', line 53 def set_hidden_field(field_locator, = {}) field = locate_field(field_locator, HiddenField) field.set([:to]) end |
#submit_form(id) ⇒ Object
262 263 264 |
# File 'lib/webrat/core/scope.rb', line 262 def submit_form(id) FormLocator.new(@session, dom, id).locate.submit end |
#uncheck(field_locator) ⇒ Object
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:
uncheck 'Remember Me'
74 75 76 |
# File 'lib/webrat/core/scope.rb', line 74 def uncheck(field_locator) locate_field(field_locator, CheckboxField).uncheck end |