Module: Splinter::Capybara::Actions
- Defined in:
- lib/splinter/capybara/actions.rb
Overview
Additional actions for Capybara.
Instance Method Summary collapse
-
#click_link_inside_row(lookup, row_content) ⇒ Object
Finds a link inside a row and clicks it.
-
#complete_form(base) {|form = FormCompleter.new(base, self)| ... } ⇒ Object
Complete and submit a form.
-
#find_and_select_option(select_id, value) ⇒ Object
Finds and selects an option from a dropdown with the given ID.
-
#javascript_confirm(result = true) ⇒ Object
Simulates a javascript alert confirmation.
-
#select_date(time, options = {}) ⇒ Object
Selects hour and minute dropdowns for a time attribute.
-
#select_datetime(time, options = {}) ⇒ Object
Selects hour and minute dropdowns for a time attribute.
-
#select_time(time, options = {}) ⇒ Object
Selects hour and minute dropdowns for a time attribute.
-
#take_screenshot!(name = "#{Time.now.strftime('%Y%m%d%H%M%S%L')}") ⇒ Object
Take a screenshot of the current page.
Instance Method Details
#click_link_inside_row(lookup, row_content) ⇒ Object
Finds a link inside a row and clicks it.
lookup - the link text to look for row_content - the text to use when looking for a row
134 135 136 |
# File 'lib/splinter/capybara/actions.rb', line 134 def click_link_inside_row(lookup, row_content) find(:xpath, "//tr[contains(.,'#{row_content}')]/td/a", :text => lookup).click end |
#complete_form(base) {|form = FormCompleter.new(base, self)| ... } ⇒ Object
Complete and submit a form
base - The name of the form, (eg: :post for form_for @post)
A required block is yielded to a FormCompleter object. The form will be completed and submitted upon execution of the block.
Example:
complete_form :post do |f|
f.text_field :author, "Joshua Priddle"
f.text_area :bio, "Lorem ipsum"
f.checkbox :admin, true
f.select :group, 'admins'
f.radio :autosave, false
f.date :publish_at, Time.now
f.datetime :publish_at, Time.now
t.time :publish_at, Time.now
end
You can also manually submit in case your submit input field requires a custom selector:
complete_form :post do |f|
...
f.submit "//custom/xpath/selector"
end
54 55 56 57 |
# File 'lib/splinter/capybara/actions.rb', line 54 def complete_form(base) yield form = FormCompleter.new(base, self) form.submit unless form.submitted? end |
#find_and_select_option(select_id, value) ⇒ Object
Finds and selects an option from a dropdown with the given ID.
select_id - CSS ID to check, do not include # value - The value to select.
109 110 111 112 113 114 |
# File 'lib/splinter/capybara/actions.rb', line 109 def find_and_select_option(select_id, value) select = find(:css, "##{select_id}") path = ".//option[contains(./@value, '#{value}')][1]" select.find(:xpath, path).select_option end |
#javascript_confirm(result = true) ⇒ Object
Simulates a javascript alert confirmation. You need to pass in a block that will generate the alert. E.g.:
javascript_confirm { click_link "Destroy" }
javascript_confirm(false) { click_link "Destroy" }
121 122 123 124 125 126 127 128 |
# File 'lib/splinter/capybara/actions.rb', line 121 def javascript_confirm(result = true) raise ArgumentError, "Block required" unless block_given? result = !! result page.evaluate_script("window.original_confirm = window.confirm; window.confirm = function() { return #{result.inspect}; }") yield page.evaluate_script("window.confirm = window.original_confirm;") end |
#select_date(time, options = {}) ⇒ Object
Selects hour and minute dropdowns for a time attribute.
time - A Time object that will be used to choose options. options - A Hash containing one of:
:id_prefix - the prefix of each dropdown's CSS ID (eg:
:post_publish_at for #post_publish_at_1i, etc)
:from - The text in a label for this dropdown (eg:
"Publish At" for <label for="#publish_at_1i">)
97 98 99 100 101 102 103 |
# File 'lib/splinter/capybara/actions.rb', line 97 def select_date(time, ={}) id = () find_and_select_option "#{id}_1i", time.year find_and_select_option "#{id}_2i", time.month find_and_select_option "#{id}_3i", time.day end |
#select_datetime(time, options = {}) ⇒ Object
Selects hour and minute dropdowns for a time attribute.
time - A Time object that will be used to choose options. options - A Hash containing one of:
:id_prefix - the prefix of each dropdown's CSS ID (eg:
:post_publish_at for #post_publish_at_1i, etc)
:from - The text in a label for this dropdown (eg:
"Publish At" for <label for="#publish_at_1i">)
67 68 69 70 71 72 |
# File 'lib/splinter/capybara/actions.rb', line 67 def select_datetime(time, = {}) select_prefix = () select_time time, :id_prefix => select_prefix select_date time, :id_prefix => select_prefix end |
#select_time(time, options = {}) ⇒ Object
Selects hour and minute dropdowns for a time attribute.
time - A Time object that will be used to choose options. options - A Hash containing one of:
:id_prefix - the prefix of each dropdown's CSS ID (eg:
:post_publish_at for #post_publish_at_1i, etc)
:from - The text in a label for this dropdown (eg:
"Publish At" for <label for="#publish_at_1i">)
82 83 84 85 86 87 |
# File 'lib/splinter/capybara/actions.rb', line 82 def select_time(time, = {}) id = () find_and_select_option "#{id}_4i", "%02d" % time.hour find_and_select_option "#{id}_5i", "%02d" % time.min end |
#take_screenshot!(name = "#{Time.now.strftime('%Y%m%d%H%M%S%L')}") ⇒ Object
Take a screenshot of the current page
name - The name of the screenshot. Uses the current time by default.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/splinter/capybara/actions.rb', line 11 def take_screenshot!(name = "#{Time.now.strftime('%Y%m%d%H%M%S%L')}") unless example.[:js] warn "Screenshots can only be captured when an example is tagged with :js => true" return end if path = Splinter.screenshot_directory args = [File.join(path, "#{name}.png"), Splinter.].compact page.driver.render(*args) puts "Saved screenshot to #{args.first}" else warn "Splinter.screenshot_directory doesn't exist!" end end |