Class: BrowserLoader::Factory
- Inherits:
-
Object
- Object
- BrowserLoader::Factory
- Defined in:
- lib/browser_loader/factory.rb
Class Method Summary collapse
-
.browser_timeout ⇒ Object
Timeout period for browser.
- .browser_timeout=(timeout) ⇒ Object
-
.build ⇒ Object
Configure and return a browser object.
- .client ⇒ Object
- .client=(new_client) ⇒ Object
-
.disk_cache_dir ⇒ Object
Directory to store cache data in Defaults to test/cache-data.
- .disk_cache_dir=(dir) ⇒ Object
- .download_dir ⇒ Object
-
.download_dir=(dir) ⇒ Object
Set the download directory the browser will use.
- .log_level ⇒ Object
-
.log_level=(level) ⇒ Object
Set the browser logging level.
-
.reset_switches ⇒ Object
Clear out all switches so they can be reconfigured.
-
.switches ⇒ Object
Switches used to configure the chrome/chromium browser.
-
.user_data_dir ⇒ Object
Directory to store profile data in Defaults to test/chrome-data.
- .user_data_dir=(dir) ⇒ Object
Class Method Details
.browser_timeout ⇒ Object
Timeout period for browser. Defaults to 60 seconds
47 48 49 |
# File 'lib/browser_loader/factory.rb', line 47 def self.browser_timeout @@browser_timeout ||= 60 end |
.browser_timeout=(timeout) ⇒ Object
51 52 53 |
# File 'lib/browser_loader/factory.rb', line 51 def self.browser_timeout= timeout @@browser_timeout = timeout end |
.build ⇒ Object
Configure and return a browser object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/browser_loader/factory.rb', line 206 def self.build # We must clear out any environmental proxy or Selenium fails # to connect to the local application. ENV['http_proxy'] = nil # No configuration is done if IE or Firefox browser is specified. env_browser = ENV['BROWSER'] if env_browser if env_browser == "ie" return Watir::Browser.new :ie end if env_browser == "ff" return Watir::Browser.new :firefox end end # Specify chrome browser capabilities. caps = Selenium::WebDriver::Remote::Capabilities.chrome caps['chromeOptions'] = {'binary' => chromium_exe } unless download_dir.empty? prefs = {'download.default_directory' => download_dir } #caps['chromeOptions']['profile.download.prompt_for_download'] = false #caps['chromeOptions']['download.default_directory'] = download_dir caps['chromeOptions']['prefs' => prefs] end # Set the browser timeout. Default is 60 seconds. client.timeout = browser_timeout browser = Watir::Browser.new :chrome, :switches => switches, :http_client => client, :service_log_path => user_data_dir + '/chromedriver.out', :desired_capabilities => caps end |
.client ⇒ Object
32 33 34 35 36 |
# File 'lib/browser_loader/factory.rb', line 32 def self.client # Client is explicitly instantiated so we can adjust the timeout period # or replace with a mock for testing. @@client ||= Selenium::WebDriver::Remote::Http::Default.new end |
.client=(new_client) ⇒ Object
38 39 40 |
# File 'lib/browser_loader/factory.rb', line 38 def self.client= new_client @@client = new_client end |
.disk_cache_dir ⇒ Object
Directory to store cache data in Defaults to test/cache-data.
Directory will be deleted and recreated to ensure a clean cache. Note thata the path is a relative path. It will be created relative to the current working directory.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/browser_loader/factory.rb', line 96 def self.disk_cache_dir @@disk_cache_dir ||= "test/cache-data" # Store chrome cache at test/cache-data. # We will wipe out this directory on each start to keep a clean cache. @@disk_cache_dir = File.(@@disk_cache_dir) # Delete the cache dir if it exists, then recreate it. if File.exists?(@@disk_cache_dir) and File.directory?(@@disk_cache_dir) FileUtils.rm_rf @@disk_cache_dir end FileUtils.makedirs @@disk_cache_dir @@disk_cache_dir end |
.disk_cache_dir=(dir) ⇒ Object
111 112 113 |
# File 'lib/browser_loader/factory.rb', line 111 def self.disk_cache_dir= dir @@disk_cache_dir = dir end |
.download_dir ⇒ Object
185 186 187 |
# File 'lib/browser_loader/factory.rb', line 185 def self.download_dir @@download_dir ||= "" end |
.download_dir=(dir) ⇒ Object
Set the download directory the browser will use
NOTE: This is not currently working as of chromedriver v2.20 Until this works in chromedriver, the download_dir value should be set to match the default download directory so things work as expected.
197 198 199 200 |
# File 'lib/browser_loader/factory.rb', line 197 def self.download_dir= dir @@download_dir = dir @@download_dir.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows? end |
.log_level ⇒ Object
115 116 117 |
# File 'lib/browser_loader/factory.rb', line 115 def self.log_level @@log_level ||= 0 end |
.log_level=(level) ⇒ Object
Set the browser logging level
Sets the minimum log level. Valid values are from 0 to 3:
INFO = 0
WARNING = 1
LOG_ERROR = 2
LOG_FATAL = 3
130 131 132 |
# File 'lib/browser_loader/factory.rb', line 130 def self.log_level= level @@log_level = level end |
.reset_switches ⇒ Object
Clear out all switches so they can be reconfigured
181 182 183 |
# File 'lib/browser_loader/factory.rb', line 181 def self.reset_switches @@switches = Array.new end |
.switches ⇒ Object
Switches used to configure the chrome/chromium browser
To modify/add switches:
Factory.switches << "--some-other-switch=#{data}"
See peter.sh/experiments/chromium-command-line-switches/ for a list of available switches. See sites.google.com/a/chromium.org/chromedriver/capabilities for details on setting ChromeDriver caps.
If BROWSER_PROXY_PORT environment variable is set to a port number, the –proxy-server switch will be added.
If you intend to override the user-data-dir, cache-data-dir or logging level, do so before calling this method.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/browser_loader/factory.rb', line 150 def self.switches @@switches ||= Array.new if @@switches.empty? # Default switches: # ignore-certificate-errors: Ignores certificate-related errors. # disable-popup-blocking: Disable pop-up blocking. # disable-translate: Allows disabling of translate from # the command line to assist with # automated browser testing. # no-first-run: Skip First Run tasks, whether or not # it's actually the First Run. @@switches = %w[--ignore-certificate-errors --disable-popup-blocking --disable-translate --no-first-run] @@switches << "--log-level=#{log_level}" @@switches << "--user-data-dir=#{user_data_dir}" @@switches << "--disk-cache-dir=#{disk_cache_dir}" proxy_port = ENV['BROWSER_PROXY_PORT'] if proxy_port && ! proxy_port.empty? proxy_connection_string = "socks://localhost:#{proxy_port}" @@switches << "--proxy-server=#{proxy_connection_string}" end end @@switches end |
.user_data_dir ⇒ Object
Directory to store profile data in Defaults to test/chrome-data.
Directory will be created if it doesn’t exist. Note that it is a relative path, so it is created relative to the current working directory.
NOTE: The only way I’ve found to stop the EULA from being displayed is to use the user-data-dir switch and point to a dir where chrome can put the data indicating it (EULA) has already been accepted.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/browser_loader/factory.rb', line 67 def self.user_data_dir @@user_data_dir ||= "test/chrome-data" # user_data_dir must be expanded to a full (absolute) path. A relative path # results in chromedriver failing to start. @@user_data_dir = File.(@@user_data_dir) #puts "*** user_data_dir location: #{@@user_data_dir}" # Create the data dir if it doesn't exist (or chromedriver fails to start). unless File.exists?(@@user_data_dir) and File.directory?(@@user_data_dir) FileUtils.makedirs @@user_data_dir end @@user_data_dir end |
.user_data_dir=(dir) ⇒ Object
83 84 85 |
# File 'lib/browser_loader/factory.rb', line 83 def self.user_data_dir= dir @@user_data_dir = dir end |