class FerrumWizard

Attributes

browser[R]
buttons[R]
js_methods[R]
radio[R]

Public Class Methods

new(url, headless: true, timeout: 10, debug: false) click to toggle source
# File lib/ferrumwizard.rb, line 13
def initialize(url, headless: true, timeout: 10, debug: false)

  @url, @debug = url, debug
  @browser = Ferrum::Browser.new headless: headless, timeout: timeout
  sleep 3
  
  if url
    @browser.goto(@url)
    @browser.network.wait_for_idle
    sleep 4      
  end
end

Public Instance Methods

inspect() click to toggle source
# File lib/ferrumwizard.rb, line 26
def inspect()
  "#<FerrumWizard>"
end
login(usernamex=nil, passwordx=nil, username: usernamex, password: passwordx) click to toggle source
# File lib/ferrumwizard.rb, line 30
def login(usernamex=nil, passwordx=nil, username: usernamex, password: passwordx)
  
  puts 'username: ' + username.inspect if @debug

  # search for the username input box
  e_username = @browser.at_xpath('//input[@type="email"]')
  puts 'e_username: ' + e_username.inspect if @debug
  sleep 1
  # search for the password input box
  e_password  = @browser.at_xpath('//input[@type="password"]')
  sleep 1
  
  if username and e_username then
    puts 'entering the username' if @debug
    e_username.focus.type(username)       
    sleep 1
  end
  
  e_password.focus.type(password, :Enter) if e_password
  
  after_login()
  
end
login2(usernamex=nil, passwordx=nil, username: usernamex, password: passwordx) click to toggle source
# File lib/ferrumwizard.rb, line 54
def login2(usernamex=nil, passwordx=nil, username: usernamex, password: passwordx)
  
  puts 'username: ' + username.inspect if @debug

  # search for the username input box
  e_username = @browser.at_xpath('//input[@type="email"]')
  puts 'e_username: ' + e_username.inspect if @debug
  sleep 1
  # search for the password input box
  
  if username and e_username then
    puts 'entering the username' if @debug
    e_username.focus.type(username, :Enter)       
    sleep 2
  end

  e_password  = b.at_xpath('//input[@type="password"]')
  sleep 1
  
  e_password.focus.type(password, :Enter) if e_password
  
  after_login()

  
end
quit() click to toggle source
# File lib/ferrumwizard.rb, line 80
def quit
  @browser.quit
end
scan_page() click to toggle source
# File lib/ferrumwizard.rb, line 84
def scan_page()
  
  @doc = Rexle.new @browser.body       
  fetch_links()
  scan_form_elements()    
  scan_js_links()
  self    
end
submit(h) click to toggle source
# File lib/ferrumwizard.rb, line 93
def submit(h)

  e = nil
  
  h.each do |key, value|
    e = @browser.xpath('//input').find {|x| x.attribute('name') == key.to_s}      
    e.focus.type(value)      
  end
  
  e.focus.type('', :Enter)    
  
  sleep 4    
  scan_page()
  
end
to_rb() click to toggle source
# File lib/ferrumwizard.rb, line 109
def to_rb()
end

Private Instance Methods

after_login() click to toggle source
# File lib/ferrumwizard.rb, line 114
def after_login()
  
  @browser.network.wait_for_idle    
  sleep 4
  scan_page()
  
  @browser.base_url = File.dirname(@browser.url)
  @browser.mouse.scroll_to(0, 800)
  self
  
end
fetch_buttons() click to toggle source
# File lib/ferrumwizard.rb, line 127
def fetch_buttons()

  a2 = @browser.xpath('//input[@type="button"]')
  @buttons = a2.flat_map do |x|

    a = x.value.split(/\W+/).map {|label| [label, x]}
    a << [x.value, x]
    a + a.map {|x, obj| [x.downcase, obj]}

  end.to_h

  names = @buttons.keys.map(&:downcase).uniq.select {|x| x =~ /^\w+$/}
  buttons = @buttons

  names.each do |name|
    
    define_singleton_method name.to_sym do
      buttons[name].click
      @browser.network.wait_for_idle
      sleep = 1
      self
    end
    
  end

end
method_missing(method_name, *args) click to toggle source
# File lib/ferrumwizard.rb, line 269
def method_missing(method_name, *args)

  puts 'method_missing: ' + method_name.inspect if @debug
  node = @browser.at_css '.' + method_name.to_s
  node.text if node
  
end
scan_form_elements() click to toggle source
# File lib/ferrumwizard.rb, line 214
def scan_form_elements()
  
  # find radio buttons
  
  a = @browser.xpath('//input[@type="radio"]')
  h = a.group_by {|x| x.attribute('name')}
  @radio = h.values
  define_singleton_method(:on) { @radio[0][0].click; self }
  define_singleton_method(:off) { @radio[0][1].click; self }
  
  fetch_buttons()

end