Class: CapybaraBox::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara-box/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters = {}) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
# File 'lib/capybara-box/base.rb', line 5

def initialize(parameters = {})
  @browser       = parameters.fetch(:browser) { :selenium_chrome }
  @max_wait_time = parameters[:max_wait_time]
  @parameters    = parameters
end

Class Method Details

.configure(parameters) ⇒ Object



181
182
183
# File 'lib/capybara-box/base.rb', line 181

def self.configure(parameters)
  new(parameters).tap(&:create)
end

Instance Method Details

#add_argument(value) ⇒ Object



11
12
13
# File 'lib/capybara-box/base.rb', line 11

def add_argument(value)
  options&.add_argument(value)
end

#add_preference(key, value) ⇒ Object



15
16
17
# File 'lib/capybara-box/base.rb', line 15

def add_preference(key, value)
  options&.add_preference(key, value)
end

#apply_argumentsObject



19
20
21
22
23
24
25
26
27
# File 'lib/capybara-box/base.rb', line 19

def apply_arguments
  arguments.each { |argument| add_argument(argument) }

  if chrome_headless?
    add_argument('--headless')
    add_argument('--no-sandbox')
    add_argument('--disable-gpu')
  end
end

#apply_bin_path(path) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/capybara-box/base.rb', line 92

def apply_bin_path(path)
  if firefox?
    ::Selenium::WebDriver::Firefox::Binary.path = path

    ::Selenium::WebDriver::Firefox::Binary.path
  end
end

#apply_preferencesObject



29
30
31
# File 'lib/capybara-box/base.rb', line 29

def apply_preferences
  preferences.each { |key, value| add_preference(key, value) }
end

#apply_version(version) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/capybara-box/base.rb', line 33

def apply_version(version)
  if chrome_family?
    Webdrivers::Chromedriver.required_version = version
  else
    Webdrivers::Geckodriver.required_version = version
  end
end

#argumentsObject



41
42
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
# File 'lib/capybara-box/base.rb', line 41

def arguments
  return @parameters[:arguments] if @parameters[:arguments]
  return [] unless chrome_family?

  %w[
    --disable-background-networking
    --disable-default-apps
    --disable-dev-shm-usage
    --disable-extensions
    --disable-infobars
    --disable-notifications
    --disable-password-generation
    --disable-password-manager-reauthentication
    --disable-password-separated-signin-flow
    --disable-popup-blocking
    --disable-save-password-bubble
    --disable-site-isolation-trials
    --disable-sync
    --disable-translate
    --hide-scrollbars
    --incognito
    --metrics-recording-only
    --mute-audio
    --no-default-browser-check
    --no-first-run
    --remote-debugging-address=0.0.0.0
    --remote-debugging-port=9222
    --safebrowsing-disable-auto-update
    --start-fullscreen
    --user-data-dir=/tmp
    --window-size=1920,1080
  ]
end

#chrome?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/capybara-box/base.rb', line 75

def chrome?
  @browser == :selenium_chrome
end

#chrome_family?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/capybara-box/base.rb', line 79

def chrome_family?
  chrome? || chrome_headless?
end

#chrome_headless?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/capybara-box/base.rb', line 83

def chrome_headless?
  @browser == :selenium_chrome_headless
end

#configure_capybaraObject



87
88
89
90
# File 'lib/capybara-box/base.rb', line 87

def configure_capybara
  Capybara.javascript_driver     = @browser
  Capybara.default_max_wait_time = @max_wait_time if @max_wait_time
end

#createObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/capybara-box/base.rb', line 100

def create
  apply_arguments
  apply_preferences

  apply_bin_path(@parameters[:bin_path]) if @parameters[:bin_path]
  apply_version(@parameters[:version])   if !!@parameters[:version]

  ::CapybaraBox::Screenshot.configure(@parameters[:screenshot], @browser) if @parameters[:screenshot]

  register(@browser)

  configure_capybara
end

#driver(app) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/capybara-box/base.rb', line 114

def driver(app)
  opts           = {}
  opts[:options] = options if chrome_family?

  Capybara::Selenium::Driver.load_selenium

  opts[:http_client] = http_client if ::CapybaraBox::Helper.true?(ENV['CI'])

  Capybara::Selenium::Driver.new(app, opts.merge(driver_options))
end

#driver_optionsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/capybara-box/base.rb', line 125

def driver_options
  return @parameters[:driver_options] if @parameters[:driver_options]

  opts = {
    browser: chrome_family? ? :chrome : @browser,
    clear_local_storage: true,
    clear_session_storage: true,
  }

  if log? && chrome_family?
    opts[:service] = ::Selenium::WebDriver::Service.chrome(
      args: { log_path: 'log/capybara-box.log', verbose: true }
    )
  end

  opts
end

#firefox?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/capybara-box/base.rb', line 143

def firefox?
  @browser == :firefox
end

#http_clientObject



156
157
158
# File 'lib/capybara-box/base.rb', line 156

def http_client
  @http_client ||= ::Selenium::WebDriver::Remote::Http::Default.new(**http_client_options)
end

#http_client_optionsObject



147
148
149
150
151
152
153
154
# File 'lib/capybara-box/base.rb', line 147

def http_client_options
  return @parameters[:http_client_options] if @parameters[:http_client_options]

  {
    open_timeout: nil,
    read_timeout: 120,
  }
end

#optionsObject



160
161
162
# File 'lib/capybara-box/base.rb', line 160

def options
  @options ||= ::Selenium::WebDriver::Chrome::Options.new if chrome_family?
end

#preferencesObject



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/capybara-box/base.rb', line 164

def preferences
  return @parameters[:preferences] if @parameters[:preferences]
  return {} unless chrome_family?

  {
    credentials_enable_service: false,

    profile: {
      password_manager_enabled: false,
    },
  }
end

#register(name) ⇒ Object



177
178
179
# File 'lib/capybara-box/base.rb', line 177

def register(name)
  Capybara.register_driver(name.to_sym) { |app| driver(app) }
end