CodePull
CodePull

Reputation: 290

Automate Chrome Extensions With Selenium and Ruby

I am currently working on an automation project where I need to use Ruby/Selenium to discover specific http headers returned to the user after authentication to a web app. I am able to automate the web app just fine; however, when I try to use a Chrome extension the browser returns the following error:

The webpage at chrome-extension://[extension address] might be temporarily down or it may have moved permanently to a new web address.

After looking into this, it appears that the Selenium web driver is using a different Chrome profile than my regular Chrome profile. As such, I was wondering if someone knew if there is a way to to tell Selenium to use my regular Chrome profile with the extension loaded or build a new profile and install the extension during runtime.

So far, most of the answers I have found were centralized around Python and Java. Please let me know if I can provide more information.

Upvotes: 2

Views: 1905

Answers (1)

Florent B.
Florent B.

Reputation: 42528

To launch Chrome with the default profile on Windows:

require 'selenium-webdriver'

switches = ['user-data-dir='+ENV['LOCALAPPDATA']+'\\Google\\Chrome\\User Data']
driver = Selenium::WebDriver.for :chrome, :switches => switches

driver.navigate.to "https://www.google.co.uk"

Or to add an extension to the created profile:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome, 
  :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome({
    'chromeOptions' => {
      'extensions' => [
        Base64.strict_encode64(File.open('C:\\App\\extension.crx', 'rb').read)
      ]
    }
  })

driver.navigate.to "https://www.google.co.uk"

Upvotes: 3

Related Questions