Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...