5

I've been trying to handle the basic authentication during my protractor test. Some hard time on it, so i've found a chrome plugin wich sends automatically my credentials for websites that require basic authentication.

As each time that chromedriver is executed, a new profile is loaded, how can i permanelty add a plugin to my tests? I know that there is https://sites.google.com/a/chromium.org/chromedriver/extensions, but i dont think this very clear.

andrepm
  • 867
  • 4
  • 13
  • 31

3 Answers3

7

You need to configure extensions list inside chromeOptions:

capabilities {
    'browserName': 'chrome',
    'chromeOptions': {
        'extensions': ['base64 encoded extension']
    }
}

Note that it in extensions, it is important to provide a list of base-64 encoded packed Chrome extension.

To get a base64 encoded extension, you need to read the .ctx extension file and encode the contents with base64. For example, using python:

>>> import base64
>>> data = open('path_to_the_ctx_extension').read()
>>> base64.standard_b64encode(data).decode('UTF-8')
# outputs the encoded chrome extension which you can paste in the config

Or, easier, make a helper.js file using fs and q:

var q = require('q');
var fs = require('fs');

exports.getCapabilities = function (filename) {
    var deferred = q.defer();

    fs.readFile(filename, function (err, data) {
        var capabilities = {
            'browserName': 'chrome',
            'chromeOptions': {
                extensions: [
                    data.toString('base64')
                ]
            }
        };
        deferred.resolve(capabilities);
    });

    return deferred.promise;
};

Then, in your protractor config, use this getCapabilities() function to get capabilities:

var helper = require('./helper.js');

exports.config = {

    capabilities: helper.getCapabilities('/path/to/crx/extension'),

    ...
}

Currently, it works with a single extension, so there is a room for improvement.

Also, look through the following issue in case you have problems:

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks, it was very helpful! But i'm having an issue with this solution. As i set the path to my desired .crx and run my test, it fails, and no error message is being displayed. Just `Process finished with exit code 100`. I also read the thread you suggested me, but haven't found a solution for my case. – andrepm Dec 03 '14 at 18:35
  • It doesnt provide a stacktrace. This is pretty much everything: `"C:\Program Files (x86)\JetBrains\WebStorm 9.0.1\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" node_modules\protractor\lib\cli.js C:\Users\QARJ\WebstormProjects\ProtractorPiloto\config.js Starting selenium standalone server... Selenium standalone server started at http://192.168.44.130:61384/wd/hub Process finished with exit code 100` – andrepm Dec 03 '14 at 19:11
  • @andrepm and what if you omit the `extensions`? Is it running? Thanks. – alecxe Dec 03 '14 at 19:32
  • Yes, i tried to comment `extensions` and the extension path itself. In both ways, my test run normally. – andrepm Dec 03 '14 at 19:33
  • @andrepm interesting, could you give the name of the extension? I'll try to reproduce the problem. Thank you. – alecxe Dec 03 '14 at 19:35
  • @alecxe I tried it; getting this error in console `[14:34:20] E/launcher - Error: TypeError: Target browser must be a string, but is ; did you forget to call forBrowser()? at TypeError (native)` – Prashanth Sams Sep 21 '16 at 09:06
  • So once you add the extension to chrome options. How do you interact with the extension? – Monnie_tester Mar 12 '19 at 05:18
7

Check this: https://github.com/andresdominguez/elementor/blob/master/bin/elementexplorer.js#L194

Here I am loading an extension from a local directory. The extension is not a crx file, but the uncompressed version.

'chromeOptions': {
  'args': ['--load-extension=' + extensionPath]
}
Andres D
  • 8,910
  • 2
  • 26
  • 31
  • It didnt worked for me. It is like this? `'chromeOptions': { 'args': ['--load-extension=' + 'C:/Users/QARJ/AppData/Local/Google/Chrome/User Data/Default/Extensions/dgpgkkfheijbcgjklcbnokoleebmeokn/1.1_0'] }` – andrepm Dec 04 '14 at 12:58
  • 1
    Maybe the space in User Data is causing you trouble. Can you copy the extension to C:/tmp to test? – Andres D Dec 04 '14 at 16:01
  • 1
    I'm glad to hear that. Happy testing! Also, check out my elementor tool: https://www.npmjs.org/package/elementor I am looking for feedback – Andres D Dec 04 '14 at 17:23
  • Hi @andrepm, so this is the working solution? Out of curiosity, why is the other answer marked as the accepted answer? – Leo Gallucci Dec 22 '14 at 14:18
  • Hi, both solutions are working. The other one is marked just because it was the first one. – andrepm Dec 23 '14 at 22:41
  • @AndresD Facing issues; can you please take a look here. Thanks! http://stackoverflow.com/questions/39605385/unable-to-load-chrome-extension-in-protractor-tests – Prashanth Sams Sep 21 '16 at 09:14
  • Link is dead? https://github.com/andresdominguez/elementor/blob/master/bin/elementexplorer.js – ken lacoste Aug 03 '18 at 04:25
  • Is it possible to include more than one extension using this method? – zero_cool Apr 27 '19 at 20:50
4

Instead of committing the extension with your code and having to load it from disk when you run the tests you might want to consider using the authenticator-browser-extension Node module I have recently open-sourced.

To use the module install it from npm:

npm install --save-dev authenticator-browser-extension

And import in the protractor.conf.js:

const { Authenticator } = require('authenticator-browser-extension');

exports.config = {
    capabilities: {
        browserName: 'chrome',

        chromeOptions: {
            extensions: [
                Authenticator.for('username', 'password').asBase64()
            ]
        }
    },
}

Pro tip: remember not to commit your credentials with your code, consider using env variables instead.

Hope this helps!

Jan

Jan Molak
  • 4,426
  • 2
  • 36
  • 32