Search code examples
google-chromewebdriverwatirmultipass

How to configure 'watir' to use an existing chrome user profile (created with chrome.exe --user-data-dir)


Is it possible to configure watir webdriver use an existing chrome user/profile

(created by chrome.exe --user-data-dir=C:\MyChromeUserProfile)

In Firefox it's possible to do the following:
(created a user profile with firefox -P)
profile = Selenium::WebDriver::Firefox::Profile.new(c://MyFFUserProfile) Watir::Browser.new :ff, :profile => profile

For Chrome, I tried the following code to no avail:
Watir::Browser.new :chrome, :switches => %w['--user-data-dir=c://MyChromeUserProfile']

While this opens a chrome session, it does not use the user's profile settings (Specifically an extension that was installed and configured, like Multi-pass for HTTP basic authentication).

By the way this is a similar workaround but for chrome that I am trying to implement like the one listed for Firefox and auto auth posted on http://watirwebdriver.com/basic-browser-authentication/)


Solution

  • It's been a while since this question was asked but it is the top ranked Google result so I'll answer it here with the newest release of both Watir (6.0.2) and Webdriver (3.0.1). This works for me on Mac and Windows:

    require 'watir'
    
    switches = %W[--user-data-dir=c:\\chrome]
    # switches = %W[--user-data-dir=/chrome]  => Linux or Mac
    
    
    prefs = {
      :download => {
        :prompt_for_download => false,
        :default_directory => "c:\\downloads"
      }
    }
    
    browser = Watir::Browser.new :chrome, :prefs => prefs, switches: switches
    
    browser.goto 'google.com'
    browser.text_field(title: 'Search').set 'Hello World!'
    browser.button(type: 'submit').click
    
    sleep 10
    puts browser.title
    browser.close