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



173
174
175
# File 'lib/capybara-box/base.rb', line 173

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



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

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

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

#apply_preferencesObject



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

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

#apply_version(version) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/capybara-box/base.rb', line 41

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

#argumentsObject



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

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
    --safebrowsing-disable-auto-update
    --start-fullscreen
    --window-size=1920,1080
  ]
end

#chrome?Boolean

Returns:

  • (Boolean)


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

def chrome?
  @browser == :selenium_chrome
end

#chrome_family?Boolean

Returns:

  • (Boolean)


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

def chrome_family?
  chrome? || chrome_headless?
end

#chrome_headless?Boolean

Returns:

  • (Boolean)


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

def chrome_headless?
  @browser == :selenium_chrome_headless
end

#configure_capybaraObject



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

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

#createObject



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

def create
  apply_arguments
  apply_preferences

  apply_bin_path(@parameters[:bin_path]) if ::CapybaraBox::Helper.present?(@parameters[:bin_path])
  apply_version(@parameters[:version])   if ::CapybaraBox::Helper.present?(@parameters[:version])

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

  register(@browser)

  configure_capybara
  configure_logger(@parameters[:logger]) if logger?
end

#driver(app) ⇒ Object



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

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



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/capybara-box/base.rb', line 123

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,
  }

  opts
end

#firefox?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/capybara-box/base.rb', line 135

def firefox?
  @browser == :firefox
end

#http_clientObject



148
149
150
# File 'lib/capybara-box/base.rb', line 148

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

#http_client_optionsObject



139
140
141
142
143
144
145
146
# File 'lib/capybara-box/base.rb', line 139

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

  {
    open_timeout: nil,
    read_timeout: 120,
  }
end

#optionsObject



152
153
154
# File 'lib/capybara-box/base.rb', line 152

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

#preferencesObject



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/capybara-box/base.rb', line 156

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



169
170
171
# File 'lib/capybara-box/base.rb', line 169

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