是否可以在量角器测试下向 chromedriver 添加插件? [英] Is it possible to add a plugin to chromedriver under a protractor test?
问题描述
我一直在尝试在量角器测试期间处理基本身份验证.遇到了一些困难,所以我找到了一个 chrome 插件,它可以自动发送我需要基本身份验证的网站的凭据.
每次执行 chromedriver 时,都会加载一个新的配置文件,我如何才能永久地将插件添加到我的测试中?我知道有 https://sites.google.com/a/chromium.org/chromedriver/extensions,但我认为这不是很清楚.
您需要在 chromeOptions
:
功能 {'浏览器名称':'铬','铬选项':{'扩展名':['base64 编码的扩展名']}}
请注意,在 extensions
中,提供 base-64 编码的打包 Chrome 扩展列表.
要获得base64 编码的扩展,您需要读取.ctx
扩展文件并使用base64
对内容进行编码.例如,使用python:
或者,更简单,使用 fs
和 q
制作 helper.js
文件:
var q = require('q');var fs = require('fs');export.getCapabilities = 函数(文件名){var deferred = q.defer();fs.readFile(文件名,函数(错误,数据){var 能力 = {'浏览器名称':'铬','铬选项':{扩展名: [data.toString('base64')]}};deferred.resolve(capabilities);});返回 deferred.promise;};
然后,在您的量角器配置中,使用此 getCapabilities()
函数来获取功能:
var helper = require('./helper.js');出口.config = {能力:helper.getCapabilities('/path/to/crx/extension'),...}
目前,它适用于单个扩展,因此还有改进的余地.
此外,如果您遇到问题,请查看以下问题:
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.
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:
这篇关于是否可以在量角器测试下向 chromedriver 添加插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!