Handle Basic authentication in Selenium for Chrome browser

Chrome extension

Create a simple chrome extension with custom username and password defined in it.

  • Create a .js file, background.js with the custom username and password
var username = "your_user";
var password = "your_pass";
var retry = 3;

chrome.webRequest.onAuthRequired.addListener(
  function handler(details) {
    if (--retry < 0)
      return {cancel: true};
    return {authCredentials: {username: username, password: password}};
  },
  {urls: ["<all_urls>"]},
  ['blocking']
);

[Text in red has to be customized specific to the project]

  • Create a .json file, manifest.json as shown below
{
  "manifest_version": 2,
  "name": "Authentication for ...",
  "version": "1.0.0",
  "permissions": ["<all_urls>", "webRequest", "webRequestBlocking"],
  "background": {
    "scripts": ["background.js"]
  }
}
  • Now, zip both the files and rename it to extension.crx

  • To check if the custom extension works fine, open a new tab in Google-Chrome and enter chrome://extensions/
  • Now, select the folder where both the manifest.json and background.js files were located (on clicking Load unpacked extension…)

  • Once the extension is loaded, you will observe the newly added icon next to the browser’s address bar; now, try loading the web application that asks you for the basic auth

Chrome options

Configure the newly created .crx chrome extension through ChromeOptions as described below

  • Ruby snippet to configure chrome extension through ChromeOptions
options = {
  'chromeOptions' => {
    'extensions' => [
      Base64.strict_encode64(File.open("../extension.crx", 'rb').read)
    ]
  }
}

caps = Selenium::WebDriver::Remote::Capabilities.chrome(options)
@driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps
  • The above snippet works fine in GUI mode; however, through browser’s headless mode, it doesn’t
  • Linux users, try xvfb to overcome this issue; for Ruby, you need a third party client/lib, headless to couple with Linux’s xvfb mode
gem 'headless'
require 'headless'
  • Start headless support before initiating the tests
@headless = Headless.new
@headless.start
  • And terminate the headless mode at exit
@headless.destroy
  • In Jenkins, you need to set the environment ready for xvfb to run tests without GUI mode
sudo yum install xorg-x11-server-Xvfb -y
(or)
sudo apt-get install xorg-x11-server-Xvfb -y
export DISPLAY=:0
  • Meanwhile, go to Manage Jenkins > Configure System; add manual export for DISPLAY :0 in the global properties itself (this step is optional)

  • Force kill Xvfb if previously running
pkill -f Xvfb || true
  • To set headless browser window size, you can still use selenium-webdriver’s native way
target_size = Selenium::WebDriver::Dimension.new(1600, 1268)
@driver.manage.window.size = target_size

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s