Class: XAutoBrowse
- Inherits:
-
Object
- Object
- XAutoBrowse
- Defined in:
- lib/xautobrowse.rb
Defined Under Namespace
Classes: Window
Instance Attribute Summary collapse
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#context ⇒ Object
Returns the value of attribute context.
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Instance Method Summary collapse
-
#accesskey(key) ⇒ Object
(also: #access_key)
custom accesskey (e.g. CTRL+SHIFT+S) typically used to reference an element on the web page.
-
#attach_console(autohide: true) ⇒ Object
Attaches the SPS client to the web browser.
- #carriage_return ⇒ Object (also: #cr)
- #click(name) ⇒ Object
- #close ⇒ Object (also: #exit)
- #close_web_console ⇒ Object (also: #hide_web_console)
- #connect ⇒ Object
-
#connect_controller ⇒ Object
Connects to the SPS broker to communicate with the web browser.
- #copy_screen ⇒ Object (also: #scrape_screen)
- #copy_source ⇒ Object
-
#ctrl_a ⇒ Object
select all.
-
#ctrl_c ⇒ Object
copy.
-
#ctrl_l ⇒ Object
jump to the location bar.
-
#ctrl_shift_a ⇒ Object
unselect all.
-
#ctrl_shift_i ⇒ Object
Chromium developer tools.
-
#ctrl_shift_k ⇒ Object
Firefox developer tools.
-
#ctrl_u ⇒ Object
view source code.
-
#ctrl_v ⇒ Object
paste.
-
#ctrl_w ⇒ Object
close the current window.
-
#enter(s = nil) ⇒ Object
input some text.
-
#go ⇒ Object
submit a form by pressing return.
- #goto(url, attachconsole: true, new_tab: !@new_window)) ⇒ Object
- #goto_tab(s, url = nil) ⇒ Object
-
#initialize(browser = :firefox, new_window: true, debug: false, sps: false, clicks: {}, scan_tabs: !new_window,, context: nil, text_fields: {}) ⇒ XAutoBrowse
constructor
A new instance of XAutoBrowse.
- #method_missing(method_name, *args) ⇒ Object
- #new_tab ⇒ Object
- #new_window ⇒ Object
- #open_web_console ⇒ Object (also: #web_console)
-
#screenshot(filename = Time.now\ .strftime("Screenshot from %Y-%m-%d %d-%m-%y")) ⇒ Object
(also: #screen_capture)
Takes a screenshot of the web page.
- #select_all ⇒ Object
- #send(s) ⇒ Object
- #shift_tab(n = 1) ⇒ Object
- #slow_tab(n = 1, seconds: 0.5) ⇒ Object
- #slow_type(s, seconds: 0.6) ⇒ Object
-
#start_broker ⇒ Object
Starts the simplepubsub broker.
- #stop_broker ⇒ Object
- #tab(n = 1) ⇒ Object
-
#tab?(s) ⇒ Boolean
determines if the given tabbed window title exists for the given tabbed windows.
- #text_field(id2, klass: nil, id: nil, name: nil, value: '') ⇒ Object
- #title ⇒ Object
- #to_doc ⇒ Object
-
#type(s) ⇒ Object
type some text.
- #unselect_all ⇒ Object
- #view_source ⇒ Object
Constructor Details
#initialize(browser = :firefox, new_window: true, debug: false, sps: false, clicks: {}, scan_tabs: !new_window,, context: nil, text_fields: {}) ⇒ XAutoBrowse
Returns a new instance of XAutoBrowse.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/xautobrowse.rb', line 139 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 puts 'before Window.new' if @debug @window = Window.new(browser, new_win: new_window, scan_tabs: scan_tabs, debug: @debug) sleep 4 if new_window connect() if sps end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
313 314 315 316 317 |
# File 'lib/xautobrowse.rb', line 313 def method_missing(method_name, *args) if method_name.to_s =~ /^alt/ then send_keys method_name end end |
Instance Attribute Details
#actions ⇒ Object
Returns the value of attribute actions.
133 134 135 |
# File 'lib/xautobrowse.rb', line 133 def actions @actions end |
#context ⇒ Object
Returns the value of attribute context.
133 134 135 |
# File 'lib/xautobrowse.rb', line 133 def context @context end |
#window ⇒ Object (readonly)
Returns the value of attribute window.
132 133 134 |
# File 'lib/xautobrowse.rb', line 132 def window @window end |
Instance Method Details
#accesskey(key) ⇒ Object Also known as: access_key
custom accesskey (e.g. CTRL+SHIFT+S) typically used to reference an element on the web page
159 |
# File 'lib/xautobrowse.rb', line 159 def accesskey(key) send_keys(key.to_sym) end |
#attach_console(autohide: true) ⇒ Object
Attaches the SPS client to the web browser. The SPS broker must be started before the code can be attached. see start_broker()
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/xautobrowse.rb', line 166 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 |
#carriage_return ⇒ Object Also known as: cr
307 308 309 |
# File 'lib/xautobrowse.rb', line 307 def carriage_return() @window.activate(); sleep 1; XDo::Keyboard.return end |
#click(name) ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/xautobrowse.rb', line 180 def click(name) web_console do |x| x.enter [@context, @clicks[name]].compact.join('.') + '.click();' end end |
#close ⇒ Object Also known as: exit
188 189 190 |
# File 'lib/xautobrowse.rb', line 188 def close() ctrl_w() end |
#close_web_console ⇒ Object Also known as: hide_web_console
194 195 196 197 |
# File 'lib/xautobrowse.rb', line 194 def close_web_console() @window.activate() ctrl_shift_i() end |
#connect ⇒ Object
201 202 203 204 205 |
# File 'lib/xautobrowse.rb', line 201 def connect() start_broker(); sleep 4; connect_controller() end |
#connect_controller ⇒ Object
Connects to the SPS broker to communicate with the web browser
209 210 211 212 213 |
# File 'lib/xautobrowse.rb', line 209 def connect_controller() @udr = UniversalDomRemote.new debug: @debug end |
#copy_screen ⇒ Object Also known as: scrape_screen
215 216 217 218 |
# File 'lib/xautobrowse.rb', line 215 def copy_screen() select_all(); sleep 2; ctrl_c(); sleep 2; unselect_all() Clipboard.paste end |
#copy_source ⇒ Object
222 223 224 225 226 227 228 229 230 |
# File 'lib/xautobrowse.rb', line 222 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 |
#ctrl_a ⇒ Object
select all
234 |
# File 'lib/xautobrowse.rb', line 234 def ctrl_a() send_keys(:ctrl_a) end |
#ctrl_c ⇒ Object
copy
248 |
# File 'lib/xautobrowse.rb', line 248 def ctrl_c() send_keys(:ctrl_c) end |
#ctrl_l ⇒ Object
jump to the location bar
252 |
# File 'lib/xautobrowse.rb', line 252 def ctrl_l() send_keys(:ctrl_l) end |
#ctrl_shift_a ⇒ Object
unselect all
238 239 240 241 242 243 244 |
# File 'lib/xautobrowse.rb', line 238 def ctrl_shift_a() XDo::Keyboard.key_down('shift') send_keys(:ctrl_a) XDo::Keyboard.key_up('shift') end |
#ctrl_shift_i ⇒ Object
Chromium developer tools
268 |
# File 'lib/xautobrowse.rb', line 268 def ctrl_shift_i() send_keys(:ctrl_shift_i) end |
#ctrl_shift_k ⇒ Object
Firefox developer tools
272 |
# File 'lib/xautobrowse.rb', line 272 def ctrl_shift_k() send_keys(:ctrl_shift_k) end |
#ctrl_u ⇒ Object
view source code
256 |
# File 'lib/xautobrowse.rb', line 256 def ctrl_u() send_keys(:ctrl_u) end |
#ctrl_v ⇒ Object
paste
260 |
# File 'lib/xautobrowse.rb', line 260 def ctrl_v() send_keys(:ctrl_v) end |
#ctrl_w ⇒ Object
close the current window
264 |
# File 'lib/xautobrowse.rb', line 264 def ctrl_w() send_keys(:ctrl_w) end |
#enter(s = nil) ⇒ Object
input some text
482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/xautobrowse.rb', line 482 def enter(s=nil) if s then type(s) sleep 0.8 end carriage_return() sleep 1 end |
#go ⇒ Object
submit a form by pressing return
276 277 278 279 280 281 282 283 284 |
# File 'lib/xautobrowse.rb', line 276 def go() if block_given? then @window.activate(); sleep 1; yield(self); carriage_return() end end |
#goto(url, attachconsole: true, new_tab: !@new_window)) ⇒ Object
286 287 288 289 290 291 292 293 294 295 |
# File 'lib/xautobrowse.rb', line 286 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 |
#goto_tab(s, url = nil) ⇒ Object
297 298 299 300 301 302 303 304 305 |
# File 'lib/xautobrowse.rb', line 297 def goto_tab(s, url=nil) if url then @window.goto_tab(s) { enter url} else @window.goto_tab(s) end end |
#new_tab ⇒ Object
339 340 341 |
# File 'lib/xautobrowse.rb', line 339 def new_tab() @window.new_tab end |
#new_window ⇒ Object
335 336 337 |
# File 'lib/xautobrowse.rb', line 335 def new_window() @window = Window.new end |
#open_web_console ⇒ Object Also known as: web_console
319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/xautobrowse.rb', line 319 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 |
#screenshot(filename = Time.now\ .strftime("Screenshot from %Y-%m-%d %d-%m-%y")) ⇒ Object Also known as: screen_capture
Takes a screenshot of the web page. Images are stored in ~/Pictures
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/xautobrowse.rb', line 345 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 |
#select_all ⇒ Object
367 368 369 |
# File 'lib/xautobrowse.rb', line 367 def select_all() ctrl_a() end |
#send(s) ⇒ Object
363 364 365 |
# File 'lib/xautobrowse.rb', line 363 def send(s) @udr.send s end |
#shift_tab(n = 1) ⇒ Object
407 408 409 410 411 412 413 414 |
# File 'lib/xautobrowse.rb', line 407 def shift_tab(n=1) @window.activate() XDo::Keyboard.key_down('shift') XDo::Keyboard.simulate("{TAB}" * n) XDo::Keyboard.key_up('shift') end |
#slow_tab(n = 1, seconds: 0.5) ⇒ Object
377 378 379 |
# File 'lib/xautobrowse.rb', line 377 def slow_tab(n=1, seconds: 0.5) n.times {XDo::Keyboard.simulate("{TAB}"); sleep seconds} end |
#slow_type(s, seconds: 0.6) ⇒ Object
381 382 383 384 385 386 387 388 389 390 |
# File 'lib/xautobrowse.rb', line 381 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 |
#start_broker ⇒ Object
Starts the simplepubsub broker
395 396 397 398 399 400 401 |
# File 'lib/xautobrowse.rb', line 395 def start_broker() Thread.new do `ruby -r 'simplepubsub' -e "SimplePubSub::Broker.start port: '55000'"` end end |
#stop_broker ⇒ Object
403 404 405 |
# File 'lib/xautobrowse.rb', line 403 def stop_broker() SPSPub.notice 'shutdown', host: '127.0.0.1', port: '55000' end |
#tab(n = 1) ⇒ Object
416 417 418 |
# File 'lib/xautobrowse.rb', line 416 def tab(n=1) @window.activate(); XDo::Keyboard.simulate("{TAB}" * n) end |
#tab?(s) ⇒ Boolean
determines if the given tabbed window title exists for the given tabbed windows
423 424 425 |
# File 'lib/xautobrowse.rb', line 423 def tab?(s) @window.tab?(s) end |
#text_field(id2, klass: nil, id: nil, name: nil, value: '') ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/xautobrowse.rb', line 427 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 |
#title ⇒ Object
453 454 455 |
# File 'lib/xautobrowse.rb', line 453 def title() @window.title end |
#to_doc ⇒ Object
457 458 459 460 |
# File 'lib/xautobrowse.rb', line 457 def to_doc() copy_source(); sleep 0.5 Nokorexi.new(Clipboard.paste).to_doc end |
#type(s) ⇒ Object
type some text
464 465 466 467 468 469 |
# File 'lib/xautobrowse.rb', line 464 def type(s) @window.activate(); s.chars.each {|char| XDo::Keyboard.type(char) ; sleep 0.008 } end |
#unselect_all ⇒ Object
371 372 373 374 375 |
# File 'lib/xautobrowse.rb', line 371 def unselect_all() @browser == :firefox ? (tab(); shift_tab()) : ctrl_shift_a() end |
#view_source ⇒ Object
471 472 473 474 475 476 477 |
# File 'lib/xautobrowse.rb', line 471 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 |