module Swat::Capybara::Helpers

Public Instance Methods

check_absence(*args) click to toggle source
# File lib/swat/capybara/helpers.rb, line 47
def check_absence(*args)
  wait_for_condition do
    all(*args).count == 0
  end
  raise ElementFound unless all(*args).count == 0
end
check_condition(tries = Capybara.config.tries) { || ... } click to toggle source
# File lib/swat/capybara/helpers.rb, line 33
def check_condition(tries = Capybara.config.tries)
  res = wait_for_condition(tries) do
    !!yield()
  end
  raise ConditionIsNotMet unless res
end
check_no_text(text, selector=Capybara.config.default_selector, tries=Capybara.config.tries) click to toggle source
# File lib/swat/capybara/helpers.rb, line 121
def check_no_text(text, selector=Capybara.config.default_selector, tries=Capybara.config.tries)
  result = nil
  wait_for_condition(tries) do
    container = safe_find(selector)
    result = Hash[
        [ text ].flatten.map do |word|
          [ word, !container.text.include?(word) ]
        end
    ]
    result.values.all?{|v|v}
  end
  result.each do |k,v|
    print_failed_args(v, [text, selector], "\nText '#{k}' WAS found") unless v
  end
  raise TextWasFound unless result.values.all?{|v|v}
  true
end
check_presence(*args) click to toggle source
# File lib/swat/capybara/helpers.rb, line 40
def check_presence(*args)
  wait_for_condition do
    all(*args).count > 0
  end
  raise ElementNotFound unless all(*args).count > 0
end
check_text(text, selector=Capybara.config.default_selector, tries=Capybara.config.tries) click to toggle source
# File lib/swat/capybara/helpers.rb, line 102
def check_text(text, selector=Capybara.config.default_selector, tries=Capybara.config.tries)
  result = nil
  wait_for_condition(tries) do
    container = safe_find(selector)
    local_text = container.text
    result = Hash[
        [ text ].flatten.map do |word|
          [ word, local_text.include?(word) ]
        end
    ]
    result.values.all?{|v|v}
  end
  result.each do |k,v|
    print_failed_args(v, [text, selector], "\nText '#{k}' NOT found") unless v
  end
  raise TextNotFound unless result.values.all?{|v|v}
  true
end
click_by_text(text, tag='span') click to toggle source
# File lib/swat/capybara/helpers.rb, line 98
def click_by_text(text, tag='span')
  safe_click(tag, text: text)
end
explain_step(message) { || ... } click to toggle source
# File lib/swat/capybara/helpers.rb, line 14
def explain_step(message)
  swat_logger.print "\n - #{message.blue}"
  yield() if block_given?
rescue Exception => ex
  print_exception(ex)
  raise_again = !swat_stop_fail?
  binding.pry if swat_debug?
  raise ex if raise_again
end
Also aliased as: step
page_refresh() click to toggle source
# File lib/swat/capybara/helpers.rb, line 85
def page_refresh
  page.driver.browser.navigate.refresh
end
pause(seconds = Capybara.config.default_pause) click to toggle source
# File lib/swat/capybara/helpers.rb, line 10
def pause(seconds = Capybara.config.default_pause)
  sleep seconds
end
print_exception(ex) click to toggle source
print_failed_args(res, args, message=nil) click to toggle source
safe_click(*args) click to toggle source
# File lib/swat/capybara/helpers.rb, line 54
def safe_click(*args)
  safe_find(*args).click
end
safe_find(*args) click to toggle source
# File lib/swat/capybara/helpers.rb, line 58
def safe_find(*args)
  wait_for_condition do
    all(*args).count == 1
  end
  find(*args)
end
safe_find_all(*args) click to toggle source
# File lib/swat/capybara/helpers.rb, line 65
def safe_find_all(*args)
  res = wait_for_condition do
    all(*args).count > 0
  end
  print_failed_args(res, args)
  all(*args)
end
safe_set(field, value) click to toggle source
# File lib/swat/capybara/helpers.rb, line 81
def safe_set(field, value)
  safe_find(field).set(value)
end
select_option(selector, value) click to toggle source
# File lib/swat/capybara/helpers.rb, line 73
def select_option(selector, value)
  dropdown = safe_find(selector)
  wait_for_condition do
    dropdown.all(:option).map(&:text).include? value
  end
  dropdown.all(:option).select{|op| op.text == value}.first.select_option
end
step(message)
Alias for: explain_step
sub_step(message) { || ... } click to toggle source
# File lib/swat/capybara/helpers.rb, line 89
def sub_step(message)
  swat_logger.print "\n    > #{message.blue} ".yellow
  yield() if block_given?
end
swat_debug?() click to toggle source
# File lib/swat/capybara/helpers.rb, line 173
def swat_debug?
  ENV['SWAT_DBG'] || ENV['FPRY']
end
swat_logger() click to toggle source
# File lib/swat/capybara/helpers.rb, line 163
def swat_logger
  swat_store.swat_logger ||= TarvitHelpers::ConditionalLogger.new do
    !ENV['SWAT_LOGS_DISABLED']
  end
end
swat_stop_fail?() click to toggle source
# File lib/swat/capybara/helpers.rb, line 169
def swat_stop_fail?
  !!ENV['SWAT_STOP_FAIL']
end
swat_store() click to toggle source
# File lib/swat/capybara/helpers.rb, line 159
def swat_store
  @swat_store ||= OpenStruct.new
end
wait_for_condition(tries = Capybara.config.tries, &condition) click to toggle source
# File lib/swat/capybara/helpers.rb, line 139
def wait_for_condition(tries = Capybara.config.tries, &condition)
  swat_logger.print Capybara.config.output[:started]
  tries.times do
    swat_logger.print Capybara.config.output[:step]
    result = condition.()
    return result if result
    sleep(Capybara.config.min_pause)
    false
  end
  swat_logger.puts "Failed: #{condition.to_source.red}" rescue nil
  result = ENV['SWAT_STOP_FAIL'] || false
  binding.pry if (ENV['FPRY'] || ENV['SWAT_DBG'])
  result
end
xstep(name) click to toggle source
# File lib/swat/capybara/helpers.rb, line 94
def xstep name
  swat_logger.print "Skipped step: #{name}"
end