Class: CapybaraBox::Base
- Inherits:
-
Object
- Object
- CapybaraBox::Base
- Defined in:
- lib/capybara-box/base.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_argument(value) ⇒ Object
- #add_preference(key, value) ⇒ Object
- #apply_arguments ⇒ Object
- #apply_bin_path(path) ⇒ Object
- #apply_preferences ⇒ Object
- #apply_version(version) ⇒ Object
- #arguments ⇒ Object
- #chrome? ⇒ Boolean
- #chrome_family? ⇒ Boolean
- #chrome_headless? ⇒ Boolean
- #configure_capybara ⇒ Object
- #create ⇒ Object
- #driver(app) ⇒ Object
- #driver_options ⇒ Object
- #firefox? ⇒ Boolean
- #http_client ⇒ Object
- #http_client_options ⇒ Object
-
#initialize(parameters = {}) ⇒ Base
constructor
A new instance of Base.
- #options ⇒ Object
- #preferences ⇒ Object
- #register(name) ⇒ Object
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) &.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) &.add_preference(key, value) end |
#apply_arguments ⇒ Object
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_preferences ⇒ Object
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 |
#arguments ⇒ Object
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
80 81 82 |
# File 'lib/capybara-box/base.rb', line 80 def chrome? @browser == :selenium_chrome end |
#chrome_family? ⇒ Boolean
84 85 86 |
# File 'lib/capybara-box/base.rb', line 84 def chrome_family? chrome? || chrome_headless? end |
#chrome_headless? ⇒ Boolean
88 89 90 |
# File 'lib/capybara-box/base.rb', line 88 def chrome_headless? @browser == :selenium_chrome_headless end |
#configure_capybara ⇒ Object
92 93 94 95 |
# File 'lib/capybara-box/base.rb', line 92 def Capybara.javascript_driver = @browser Capybara.default_max_wait_time = @max_wait_time if @max_wait_time end |
#create ⇒ Object
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_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] = 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()) end |
#driver_options ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/capybara-box/base.rb', line 123 def 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
135 136 137 |
# File 'lib/capybara-box/base.rb', line 135 def firefox? @browser == :firefox end |
#http_client ⇒ Object
148 149 150 |
# File 'lib/capybara-box/base.rb', line 148 def http_client @http_client ||= ::Selenium::WebDriver::Remote::Http::Default.new(**) end |
#http_client_options ⇒ Object
139 140 141 142 143 144 145 146 |
# File 'lib/capybara-box/base.rb', line 139 def return @parameters[:http_client_options] if @parameters[:http_client_options] { open_timeout: nil, read_timeout: 120, } end |
#options ⇒ Object
152 153 154 |
# File 'lib/capybara-box/base.rb', line 152 def @options ||= ::Selenium::WebDriver::Chrome::Options.new if chrome_family? end |
#preferences ⇒ Object
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 |