Commit 75755c0f 75755c0f90bf6abe25b872e4638f48613daf846d by Nicolas Perriault

refs #54 - added helper function to use XPath selectors

1 parent 241f53da
......@@ -41,6 +41,16 @@ exports.create = function create(options) {
return new Casper(options);
};
exports.selectXPath = function selectXPath(expression) {
return {
type: 'xpath',
path: expression,
toString: function() {
return this.type + ' selector: ' + this.selector;
}
};
};
/**
* Main Casper object.
*
......@@ -479,9 +489,6 @@ Casper.prototype.fetchText = function fetchText(selector) {
*/
Casper.prototype.fill = function fill(selector, vals, submit) {
submit = submit === true ? submit : false;
if (!utils.isString(selector) || !selector.length) {
throw new CasperError("Form selector must be a non-empty string");
}
if (!utils.isObject(vals)) {
throw new CasperError("Form values must be provided as an object");
}
......@@ -507,12 +514,17 @@ Casper.prototype.fill = function fill(selector, vals, submit) {
}
// File uploads
if (fillResults.files && fillResults.files.length > 0) {
(function _each(self) {
fillResults.files.forEach(function _forEach(file) {
var fileFieldSelector = [selector, 'input[name="' + file.name + '"]'].join(' ');
self.page.uploadFile(fileFieldSelector, file.path);
});
})(this);
if (utils.isObject(selector) && selector.type === 'xpath') {
this.echo('⚠ Filling file upload fields is currently not supported using', 'COMMENT');
this.echo(' XPath selectors; Please use a CSS selector instead.', 'COMMENT');
} else {
(function _each(self) {
fillResults.files.forEach(function _forEach(file) {
var fileFieldSelector = [selector, 'input[name="' + file.name + '"]'].join(' ');
self.page.uploadFile(fileFieldSelector, file.path);
});
})(this);
}
}
// Form submission?
if (submit) {
......
var fs = require('fs');
var clientutils = require('clientutils').create();
var testCases = {
'an empty string': '',
'a word': 'plop',
......@@ -20,19 +21,4 @@ for (var what in testCases) {
casper.test.assertEquals(clientutils.decode(encoded), source, 'ClientUtils can encode and decode ' + what);
}
casper.test.comment('XPath');
casper.start('tests/site/index.html', function() {
this.test.assertExists({
type: 'xpath',
path: '/html/body/ul/li[2]'
}, 'XPath selector can find an element');
this.test.assertDoesntExist({
type: 'xpath',
path: '/html/body/ol/li[2]'
}, 'XPath selector does not retrieve an unexistent element');
});
casper.run(function() {
this.test.done();
});
casper.test.done();
......
var x = require('casper').selectXPath;
casper.test.comment('XPath');
casper.start('tests/site/index.html', function() {
this.test.assertExists({
type: 'xpath',
path: '/html/body/ul/li[2]'
}, 'XPath selector can find an element');
this.test.assertDoesntExist({
type: 'xpath',
path: '/html/body/ol/li[2]'
}, 'XPath selector does not retrieve an unexistent element');
this.test.assertExists(x('/html/body/ul/li[2]'), 'selectXPath() shortcut can find an element as well');
this.test.assertEvalEquals(function() {
return __utils__.findAll({type: 'xpath', path: '/html/body/ul/li'}).length;
}, 3, 'Correct number of elements are found');
});
casper.thenClick(x('/html/body/a[2]'), function() {
this.test.assertTitle('CasperJS test form', 'Clicking XPath works as expected');
this.fill(x('/html/body/form'), {
email: 'chuck@norris.com'
});
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="email"]').value;
}, 'chuck@norris.com', 'Casper.fill() can fill an input[type=text] form field');
});
casper.run(function() {
this.test.done();
});