Commit 90175b9f 90175b9f481c2fe313d941a576b6ad7d0da7f7fe by Nicolas Perriault

added a 'safeLogs' option to blur password value in debug logs

1 parent 2ffa3112
......@@ -80,6 +80,7 @@ var Casper = function Casper(options) {
exitOnError: true,
logLevel: "error",
httpStatusHandlers: {},
safeLogs: true,
onAlert: null,
onDie: null,
onError: null,
......@@ -385,7 +386,7 @@ Casper.prototype.die = function die(message, status) {
* @return Casper
*/
Casper.prototype.download = function download(url, targetPath, method, data) {
var cu = require('clientutils').create();
var cu = require('clientutils').create(this.options);
try {
fs.write(targetPath, cu.decode(this.base64encode(url, method, data)), 'wb');
this.emit('downloaded.file', targetPath);
......@@ -700,8 +701,13 @@ Casper.prototype.injectClientUtils = function injectClientUtils() {
if (true === this.page.injectJs(clientUtilsPath)) {
this.log("Successfully injected Casper client-side utilities", "debug");
} else {
this.log("Failed to instantiate Casper client-side utilities!", "warning");
this.warn("Failed to inject Casper client-side utilities");
}
// ClientUtils and Casper shares the same options
// These are not the lines I'm the most proud of in my life, but it works.
this.page.evaluate(function() {
__utils__ = new ClientUtils(__options);
}.toString().replace('__options', JSON.stringify(this.options)));
};
/**
......
......@@ -28,14 +28,15 @@
*
*/
(function(exports) {
exports.create = function create() {
return new this.ClientUtils();
exports.create = function create(options) {
return new this.ClientUtils(options);
};
/**
* Casper client-side helpers.
*/
exports.ClientUtils = function ClientUtils() {
exports.ClientUtils = function ClientUtils(options) {
// private members
var BASE64_ENCODE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var BASE64_DECODE_CHARS = new Array(
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
......@@ -49,6 +50,9 @@
);
var SUPPORTED_SELECTOR_TYPES = ['css', 'xpath'];
// public members
this.options = options || {};
/**
* Clicks on the DOM element behind the provided selector.
*
......@@ -185,7 +189,7 @@
files: []
};
if (!(form instanceof HTMLElement) || typeof form === "string") {
__utils__.log("attempting to fetch form element from selector: '" + form + "'", "info");
this.log("attempting to fetch form element from selector: '" + form + "'", "info");
try {
form = this.findOne(form);
} catch (e) {
......@@ -463,7 +467,7 @@
*/
this.setField = function setField(field, value) {
var fields, out;
value = value || "";
value = logValue = (value || "");
if (field instanceof NodeList) {
fields = field;
field = fields[0];
......@@ -471,11 +475,15 @@
if (!field instanceof HTMLElement) {
this.log("Invalid field type; only HTMLElement and NodeList are supported", "error");
}
this.log('Set "' + field.getAttribute('name') + '" field value to ' + value, "debug");
if (this.options && this.options.safeLogs && field.getAttribute('type') === "password") {
// obfuscate password value
logValue = Array(value.length + 1).join("*");
}
this.log('Set "' + field.getAttribute('name') + '" field value to ' + logValue, "debug");
try {
field.focus();
} catch (e) {
__utils__.log("Unable to focus() input field " + field.getAttribute('name') + ": " + e, "warning");
this.log("Unable to focus() input field " + field.getAttribute('name') + ": " + e, "warning");
}
var nodeName = field.nodeName.toLowerCase();
switch (nodeName) {
......@@ -544,7 +552,7 @@
try {
field.blur();
} catch (err) {
__utils__.log("Unable to blur() input field " + field.getAttribute('name') + ": " + err, "warning");
this.log("Unable to blur() input field " + field.getAttribute('name') + ": " + err, "warning");
}
return out;
};
......@@ -570,7 +578,4 @@
}
};
};
// silly "hack" to force having an instance available
exports.__utils__ = new exports.ClientUtils();
})(typeof exports === "object" ? exports : window);
......
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>CasperJS test form</title>
</head>
<body>
<form action="result.html" enctype="multipart/form-data">
<input type="text" name="email" placeholder="email" />
<input type="text" name="email" placeholder="email">
<input type="password" name="password" placeholder="password">
<textarea name="content"></textarea>
<select name="topic">
<option>foo</option>
<option value="bar">baz</option>
</select>
<input type="checkbox" name="check" />
<input type="radio" name="choice" value="yes"/>
<input type="radio" name="choice" value="no"/>
<input type="file" name="file"/>
<input type="checkbox" name="checklist[]" value="1" />
<input type="checkbox" name="checklist[]" value="2" />
<input type="checkbox" name="checklist[]" value="3" />
<input type="submit" name="submit" value="submit" />
<input type="checkbox" name="check">
<input type="radio" name="choice" value="yes">
<input type="radio" name="choice" value="no">
<input type="file" name="file">
<input type="checkbox" name="checklist[]" value="1">
<input type="checkbox" name="checklist[]" value="2">
<input type="checkbox" name="checklist[]" value="3">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
......
......@@ -2,6 +2,7 @@ casper.start('tests/site/form.html', function() {
this.test.comment('Casper.fill()');
this.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
password: 'chuck',
content: 'Am watching thou',
check: true,
choice: 'no',
......@@ -13,6 +14,9 @@ casper.start('tests/site/form.html', function() {
return document.querySelector('input[name="email"]').value;
}, 'chuck@norris.com', 'Casper.fill() can fill an input[type=text] form field');
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="password"]').value;
}, 'chuck', 'Casper.fill() can fill an input[type=password] form field');
this.test.assertEvalEquals(function() {
return document.querySelector('textarea[name="content"]').value;
}, 'Am watching thou', 'Casper.fill() can fill a textarea form field');
this.test.assertEvalEquals(function() {
......@@ -41,6 +45,7 @@ casper.start('tests/site/form.html', function() {
casper.then(function() {
this.test.comment('Form submitted');
this.test.assertUrlMatch(/email=chuck@norris.com/, 'Casper.fill() input[type=email] field was submitted');
this.test.assertUrlMatch(/password=chuck/, 'Casper.fill() input[type=password] field was submitted');
this.test.assertUrlMatch(/content=Am\+watching\+thou/, 'Casper.fill() textarea field was submitted');
this.test.assertUrlMatch(/check=on/, 'Casper.fill() input[type=checkbox] field was submitted');
this.test.assertUrlMatch(/choice=no/, 'Casper.fill() input[type=radio] field was submitted');
......