Reputation: 6064
I know how to create profile for Firefox
require 'watir'
options = Selenium::WebDriver::Firefox::Options.new
options.profile = "default"
@driver = Selenium::WebDriver.for :firefox, options: options
@b = Watir::Browser.new @driver
But when I do same thing for Chrome it's not creating, Infact I realized that options(please look above) object doesn't even have the method profile=
so I try adding profile like as given below(I saw how people are creating in Java Selenium Binding so I have done the same but it's not working here)
options = Selenium::WebDriver::Firefox::Options.new
options.add_argument('user-data-dir=C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default')
@driver = Selenium::WebDriver.for :chrome, options: options
Can someone help me how to create Chrome Profile via Ruby Selenium binding(or WATIR)?
Upvotes: 0
Views: 2591
Reputation: 325
Since you asked in the comments for a more detailed explanation for Capybara, I post it as an answer (although you seem to already have a working solution now - sorry for the delayed answer).
In my rails projects I usually configure the Selenium chrome driver as follows:
gem 'chromedriver-helper'
in the Gemfile (or install it locally). Then in a system-test initializer define
Capybara.register_driver :selenium_chrome_headless_no_sandbox do |app|
browser_options = ::Selenium::WebDriver::Chrome::Options.new
browser_options.args << '--headless'
browser_options.args << '--disable-gpu'
browser_options.args << '--no-sandbox'
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end
and later (configuring RSpec) I set it as the used driver like:
RSpec.configure do |config|
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless_no_sandbox
end
end
Maybe this helps someone. Cheers
edit: added chromedriver-helper
Upvotes: 0
Reputation: 46846
Using an existing or creating a new profile can be done via Chromedrivers user-data-dir argument. In Watir, you can pass the argument via the :args
parameter:
browser = Watir::Browser.new :chrome,
args: ['user-data-dir=C:\Users\user_name\AppData\Local\Google\Chrome\User Data']
Note that if you trying to use the existing default profile, you do not want to include the "Default" directory in the path.
Upvotes: 1
Reputation: 13
I made a function for creating new browser. You can use it.
def new_browser
if Rails.env.production?
chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
Selenium::WebDriver::Chrome.path = "/app/.apt/usr/bin/google-chrome"
Selenium::WebDriver::Chrome.driver_path = "/app/vendor/bundle/bin/chromedriver"
end
profile = Selenium::WebDriver::Chrome::Profile.new
profile['general.useragent.override'] = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'
driver = Selenium::WebDriver.for :chrome, :profile => profile
pid = driver.instance_variable_get(:@service).instance_variable_get(:@process).instance_variable_get(:@pid)
begin
browser = Watir::Browser.new driver
rescue => e
system("kill -9 #{@pid}")
end
return {:browser => browser , :pid => pid}
end
Upvotes: 0