Skip to content

Commit

Permalink
rb - fix Chrome Profile implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 18, 2016
1 parent 8973aee commit 5ff3d29
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 37 deletions.
17 changes: 9 additions & 8 deletions rb/lib/selenium/webdriver/chrome/bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def quit

def create_capabilities(opts)
caps = opts.delete(:desired_capabilities) { Remote::Capabilities.chrome }
args = opts.delete(:args) || opts.delete(:switches)
args = opts.delete(:args) || []
profile = opts.delete(:profile)
detach = opts.delete(:detach)
proxy = opts.delete(:proxy)
Expand All @@ -84,14 +84,15 @@ def create_capabilities(opts)

chrome_options = caps['chromeOptions'] || {}

if args
unless args.is_a? Array
raise ArgumentError, ':args must be an Array of Strings'
end
chrome_options['args'] = args.map(&:to_s)
unless args.is_a? Array
raise ArgumentError, ':args must be an Array of Strings'
end

chrome_options['extensions'] = profile.as_json['extensions'] if profile
chrome_options['args'] = args.map(&:to_s)
profile = profile.as_json if profile
if profile && chrome_options['args'].none? { |arg| arg =~ /user-data-dir/}
chrome_options['args'] << "--user-data-dir=#{profile[:directory]}"
end
chrome_options['extensions'] = profile[:extensions] if profile && profile[:extensions]
chrome_options['binary'] = Chrome.path if Chrome.path
chrome_options['detach'] = true if detach
chrome_options['prefs'] = prefs if prefs
Expand Down
19 changes: 12 additions & 7 deletions rb/lib/selenium/webdriver/chrome/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ module Chrome
class Profile
include ProfileHelper

attr_reader :directory

def initialize(model = nil)
@model = verify_model(model)
@model = verify_model(model)
@extensions = []
@encoded_extensions = []
end
Expand Down Expand Up @@ -62,22 +64,25 @@ def [](key)
end

def layout_on_disk
dir = @model ? create_tmp_copy(@model) : Dir.mktmpdir('webdriver-chrome-profile')
FileReaper << dir
@directory = @model ? create_tmp_copy(@model) : Dir.mktmpdir('webdriver-chrome-profile')
FileReaper << @directory

write_prefs_to dir
write_prefs_to @directory

dir
@directory
end

def as_json(opts = nil)
def as_json(*)
extensions = @extensions.map do |crx_path|
File.open(crx_path, 'rb') { |crx_file| Base64.strict_encode64 crx_file.read }
end

extensions.concat(@encoded_extensions)

super.merge('extensions' => extensions)
opts = {directory: @directory || layout_on_disk}
opts[:extensions] = extensions if extensions
opts[:zip] = Zipper.zip(@directory)
opts
end

private
Expand Down
32 changes: 19 additions & 13 deletions rb/spec/integration/selenium/webdriver/chrome/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@ module Chrome
describe Profile do
let(:profile) { Profile.new }

# Won't work on ChromeDriver 2.0
#
# it "launches Chrome with a custom profile" do
# profile['autofill.disabled'] = true
#
# begin
# driver = WebDriver.for :chrome, :profile => profile
# ensure
# driver.quit if driver
# end
# end
it 'can be manually verified without mocks' do
# Example for command line in mac to create a new data directory
# /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir=/path/to/data
# directory = File.expand_path('../', __FILE__)
# profile = Profile.new(directory)
profile = Profile.new
profile['browser.show_home_button'] = true
file = File.expand_path('../sample.crx', __FILE__)
profile.add_extension(file)

driver = Selenium::WebDriver.for :chrome, profile: profile
driver.navigate.to url_for('xhtmlTest.html')
expect('verify manually - home button displayed')
expect('verify manually - make page red extension properly installed')
driver.quit
end

it 'should be serializable to JSON' do
profile['foo.boolean'] = true
Expand Down Expand Up @@ -65,8 +70,9 @@ module Chrome
expect(profile).to receive(:layout_on_disk).and_return 'ignored'
expect(Zipper).to receive(:zip).and_return 'ignored'

expect(profile.as_json).to eq('zip' => 'ignored',
'extensions' => [Base64.strict_encode64('test')])
expect(profile.as_json).to eq(zip: 'ignored',
directory: 'ignored',
extensions: [Base64.strict_encode64('test')])
end

it "raises an error if the extension doesn't exist" do
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ def create_chrome_driver
args = ENV['TRAVIS'] ? ['--no-sandbox'] : []

WebDriver::Driver.for :chrome,
native_events: native_events?,
args: args
end

Expand Down
10 changes: 2 additions & 8 deletions rb/spec/unit/selenium/webdriver/chrome/bridge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ module Chrome
Bridge.new(http_client: http, args: %w[--foo=bar])

expect(caps['chromeOptions']['args']).to eq(%w[--foo=bar])
expect(caps['chrome.switches']).to eq(%w[--foo=bar])
end

it 'sets the proxy capabilitiy' do
Expand All @@ -61,14 +60,12 @@ module Chrome
Bridge.new(http_client: http, prefs: {foo: 'bar'})

expect(caps['chromeOptions']['prefs']).to eq(foo: 'bar')
expect(caps['chrome.prefs']).to eq(foo: 'bar')
end

it 'lets the user override chrome.detach' do
Bridge.new(http_client: http, detach: true)

expect(caps['chromeOptions']['detach']).to be true
expect(caps['chrome.detach']).to be true
end

it 'uses the user-provided server URL if given' do
Expand All @@ -91,11 +88,8 @@ module Chrome
Bridge.new(http_client: http, profile: profile)

profile_data = profile.as_json
expect(caps['chromeOptions']['profile']).to eq(profile_data['zip'])
expect(caps['chromeOptions']['extensions']).to eq(profile_data['extensions'])

expect(caps['chrome.profile']).to eq(profile_data['zip'])
expect(caps['chrome.extensions']).to eq(profile_data['extensions'])
expect(caps['chromeOptions']['args'].first).to include(profile_data[:directory])
expect(caps['chromeOptions']['extensions']).to eq(profile_data[:extensions])
end

it 'takes desired capabilities' do
Expand Down

0 comments on commit 5ff3d29

Please sign in to comment.