Class: XAutoBrowse

Inherits:
Object
  • Object
show all
Defined in:
lib/xautobrowse.rb

Defined Under Namespace

Classes: Window

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser = :firefox, new_window: true, debug: false, sps: false, clicks: {}, scan_tabs: !new_window,, context: nil) ⇒ XAutoBrowse

Returns a new instance of XAutoBrowse.



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/xautobrowse.rb', line 135

def initialize(browser= :firefox, new_window: true, debug: false, 
               sps: false, clicks: {}, scan_tabs: !new_window, context: nil)

  @browser, @debug, @sps, @clicks = browser.to_sym, debug, sps, clicks
  @new_window, @context = new_window, context
  
  @window = Window.new(browser, new_win: new_window, scan_tabs: scan_tabs)

  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



306
307
308
309
310
# File 'lib/xautobrowse.rb', line 306

def method_missing(method_name, *args)
  if method_name.to_s =~ /^alt/ then
    send_keys method_name
  end
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



129
130
131
# File 'lib/xautobrowse.rb', line 129

def actions
  @actions
end

#contextObject

Returns the value of attribute context.



129
130
131
# File 'lib/xautobrowse.rb', line 129

def context
  @context
end

#windowObject (readonly)

Returns the value of attribute window.



128
129
130
# File 'lib/xautobrowse.rb', line 128

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



152
# File 'lib/xautobrowse.rb', line 152

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()



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/xautobrowse.rb', line 159

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_returnObject Also known as: cr



300
301
302
# File 'lib/xautobrowse.rb', line 300

def carriage_return()
  @window.activate(); sleep 1; XDo::Keyboard.return
end

#click(name) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/xautobrowse.rb', line 173

def click(name)
  
  web_console do |x| 
    x.enter [@context, @clicks[name]].compact.join('.') + '.click();'
  end
  
end

#closeObject Also known as: exit



181
182
183
# File 'lib/xautobrowse.rb', line 181

def close()
  ctrl_w()
end

#close_web_consoleObject Also known as: hide_web_console



187
188
189
190
# File 'lib/xautobrowse.rb', line 187

def close_web_console()
  @window.activate()
  ctrl_shift_i()
end

#connectObject



194
195
196
197
198
# File 'lib/xautobrowse.rb', line 194

def connect()

  start_broker(); sleep 4; connect_controller()
  
end

#connect_controllerObject

Connects to the SPS broker to communicate with the web browser



202
203
204
205
206
# File 'lib/xautobrowse.rb', line 202

def connect_controller()
  
  @udr = UniversalDomRemote.new debug: @debug
  
end

#copy_screenObject Also known as: scrape_screen



208
209
210
211
# File 'lib/xautobrowse.rb', line 208

def copy_screen()
  select_all(); sleep 2; ctrl_c(); sleep 2; unselect_all()
  Clipboard.paste    
end

#copy_sourceObject



215
216
217
218
219
220
221
222
223
# File 'lib/xautobrowse.rb', line 215

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_aObject

select all



227
# File 'lib/xautobrowse.rb', line 227

def ctrl_a() send_keys(:ctrl_a)  end

#ctrl_cObject

copy



241
# File 'lib/xautobrowse.rb', line 241

def ctrl_c() send_keys(:ctrl_c)  end

#ctrl_lObject

jump to the location bar



245
# File 'lib/xautobrowse.rb', line 245

def ctrl_l() send_keys(:ctrl_l)  end

#ctrl_shift_aObject

unselect all



231
232
233
234
235
236
237
# File 'lib/xautobrowse.rb', line 231

def ctrl_shift_a()

  XDo::Keyboard.key_down('shift')
  send_keys(:ctrl_a)
  XDo::Keyboard.key_up('shift')    

end

#ctrl_shift_iObject

Chromium developer tools



261
# File 'lib/xautobrowse.rb', line 261

def ctrl_shift_i() send_keys(:ctrl_shift_i) end

#ctrl_shift_kObject

Firefox developer tools



265
# File 'lib/xautobrowse.rb', line 265

def ctrl_shift_k() send_keys(:ctrl_shift_k) end

#ctrl_uObject

view source code



249
# File 'lib/xautobrowse.rb', line 249

def ctrl_u() send_keys(:ctrl_u)  end

#ctrl_vObject

paste



253
# File 'lib/xautobrowse.rb', line 253

def ctrl_v() send_keys(:ctrl_v)  end

#ctrl_wObject

close the current window



257
# File 'lib/xautobrowse.rb', line 257

def ctrl_w() send_keys(:ctrl_w)  end

#enter(s = nil) ⇒ Object

input some text



452
453
454
455
456
457
458
459
460
461
462
# File 'lib/xautobrowse.rb', line 452

def enter(s=nil)
  
  if s then
    type(s)
    sleep 0.8
  end
  
  carriage_return()
  sleep 1
  
end

#goObject

submit a form by pressing return



269
270
271
272
273
274
275
276
277
# File 'lib/xautobrowse.rb', line 269

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



279
280
281
282
283
284
285
286
287
288
# File 'lib/xautobrowse.rb', line 279

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



290
291
292
293
294
295
296
297
298
# File 'lib/xautobrowse.rb', line 290

def goto_tab(s, url=nil)
  
  if url then      
    @window.goto_tab(s) { enter url}
  else
    @window.goto_tab(s)
  end
  
end

#new_tabObject



332
333
334
# File 'lib/xautobrowse.rb', line 332

def new_tab()
  @window.new_tab
end

#new_windowObject



328
329
330
# File 'lib/xautobrowse.rb', line 328

def new_window()
  @window = Window.new
end

#open_web_consoleObject Also known as: web_console



312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/xautobrowse.rb', line 312

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



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/xautobrowse.rb', line 338

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_allObject



360
361
362
# File 'lib/xautobrowse.rb', line 360

def select_all()
  ctrl_a()
end

#send(s) ⇒ Object



356
357
358
# File 'lib/xautobrowse.rb', line 356

def send(s)
  @udr.send s
end

#shift_tab(n = 1) ⇒ Object



384
385
386
387
388
389
390
391
# File 'lib/xautobrowse.rb', line 384

def shift_tab(n=1)
  
  @window.activate()
  XDo::Keyboard.key_down('shift')
  XDo::Keyboard.simulate("{TAB}" * n)
  XDo::Keyboard.key_up('shift')
  
end

#start_brokerObject

Starts the simplepubsub broker



372
373
374
375
376
377
378
# File 'lib/xautobrowse.rb', line 372

def start_broker()
  
  Thread.new do 
    `ruby -r 'simplepubsub' -e "SimplePubSub::Broker.start port: '55000'"`
  end
  
end

#stop_brokerObject



380
381
382
# File 'lib/xautobrowse.rb', line 380

def stop_broker()
  SPSPub.notice 'shutdown', host: '127.0.0.1', port: '55000'
end

#tab(n = 1) ⇒ Object



393
394
395
# File 'lib/xautobrowse.rb', line 393

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

Returns:

  • (Boolean)


400
401
402
# File 'lib/xautobrowse.rb', line 400

def tab?(s)
  @window.tab?(s)
end

#text_field(klass: nil, id: nil, name: nil, value: '') ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/xautobrowse.rb', line 404

def text_field(klass: nil, id: nil, name: nil, value: '')
  
  open_web_console() do |console|
  
    cmd = if klass then
      "querySelector('#{klass}')"
    elsif id then
      "getElementById(\"#{id}\")"
    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

#titleObject



428
429
430
# File 'lib/xautobrowse.rb', line 428

def title()
  @window.title
end

#to_docObject



432
433
434
435
# File 'lib/xautobrowse.rb', line 432

def to_doc()
  copy_source(); sleep 0.5
  Nokorexi.new(Clipboard.paste).to_doc
end

#type(s) ⇒ Object

type some text



439
# File 'lib/xautobrowse.rb', line 439

def type(s)  @window.activate(); XDo::Keyboard.type(s)  end

#unselect_allObject



364
365
366
367
368
# File 'lib/xautobrowse.rb', line 364

def unselect_all()
  
  @browser == :firefox ? (tab(); shift_tab()) : ctrl_shift_a()
  
end

#view_sourceObject



441
442
443
444
445
446
447
# File 'lib/xautobrowse.rb', line 441

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