Class: WebMinion::MechanizeBot

Inherits:
Bot
  • Object
show all
Defined in:
lib/web_minion/bots/mechanize_bot.rb

Instance Attribute Summary

Attributes inherited from Bot

#bot, #config

Instance Method Summary collapse

Methods inherited from Bot

#execute_step

Constructor Details

#initialize(config = {}) ⇒ MechanizeBot



10
11
12
13
# File 'lib/web_minion/bots/mechanize_bot.rb', line 10

def initialize(config = {})
  super(config)
  @bot = Mechanize.new
end

Instance Method Details

#bodyObject



19
20
21
# File 'lib/web_minion/bots/mechanize_bot.rb', line 19

def body
  page.body.to_s
end

#body_includes(_target, value, _element) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Object



27
28
29
30
# 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) ⇒ Object



32
33
34
# 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) ⇒ Object

Raises:



75
76
77
78
79
80
# 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) ⇒ Object



71
72
73
# 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) ⇒ Object

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



67
68
69
# 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) ⇒ Object



23
24
25
# File 'lib/web_minion/bots/mechanize_bot.rb', line 23

def go(target, _value, _element)
  @bot.get(target)
end

#pageObject



15
16
17
# File 'lib/web_minion/bots/mechanize_bot.rb', line 15

def page
  @bot.page
end

#save_page_html(_target, value, _element) ⇒ Object

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)



45
46
47
48
49
50
51
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



93
94
95
96
97
98
99
# 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) ⇒ Object



87
88
89
90
91
# 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) ⇒ Object



107
108
109
110
111
# 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) ⇒ Object



101
102
103
104
105
# 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) ⇒ Object



36
37
38
# 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) ⇒ Object

Element should be an instance of a form



83
84
85
# File 'lib/web_minion/bots/mechanize_bot.rb', line 83

def submit(_target, _value, element)
  @bot.submit element
end

#url_equals(_target, value, _element) ⇒ Object

VALIDATION METHODS ##



115
116
117
# 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) ⇒ Object



131
132
133
# File 'lib/web_minion/bots/mechanize_bot.rb', line 131

def value_equals(_target, value, element)
  !!(element && (element.value == value))
end