Learn how to add extensions to remote Chrome browsers in BrowserStack Automate.
You can add extensions to remote Chrome browsers in BrowserStack Automate.
End-users extend browser functionalities with plugins / extensions. For some scenarios, you may need to verify the following:
How your web pages look or behave on browsers with 3rd party plugins / extensions.
How your plugin / extension modifies the appearance or behavior of different webpages.
You can add plugins / extensions to remote browsers in BrowserStack Automate.
This page shows you how to add extensions to remote Chrome browsers. We’ll update it to include Firefox soon.
Prerequisites
First, you’ll need the Chrome extension’s .crx file. To obtain it, follow the steps below:
Find the Extension ID
Open Google Chrome browser on your workstation.
Open URL: chrome://extensions/. This will show you a list of installed extensions.
Tick the Developer Mode checkbox. This will show you the ID of each extension installed on your Chrome browser.
Find the extension you want to add on the remote Chrome browser. Copy its extension ID to Notepad.
Locating Extension-version subdirectory
Find the extensions directory on your workstation.
In the extensions directory, find the subdirectory with the extension’s version number.
Copy the path to this extension-version subdirectory and paste it in the Notepad.
Pack extension
Go back to Google Chrome extensions page and click ‘Pack extension’.
In the dialogue box that pops up, paste the path to the extension-version subdirectory in the “Extension root directory” input field.
Click the ‘Pack extension’ button. This will create a .crx file in the extension-version folder.
Copy the path to the extension-version folder (now containing the .crx file). We’ll need this path to configure our Selenium test.
Adding extensions to remote Chrome browser
This section shows you how to use Selenium’s chromeOptions class to add extensions in our remote Chrome browsers.
Add the code snippet below in your test script. This will instruct the remote ChromeDriver to add the specified extension before executing the test:
/* You need to use the below modules into your NodeJS test before implementing this code.
var chrome = require("selenium-webdriver/chrome");
var fs = require('fs');
*/let file_path ='<path to extension directory>.crx';let buff =newBuffer.from(fs.readFileSync(file_path));let base64data = buff.toString('base64');var capabilities ={'bstack:options':{"os":"Windows","osVersion":"10",},"chromeOptions":{"extensions":[ base64data ]}"browserName":"Chrome","browserVersion":"latest",}var driver =newwebdriver.Builder().usingServer('https://hub-cloud.browserstack.com/wd/hub').withCapabilities(capabilities).build();
/* You need to use the below packages into your C# test before implementing
this code.
using System.IO;
using System.Collections.Generic;
*/IWebDriver driver;Byte[] bytes = File.ReadAllBytes("<path to extension directory>.crx");String crx_file_base64 = Convert.ToBase64String(bytes);String[] files_string_array ={ crx_file_base64 };Dictionary<string,object> chromeOptionsD =newDictionary<string,object>();
chromeOptionsD.Add("extensions", files_string_array);ChromeOptions capabilities =newChromeOptions();
capabilities.BrowserVersion ="latest";Dictionary<string,object> browserstackOptions =newDictionary<string,object>();
browserstackOptions.Add("os","Windows");
browserstackOptions.Add("osVersion","10");
browserstackOptions.Add("browserName","Chrome");
capabilities.AddAdditionalOption("bstack:options", browserstackOptions);
driver =newRemoteWebDriver(newUri("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub/"), capabilities
);
/* Requires PHP Class
You need to use the class "Facebook\WebDriver\Chrome\ChromeOptions" into your PHP test before implementing following code. */$options=newChromeOptions();$options->addExtensions(array('<path to extension directory>.crx',));$caps=array('bstack:options'=>array("os"=>"Windows","osVersion"=>"10","chromeOptions"=>$options),"browserName"=>"Chrome","browserVersion"=>"latest",)$web_driver=RemoteWebDriver::create("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",$caps);
/* You need to use the below modules into your NodeJS test before implementing this code.
var chrome = require("selenium-webdriver/chrome");
var fs = require('fs');
*/let file_path ='<path to extension directory>.crx';let buff =newBuffer.from(fs.readFileSync(file_path));let base64data = buff.toString('base64');var capabilities ={'browserName':'Chrome','os':'Windows','os_version':'10','chromeOptions':{'extensions':[ base64data ]}}var driver =newwebdriver.Builder().usingServer('https://hub-cloud.browserstack.com/wd/hub').withCapabilities(capabilities).build();
/* You need to use the below packages into your C# test before implementing
this code.
using System.IO;
using System.Collections.Generic;
*/IWebDriver driver;Byte[] bytes = File.ReadAllBytes("<path to extension directory>.crx");String crx_file_base64 = Convert.ToBase64String(bytes);String[] files_string_array ={ crx_file_base64 };Dictionary<string,object> chromeOptionsD =newDictionary<string,object>();
chromeOptionsD.Add("extensions", files_string_array);DesiredCapabilities capabilities =newDesiredCapabilities();
capabilities.SetCapability("browserName","Chrome");
capabilities.SetCapability("browserVersion","75.0");
capabilities.SetCapability("chromeOptions", chromeOptionsD);
driver =newRemoteWebDriver(newUri("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub/"), capabilities
);
/* Requires PHP Class
You need to use the class "Facebook\WebDriver\Chrome\ChromeOptions" into your PHP test before implementing following code. */$options=newChromeOptions();$options->addExtensions(array('<path to extension directory>.crx',));$caps=array("browser"=>"Chrome","os"=>"Windows","os_version"=>"10","chromeOptions"=>$options);$web_driver=RemoteWebDriver::create("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",$caps);