Is it possible to add a plugin to chromedriver under a protractor test?

别来无恙 提交于 2019-11-28 08:49:22

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:

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]
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!