. nightwatch.
//, 8080. 2.
// -, . browserermob-proxy-api. , .
proxyObj.startPort(, (err, data) {
if (err) {
console.log(ERR);
} else {
console.log( " " )
}
})
, Chrome , .
proxyObj.startPort(, (err, data) {
if (err) {
console.log(ERR);
} else {
console.log( " " )
var dataInJson = JSON.parse();
// 8:
this.test_settings.desiredCapabilities = {
"browserName": "chrome",
"proxyObj": proxyObj,
"proxyport": dataInJson.port,
"proxy": {
"proxyType": "manual",
"httpProxy": "127.0.0.1:" + dataInJson.port,
"sslProxy": "127.0.0.1:" + dataInJson.port
},
"javascriptEnabled": true,
"acceptSslCerts": true,
"loggingPrefs": {
"browser": "ALL"
}
}
}
})
, , cmd [, 2 , . - ]
HAR HAR, browserermob-proxy-api api.
createHAR.js nightwatch.json [custom_commands section]
export.command = function (callback) {
var self = this;
if (! self.options.desiredCapabilities.proxyObj) {
console.error( " - - setupProxy()?" );
}
this.options.desiredCapabilities.proxyObj.createHAR(this.options.desiredCapabilities.proxyport, {
'captureHeaders': 'true',
'captureContent': 'true',
'captureBinaryContent': 'true',
'initialPageRef': 'homepage'
}, function (err, result) {
(ERR) {
console.log(ERR)
} {
console.log()
if (typeof callback === "function" ) {
console.log(this.options.desiredCapabilities.proxyObj);
console.log(this.options.desiredCapabilities.proxyport);
//console.log();
callback.call(self, result);
}
}
});
;
};
, HAR, getHAR.js, .
var parsedData;
exports.command = function(callback) {
var self = this;
if (!self.options.desiredCapabilities.proxy) {
console.error('No proxy setup - did you call setupProxy() ?');
}
self.options.desiredCapabilities.proxyObj.getHAR(self.options.desiredCapabilities.proxyport, function (err, data) {
console.log(self.options.desiredCapabilities.proxyObj);
console.log(self.options.desiredCapabilities.proxyport);
if(err){
console.log(err)
}else{
parsedData = JSON.parse(data)
console.log(parsedData.log.entries)
}
if (typeof callback === "function") {
console.log(self.options.desiredCapabilities.proxyObj);
console.log(self.options.desiredCapabilities.proxyport);
callback.call(self, parsedData);
}
});
return this;
};
createHAR proxyObj, . browser.perform()
browser.perform (function () {browser.createHAR ()}) //// some kind of navigation
browser.perform (function () {browser.getHAR ()})
Note. If you work for a corporate proxy, you may need to use the proxy function offered by the browser. According to the browsermob proxy documentation, go to the api section, → / proxy may have the request parameters "proxyUsername" and "proxyPassword"
In node_modules -> browserermob-proxy-api-> index.js add the line below line 22:
this.proxyUsername = cfg.proxyUsername || '';
this.proxyPassword = cfg.proxyPassword || '';
this.queryString = cfg.queryString || 'httpProxy=yourupstreamProxy:8080';
then at line 177, where package is making request '/proxy' to browser.
replace
path: url
to
path: url + '?proxyUsername=' +this.proxyUsername + '&proxyPassword=' + this.proxyPassword + '&' + this.queryString
source
share