class WebMinion::MechanizeBot

Public Class Methods

new(config = {}) click to toggle source
Calls superclass method WebMinion::Bot::new
# File lib/web_minion/bots/mechanize_bot.rb, line 10
def initialize(config = {})
  super(config)
  @bot = Mechanize.new
end

Public Instance Methods

body() click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 19
def body
  page.body.to_s
end
body_includes(_target, value, _element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 119
def body_includes(_target, value, _element)
  if value.is_a?(Array)
    val_check_arr = []
    value.each do |val|
      val_check_arr << !!(body.index(val) && body.index(val) > 0)
    end
    val_check_arr.uniq.include?(true)
  else
    !!(body.index(value) && body.index(value) > 0)
  end
end
click(target, _value, _element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 27
def click(target, _value, _element)
  button = @bot.page.at(target)
  @bot.click(button)
end
click_button_in_form(target, _value, element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 32
def click_button_in_form(target, _value, element)
  element.button_with(target).click
end
fill_in_input(target, value, element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 75
def fill_in_input(target, value, element)
  input = element[target]
  raise(NoInputFound, "For target: #{target}") unless input
  element[target] = value
  element
end
get_field(target, _value, element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 71
def get_field(target, _value, element)
  element.field_with(target)
end
get_form(target, _value, _element) click to toggle source
FORM METHODS ##

Must have an element passed to them (except get form)

# File lib/web_minion/bots/mechanize_bot.rb, line 67
def get_form(target, _value, _element)
  FormElement.new(@bot, target, nil, nil).get
end
go(target, _value, _element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 23
def go(target, _value, _element)
  @bot.get(target)
end
page() click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 15
def page
  @bot.page
end
save_page_html(_target, value, _element) click to toggle source

ability to save dynamic dates in the filename with string “INSERT_DATE” so a value of “myfilename-INSERT_DATE.html” will look like this: “myfilename-2016-09-19_18-51-40.html” if you want multiple steps saving at the same time without overwriting, rename save_html step in your json (i.e. 'save_html_confirmation' etc)

# File lib/web_minion/bots/mechanize_bot.rb, line 45
def save_page_html(_target, value, _element)
  if value.include?("INSERT_DATE")
    time = Time.now.strftime('%Y-%m-%d_%H-%M-%S').to_s
    value = value.split("INSERT_DATE").insert(1, time).join
  end
  write_html_file(value)
end
save_value(target, value, _element, val_hash) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 53
def save_value(target, value, _element, val_hash)
  element = @bot.page.search(target)

  if val_hash[value.to_sym]
    val_hash[value.to_sym] << element if element
  else
    val_hash[value.to_sym] = element if element
  end

  val_hash
end
select_checkbox(target, _value, element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 93
def select_checkbox(target, _value, element)
  if target.is_a?(Array)
    target.each { |tar| select_checkbox(tar, nil, element) }
  else
    element.checkbox_with(target).check
  end
end
select_field(target, _value, element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 87
def select_field(target, _value, element)
  options = element.options_with(target)
  raise(MultipleOptionsFoundError, "For target: #{target}") if options.count > 1
  options.first.click
end
select_first_radio_button(_target, _value, element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 107
def select_first_radio_button(_target, _value, element)
  radio = element.radiobuttons.first
  radio.checked = true
  radio
end
select_radio_button(target, _value, element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 101
def select_radio_button(target, _value, element)
  radio = element.radiobutton_with(target)
  radio.checked = true
  radio
end
set_file_upload(target, value, element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 36
def set_file_upload(target, value, element)
  FileUploadElement.new(@bot, target, value, element).set_file
end
submit(_target, _value, element) click to toggle source

Element should be an instance of a form

# File lib/web_minion/bots/mechanize_bot.rb, line 83
def submit(_target, _value, element)
  @bot.submit element
end
url_equals(_target, value, _element) click to toggle source

VALIDATION METHODS ##

# File lib/web_minion/bots/mechanize_bot.rb, line 115
def url_equals(_target, value, _element)
  !!(@bot.page.uri.to_s == value)
end
value_equals(_target, value, element) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 131
def value_equals(_target, value, element)
  !!(element && (element.value == value))
end

Private Instance Methods

write_html_file(filename) click to toggle source
# File lib/web_minion/bots/mechanize_bot.rb, line 137
def write_html_file(filename)
  File.open(filename, "w") { |f| f.puts body }
end