class XAutoBrowse
Attributes
Public Class Methods
note: context is the parent of the document element e.g. frames
# File lib/xautobrowse.rb, line 135 def initialize(browser= :firefox, new_window: true, debug: false, sps: false, clicks: {}, scan_tabs: !new_window, context: nil, text_fields: {}) @browser, @debug, @sps, @clicks = browser.to_sym, debug, sps, clicks @new_window, @context, @text_fields = new_window, context, text_fields @window = Window.new(browser, new_win: new_window, scan_tabs: scan_tabs) sleep 4 if new_window connect() if sps end
Public Instance Methods
custom accesskey (e.g. CTRL+SHIFT+S) typically used to reference an element on the web page
# File lib/xautobrowse.rb, line 153 def accesskey(key) send_keys(key.to_sym) end
Attaches the SPS client to the web browser. The SPS broker must be started before the code can be attached. see start_broker
()
# File lib/xautobrowse.rb, line 160 def attach_console(autohide: true) @window.activate() open_web_console(); sleep 1 clipboard = Clipboard.paste Clipboard.copy javascript(); sleep 1 ctrl_v(); sleep 0.5; carriage_return() close_web_console() if autohide Clipboard.copy clipboard end
# File lib/xautobrowse.rb, line 301 def carriage_return() @window.activate(); sleep 1; XDo::Keyboard.return end
# File lib/xautobrowse.rb, line 174 def click(name) web_console do |x| x.enter [@context, @clicks[name]].compact.join('.') + '.click();' end end
# File lib/xautobrowse.rb, line 182 def close() ctrl_w() end
# File lib/xautobrowse.rb, line 188 def close_web_console() @window.activate() ctrl_shift_i() end
# File lib/xautobrowse.rb, line 195 def connect() start_broker(); sleep 4; connect_controller() end
Connects to the SPS broker to communicate with the web browser
# File lib/xautobrowse.rb, line 203 def connect_controller() @udr = UniversalDomRemote.new debug: @debug end
# File lib/xautobrowse.rb, line 209 def copy_screen() select_all(); sleep 2; ctrl_c(); sleep 2; unselect_all() Clipboard.paste end
# File lib/xautobrowse.rb, line 216 def copy_source() view_source(); sleep 3 select_all(); sleep 1 ctrl_c() # copy the source code to the clipboard sleep 1 ctrl_w() # close the viewsource window end
select all
# File lib/xautobrowse.rb, line 228 def ctrl_a() send_keys(:ctrl_a) end
copy
# File lib/xautobrowse.rb, line 242 def ctrl_c() send_keys(:ctrl_c) end
jump to the location bar
# File lib/xautobrowse.rb, line 246 def ctrl_l() send_keys(:ctrl_l) end
unselect all
# File lib/xautobrowse.rb, line 232 def ctrl_shift_a() XDo::Keyboard.key_down('shift') send_keys(:ctrl_a) XDo::Keyboard.key_up('shift') end
Chromium developer tools
# File lib/xautobrowse.rb, line 262 def ctrl_shift_i() send_keys(:ctrl_shift_i) end
Firefox developer tools
# File lib/xautobrowse.rb, line 266 def ctrl_shift_k() send_keys(:ctrl_shift_k) end
view source code
# File lib/xautobrowse.rb, line 250 def ctrl_u() send_keys(:ctrl_u) end
paste
# File lib/xautobrowse.rb, line 254 def ctrl_v() send_keys(:ctrl_v) end
close the current window
# File lib/xautobrowse.rb, line 258 def ctrl_w() send_keys(:ctrl_w) end
input some text
# File lib/xautobrowse.rb, line 471 def enter(s=nil) if s then type(s) sleep 0.8 end carriage_return() sleep 1 end
submit a form by pressing return
# File lib/xautobrowse.rb, line 270 def go() if block_given? then @window.activate(); sleep 1; yield(self); carriage_return() end end
# File lib/xautobrowse.rb, line 280 def goto(url, attachconsole: true, new_tab: !@new_window) new_tab() if new_tab sleep 1; ctrl_l() sleep 2 if @new_window enter(url) sleep 5 if @new_window attach_console() if @sps and attachconsole end
# File lib/xautobrowse.rb, line 291 def goto_tab(s, url=nil) if url then @window.goto_tab(s) { enter url} else @window.goto_tab(s) end end
# File lib/xautobrowse.rb, line 307 def method_missing(method_name, *args) if method_name.to_s =~ /^alt/ then send_keys method_name end end
# File lib/xautobrowse.rb, line 333 def new_tab() @window.new_tab end
# File lib/xautobrowse.rb, line 329 def new_window() @window = Window.new end
# File lib/xautobrowse.rb, line 313 def open_web_console() console = @browser == :firefox ? :ctrl_shift_k : :ctrl_shift_i method(console).call # web console sleep 2 if block_given? then yield(self) close_web_console() end end
Takes a screenshot of the web page. Images are stored in ~/Pictures
# File lib/xautobrowse.rb, line 339 def screenshot(filename=Time.now\ .strftime("Screenshot from %Y-%m-%d %d-%m-%y")) XDo::Keyboard.simulate('{print}'); sleep 4; XDo::Keyboard.simulate("{down}") sleep 4; XDo::Keyboard.alt_s sleep 5; XDo::Keyboard.type filename sleep 3 XDo::Keyboard.alt_s sleep 1 end
# File lib/xautobrowse.rb, line 361 def select_all() ctrl_a() end
# File lib/xautobrowse.rb, line 357 def send(s) @udr.send s end
# File lib/xautobrowse.rb, line 401 def shift_tab(n=1) @window.activate() XDo::Keyboard.key_down('shift') XDo::Keyboard.simulate("{TAB}" * n) XDo::Keyboard.key_up('shift') end
# File lib/xautobrowse.rb, line 371 def slow_tab(n=1, seconds: 0.5) n.times {XDo::Keyboard.simulate("{TAB}"); sleep seconds} end
# File lib/xautobrowse.rb, line 375 def slow_type(s, seconds: 0.6) #@window.activate(); sleep 0.3 s.split(//).each do |c| XDo::Keyboard.type(c) sleep seconds end end
Starts the simplepubsub broker
# File lib/xautobrowse.rb, line 389 def start_broker() Thread.new do `ruby -r 'simplepubsub' -e "SimplePubSub::Broker.start port: '55000'"` end end
# File lib/xautobrowse.rb, line 397 def stop_broker() SPSPub.notice 'shutdown', host: '127.0.0.1', port: '55000' end
# File lib/xautobrowse.rb, line 410 def tab(n=1) @window.activate(); XDo::Keyboard.simulate("{TAB}" * n) end
determines if the given tabbed window title exists for the given tabbed windows
# File lib/xautobrowse.rb, line 417 def tab?(s) @window.tab?(s) end
# File lib/xautobrowse.rb, line 421 def text_field(id2, klass: nil, id: nil, name: nil, value: '') open_web_console() do |console| cmd = if klass then "querySelector('#{klass}')" elsif id then "getElementById(\"#{id}\")" elsif name then @text_fields[id2] end a = [ 'r = ' + [@context, 'document'].compact.join('.') + '.' + cmd, "r.value = \"\"", "r.focus()" ] a.each {|x| console.enter x } end sleep 2; type(value); sleep 1 end
# File lib/xautobrowse.rb, line 447 def title() @window.title end
# File lib/xautobrowse.rb, line 451 def to_doc() copy_source(); sleep 0.5 Nokorexi.new(Clipboard.paste).to_doc end
type some text
# File lib/xautobrowse.rb, line 458 def type(s) @window.activate(); XDo::Keyboard.type(s) end
# File lib/xautobrowse.rb, line 365 def unselect_all() @browser == :firefox ? (tab(); shift_tab()) : ctrl_shift_a() end
# File lib/xautobrowse.rb, line 460 def view_source() @window.activate(); sleep 0.5 ctrl_l() # jump to the location bar sleep 0.6; tab(2); sleep 0.5; ctrl_u() # View source code end
Private Instance Methods
A helpful method to generate the javascript code necessary for the web browser to communicate with the universal DOM remote
# File lib/xautobrowse.rb, line 489 def javascript() " var ws = new WebSocket('ws://127.0.0.1:55000/'); ws.onopen = function() { console.log('CONNECT'); ws.send('subscribe to topic: udr/controller'); }; ws.onclose = function() { console.log('DISCONNECT'); }; ws.onmessage = function(event) { var a = event.data.split(/: +/,2); console.log(a[1]); try { r = eval(a[1]); } catch(err) { r = err.message; } ws.send('udr/browser: ' + r); }; " end
# File lib/xautobrowse.rb, line 519 def send_keys(keys) @window.activate(); XDo::Keyboard.send keys.to_sym end