Class: Webdrone::Browser
- Inherits:
-
Object
- Object
- Webdrone::Browser
- Defined in:
- lib/webdrone/clic.rb,
lib/webdrone/conf.rb,
lib/webdrone/ctxt.rb,
lib/webdrone/exec.rb,
lib/webdrone/find.rb,
lib/webdrone/form.rb,
lib/webdrone/html.rb,
lib/webdrone/mark.rb,
lib/webdrone/open.rb,
lib/webdrone/shot.rb,
lib/webdrone/text.rb,
lib/webdrone/vrfy.rb,
lib/webdrone/wait.rb,
lib/webdrone/xlsx.rb,
lib/webdrone/browser.rb
Constant Summary collapse
- @@firefox_profile =
@@chrome_prefs = nil
Instance Attribute Summary collapse
-
#driver ⇒ Object
Returns the value of attribute driver.
Class Method Summary collapse
Instance Method Summary collapse
- #clic ⇒ Object
- #conf ⇒ Object
- #console(binding = nil) ⇒ Object
- #ctxt ⇒ Object
- #env_update(binding) ⇒ Object
- #exec ⇒ Object
- #find ⇒ Object
- #form ⇒ Object
- #html ⇒ Object
-
#initialize(browser: 'firefox', create_outdir: true, outdir: nil, timeout:, developer: false, quit_at_exit: false, maximize: true, error: :raise_report, win_x: nil, win_y: nil, win_w: nil, win_h: nil, use_env: true, chrome_prefs: nil, firefox_profile: nil) ⇒ Browser
constructor
A new instance of Browser.
- #mark ⇒ Object
- #maximize ⇒ Object
- #open ⇒ Object
- #quit ⇒ Object
- #shot ⇒ Object
- #text ⇒ Object
- #vrfy ⇒ Object
- #wait ⇒ Object
- #xlsx ⇒ Object
Constructor Details
#initialize(browser: 'firefox', create_outdir: true, outdir: nil, timeout:, developer: false, quit_at_exit: false, maximize: true, error: :raise_report, win_x: nil, win_y: nil, win_w: nil, win_h: nil, use_env: true, chrome_prefs: nil, firefox_profile: nil) ⇒ Browser
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/webdrone/browser.rb', line 53 def initialize(browser: 'firefox', create_outdir: true, outdir: nil, timeout:, developer: false, quit_at_exit: false, maximize: true, error: :raise_report, win_x: nil, win_y: nil, win_w: nil, win_h: nil, use_env: true, chrome_prefs: nil, firefox_profile: nil) env_update(Kernel.binding) if use_env if create_outdir or outdir outdir ||= File.join("webdrone_output", Time.new.strftime('%Y%m%d_%H%M%S')) self.conf.outdir = outdir end outdir = File.join(Dir.pwd, outdir) if outdir != nil and not Pathname.new(outdir).absolute? if outdir != nil and browser.to_sym == :chrome chrome_prefs = Browser.chrome_prefs if chrome_prefs == nil chrome_prefs[:download][:default_directory] = outdir @driver = Selenium::WebDriver.for browser.to_sym, prefs: chrome_prefs, args: ['--disable-popup-blocking'] elsif outdir != nil and browser.to_sym == :firefox firefox_profile = Browser.firefox_profile if firefox_profile == nil downdir = OS.windows? ? outdir.gsub("/", "\\") : outdir firefox_profile['browser.download.dir'] = downdir @driver = Selenium::WebDriver.for browser.to_sym, profile: firefox_profile else @driver = Selenium::WebDriver.for browser.to_sym end if quit_at_exit at_exit do begin @driver.quit rescue end end end self.conf.error = error.to_sym self.conf.developer = developer self.conf.timeout = timeout.to_i if timeout if developer win_x = win_y = 0 win_w = 0.5 win_h = 1.0 end if win_x or win_y or win_w or win_h x, y, w, h = self.exec.script 'return [window.screenLeft ? window.screenLeft : window.screenX, window.screenTop ? window.screenTop : window.screenY, window.screen.availWidth, window.screen.availHeight];' win_x ||= x win_y ||= y if win_w.is_a? Float win_w = (w * win_w).to_i else win_w ||= w end if win_h.is_a? Float win_h = (h * win_h).to_i else win_h ||= h end @driver.manage.window.position= Selenium::WebDriver::Point.new win_x, win_y @driver.manage.window.resize_to(win_w, win_h) else self.maximize if maximize end end |
Instance Attribute Details
#driver ⇒ Object
Returns the value of attribute driver.
4 5 6 |
# File 'lib/webdrone/browser.rb', line 4 def driver @driver end |
Class Method Details
.chrome_prefs ⇒ Object
21 22 23 24 25 26 |
# File 'lib/webdrone/browser.rb', line 21 def self.chrome_prefs if @@chrome_prefs == nil @@chrome_prefs = { download: { prompt_for_download: false } } end @@chrome_prefs end |
.firefox_profile ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/webdrone/browser.rb', line 6 def self.firefox_profile if @@firefox_profile == nil @@firefox_profile = Selenium::WebDriver::Firefox::Profile.new @@firefox_profile['startup.homepage_welcome_url.additional'] = 'about:blank' @@firefox_profile['browser.download.folderList'] = 2 @@firefox_profile['browser.download.manager.showWhenStarting'] = false @@firefox_profile['browser.helperApps.neverAsk.saveToDisk'] = "images/jpeg, application/pdf, application/octet-stream, application/download" end @@firefox_profile end |
.firefox_profile=(firefox_profile) ⇒ Object
17 18 19 |
# File 'lib/webdrone/browser.rb', line 17 def self.firefox_profile=(firefox_profile) @@firefox_profile = firefox_profile end |
Instance Method Details
#clic ⇒ Object
3 4 5 |
# File 'lib/webdrone/clic.rb', line 3 def clic @clic ||= Clic.new self end |
#conf ⇒ Object
3 4 5 |
# File 'lib/webdrone/conf.rb', line 3 def conf @conf ||= Conf.new self end |
#console(binding = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/webdrone/browser.rb', line 118 def console(binding = nil) binding = Kernel.binding.of_caller(1) unless binding old_error = self.conf.error old_developer = self.conf.developer begin self.conf.error = :raise self.conf.developer = false Webdrone.pry_console binding ensure self.conf.error = old_error self.conf.developer = old_developer end end |
#ctxt ⇒ Object
3 4 5 |
# File 'lib/webdrone/ctxt.rb', line 3 def ctxt @ctxt ||= Ctxt.new self end |
#env_update(binding) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/webdrone/browser.rb', line 28 def env_update(binding) bool_vars = [:create_outdir, :developer, :quit_at_exit, :maximize] ENV.keys.select { |env| env.start_with? 'WEBDRONE_' }.each do |env| v = env[9..-1].downcase.to_sym if binding.local_variable_defined? v o = binding.local_variable_get(v) n = ENV[env] if bool_vars.include? v if n == "true" n = true elsif n == "false" n = false else puts "Webdrone: ignoring value '#{n}' for boolean parameter #{v}." next end end binding.local_variable_set(v, n) puts "Webdrone: overriding #{v} from '#{o}' to '#{n}'." else puts "Webdrone: ignoring unknown parameter #{env}." end end end |
#exec ⇒ Object
3 4 5 |
# File 'lib/webdrone/exec.rb', line 3 def exec @exec ||= Exec.new self end |
#find ⇒ Object
3 4 5 |
# File 'lib/webdrone/find.rb', line 3 def find @find ||= Find.new self end |
#form ⇒ Object
3 4 5 |
# File 'lib/webdrone/form.rb', line 3 def form @form ||= Form.new self end |
#html ⇒ Object
3 4 5 |
# File 'lib/webdrone/html.rb', line 3 def html @html ||= Html.new self end |
#mark ⇒ Object
3 4 5 |
# File 'lib/webdrone/mark.rb', line 3 def mark @mark ||= Mark.new self end |
#maximize ⇒ Object
110 111 112 |
# File 'lib/webdrone/browser.rb', line 110 def maximize @driver.manage.window.maximize end |
#open ⇒ Object
3 4 5 |
# File 'lib/webdrone/open.rb', line 3 def open @open ||= Open.new self end |
#quit ⇒ Object
114 115 116 |
# File 'lib/webdrone/browser.rb', line 114 def quit @driver.quit end |
#shot ⇒ Object
3 4 5 |
# File 'lib/webdrone/shot.rb', line 3 def shot @shot ||= Shot.new self end |
#text ⇒ Object
3 4 5 |
# File 'lib/webdrone/text.rb', line 3 def text @text ||= Text.new self end |
#vrfy ⇒ Object
3 4 5 |
# File 'lib/webdrone/vrfy.rb', line 3 def vrfy @vrfy ||= Vrfy.new self end |
#wait ⇒ Object
3 4 5 |
# File 'lib/webdrone/wait.rb', line 3 def wait @wait ||= Wait.new self end |
#xlsx ⇒ Object
3 4 5 |
# File 'lib/webdrone/xlsx.rb', line 3 def xlsx @xlsx ||= Xlsx.new self end |