class FerrumWizard
Attributes
browser[R]
js_methods[R]
links[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_links()
click to toggle source
# File lib/ferrumwizard.rb, line 154 def fetch_links() all_links = @doc.root.xpath('//a[@href]') all_links.each do |x| if x.plaintext.empty? then x.text = x.attributes[:href].sub(/\.\w+$/,'')[/([^\/]+)$/].split(/[_]|(?=[A-Z])/).join(' ') else x.text = x.plaintext.gsub('&','&') end end valid_links = all_links.reject do |x| puts 'x: ' + x.inspect if @debug r = (x.attributes[:target] == '_blank') puts 'r: ' + r.inspect if @debug r end indices = valid_links.map {|x| all_links.index x} active_links = @browser.xpath('//a[@href]') valid_active_links = indices.map {|n| active_links[n]} @links = valid_active_links.flat_map.with_index do |x, i| a = valid_links[i].text.split(/\W+/).map {|label| [label, x]} a << [valid_links[i].text, x] puts 'a: ' + a.inspect if @debug a + a.map {|x2, obj| [x2.downcase, obj]} end.to_h names = @links.keys.map(&:downcase).uniq.select {|x| x =~ /^[\w ]+$/} links = @links names.each do |name| define_singleton_method name.gsub(/ +/,'_').to_sym do links[name].click @browser.network.wait_for_idle sleep 1 scan_page() 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
scan_js_links()
click to toggle source
# File lib/ferrumwizard.rb, line 228 def scan_js_links() @js_methods = {} b = @browser b.xpath('//a').select {|x| x.attribute('href') =~ /^javascript/}.each do |e| s = e.attribute('href')[/(?<=^javascript:)[^\(]+/] puts 's: ' + s.inspect if @debug a = s.split(/\W+|(?=[A-Z])/).map {|label| [label, s]} a << [s, s] a << [s.split(/\W+|(?=[A-Z])/).join('_'), s] a << [s.split(/\W+|(?=[A-Z])/).join('_').downcase, s] #@js_methods[s] = a a.concat a.map {|x, name| [x.downcase, name] } puts 'a: ' + a.inspect if @debug a.uniq.select {|x, _| x =~ /^[a-z0-9_]+$/}.each do |x, name| if @debug then puts 'x: ' + x.inspect puts 'name: ' + name.inspect end define_singleton_method(x.to_sym) do |*args| #args = raw_args.map {|x| x[/^[0-9]+$/] ? x.to_i : x} js_method = "%s(%s)" % [name, args.map(&:inspect).join(', ')] puts 'js_method: ' + js_method.inspect if @debug @browser.evaluate(js_method) sleep 4 self.scan_page() end end end end