Class: Webdrone::Browser

Inherits:
Object
  • Object
show all
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/logg.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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser: 'firefox', create_outdir: true, outdir: nil, timeout: 30, developer: false, logger: true, quit_at_exit: true, maximize: true, error: :raise_report, win_x: nil, win_y: nil, win_w: nil, win_h: nil, use_env: true, chrome_options: nil, firefox_options: nil, firefox_profile: nil, remote_url: nil, headless: false) ⇒ Browser

Returns a new instance of Browser.



43
44
45
46
47
48
49
50
51
52
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
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/webdrone/browser.rb', line 43

def initialize(browser: 'firefox', create_outdir: true, outdir: nil, timeout: 30, developer: false, logger: true, quit_at_exit: true, maximize: true, error: :raise_report, win_x: nil, win_y: nil, win_w: nil, win_h: nil, use_env: true, chrome_options: nil, firefox_options: nil, firefox_profile: nil, remote_url: nil, headless: false)
  env_update(Kernel.binding) if use_env

  if create_outdir || outdir
    outdir ||= File.join('webdrone_output', Time.new.strftime('%Y%m%d_%H%M%S'))
    conf.outdir = outdir
  end
  outdir = File.join(Dir.pwd, outdir) if !outdir.nil? && !Pathname.new(outdir).absolute?

  if remote_url
    @driver = Selenium::WebDriver.for :remote, url: remote_url, desired_capabilities: browser.to_sym
  elsif !outdir.nil? && browser.to_sym == :chrome
    chrome_options ||= Browser.chrome_options
    chrome_options.add_preference 'download.default_directory', outdir
    chrome_options.add_argument '--disable-popup-blocking'
    chrome_options.add_argument '--headless' if headless
    chrome_options.add_argument '--start-maximized' if maximize
    maximize = false

    service = Selenium::WebDriver::Service.chrome(args: { log_path: "/tmp/chromedriver.#{$PID}.log", verbose: true })
    @driver = Selenium::WebDriver.for browser.to_sym, options: chrome_options, service: service
  elsif !outdir.nil? && browser.to_sym == :firefox
    firefox_options ||= Browser.firefox_options
    firefox_profile ||= Browser.firefox_profile

    firefox_options.add_argument '-headless' if headless
    downdir = OS.windows? ? outdir.tr('/', '\\') : outdir
    firefox_profile['browser.download.dir'] = downdir
    firefox_options.profile = firefox_profile
    @driver = Selenium::WebDriver.for browser.to_sym, options: firefox_options
  else
    @driver = Selenium::WebDriver.for browser.to_sym
  end

  if quit_at_exit
    at_exit do
      @driver.quit
    rescue StandardError
      nil
    end
  end

  conf.error = error.to_sym
  conf.developer = developer
  conf.timeout = timeout.to_i if timeout
  conf.logger = logger

  if developer
    win_x = win_y = 0
    win_w = 0.5
    win_h = 1.0
  end

  if win_x || win_y || win_w || win_h
    x, y, w, h = 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
    begin
      @driver.manage.window.position = Selenium::WebDriver::Point.new win_x, win_y
      @driver.manage.window.resize_to(win_w, win_h)
    rescue StandardError => e
      puts "Ignoring error on window position/resize: #{e}"
    end
  elsif maximize
    self.maximize
  end
end

Class Attribute Details

.chrome_optionsObject



32
33
34
35
36
37
38
39
40
# File 'lib/webdrone/browser.rb', line 32

def chrome_options
  return @chrome_options if defined? @chrome_options

  @chrome_options = Selenium::WebDriver::Chrome::Options.new
  @chrome_options.add_preference 'download.prompt_for_download', false
  @chrome_options.add_preference 'credentials_enable_service:', false

  @chrome_options
end

.firefox_profileObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/webdrone/browser.rb', line 12

def firefox_profile
  return @firefox_profile if defined? @firefox_profile

  @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'

  @firefox_profile
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



7
8
9
# File 'lib/webdrone/browser.rb', line 7

def driver
  @driver
end

Class Method Details

.firefox_optionsObject



24
25
26
27
28
29
30
# File 'lib/webdrone/browser.rb', line 24

def firefox_options
  return @firefox_options if defined? @firefox_options

  @firefox_options = Selenium::WebDriver::Firefox::Options.new

  @firefox_options
end

Instance Method Details

#clicObject



5
6
7
# File 'lib/webdrone/clic.rb', line 5

def clic
  @clic ||= Clic.new self
end

#confObject



5
6
7
# File 'lib/webdrone/conf.rb', line 5

def conf
  @conf ||= Conf.new self
end

#console(binding = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/webdrone/browser.rb', line 129

def console(binding = nil)
  return unless conf.developer

  binding ||= Kernel.binding.of_caller(1)
  old_error = conf.error
  old_developer = conf.developer
  begin
    conf.error = :raise
    conf.developer = false
    Webdrone.pry_console binding
  ensure
    conf.error = old_error
    conf.developer = old_developer
  end
end

#ctxtObject



5
6
7
# File 'lib/webdrone/ctxt.rb', line 5

def ctxt
  @ctxt ||= Ctxt.new self
end

#execObject



5
6
7
# File 'lib/webdrone/exec.rb', line 5

def exec
  @exec ||= Exec.new self
end

#findObject



5
6
7
# File 'lib/webdrone/find.rb', line 5

def find
  @find ||= Find.new self
end

#formObject



5
6
7
# File 'lib/webdrone/form.rb', line 5

def form
  @form ||= Form.new self
end

#htmlObject



5
6
7
# File 'lib/webdrone/html.rb', line 5

def html
  @html ||= Html.new self
end

#logsObject



83
84
85
# File 'lib/webdrone/logg.rb', line 83

def logs
  @logs ||= Logs.new self
end

#markObject



5
6
7
# File 'lib/webdrone/mark.rb', line 5

def mark
  @mark ||= Mark.new self
end

#maximizeObject



121
122
123
# File 'lib/webdrone/browser.rb', line 121

def maximize
  @driver.manage.window.maximize
end

#openObject



5
6
7
# File 'lib/webdrone/open.rb', line 5

def open
  @open ||= Open.new self
end

#quitObject



125
126
127
# File 'lib/webdrone/browser.rb', line 125

def quit
  @driver.quit
end

#shotObject



5
6
7
# File 'lib/webdrone/shot.rb', line 5

def shot
  @shot ||= Shot.new self
end

#textObject



5
6
7
# File 'lib/webdrone/text.rb', line 5

def text
  @text ||= Text.new self
end

#vrfyObject



5
6
7
# File 'lib/webdrone/vrfy.rb', line 5

def vrfy
  @vrfy ||= Vrfy.new self
end

#waitObject



5
6
7
# File 'lib/webdrone/wait.rb', line 5

def wait
  @wait ||= Wait.new self
end

#xlsxObject



5
6
7
# File 'lib/webdrone/xlsx.rb', line 5

def xlsx
  @xlsx ||= Xlsx.new self
end