Module: Splinter::Capybara::Actions

Defined in:
lib/splinter/capybara/actions.rb

Overview

Additional actions for Capybara.

Instance Method Summary collapse

Instance Method Details

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



129
130
131
# File 'lib/splinter/capybara/actions.rb', line 129

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

Yields:



49
50
51
52
# File 'lib/splinter/capybara/actions.rb', line 49

def complete_form(base)
  yield form = FormCompleter.new(base, self)
  form.submit unless form.
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.



104
105
106
107
108
109
# File 'lib/splinter/capybara/actions.rb', line 104

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" }

Raises:

  • (ArgumentError)


116
117
118
119
120
121
122
123
# File 'lib/splinter/capybara/actions.rb', line 116

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">)


92
93
94
95
96
97
98
# File 'lib/splinter/capybara/actions.rb', line 92

def select_date(time, options={})
  id = select_prefix_from_options(options)

  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">)


62
63
64
65
66
67
# File 'lib/splinter/capybara/actions.rb', line 62

def select_datetime(time, options = {})
  select_prefix = select_prefix_from_options(options)

  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">)


77
78
79
80
81
82
# File 'lib/splinter/capybara/actions.rb', line 77

def select_time(time, options = {})
  id = select_prefix_from_options(options)

  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
# File 'lib/splinter/capybara/actions.rb', line 11

def take_screenshot!(name = "#{Time.now.strftime('%Y%m%d%H%M%S%L')}")
  if path = Splinter.screenshot_directory
    args = [File.join(path, "#{name}.png"), Splinter.screenshot_options].compact
    page.driver.render(*args)
    puts "Saved screenshot to #{args.first}"
  else
    warn "Splinter.screenshot_directory doesn't exist!"
  end
end