refs #54 - added helper function to use XPath selectors
Showing
3 changed files
with
49 additions
and
19 deletions
... | @@ -41,6 +41,16 @@ exports.create = function create(options) { | ... | @@ -41,6 +41,16 @@ exports.create = function create(options) { |
41 | return new Casper(options); | 41 | return new Casper(options); |
42 | }; | 42 | }; |
43 | 43 | ||
44 | exports.selectXPath = function selectXPath(expression) { | ||
45 | return { | ||
46 | type: 'xpath', | ||
47 | path: expression, | ||
48 | toString: function() { | ||
49 | return this.type + ' selector: ' + this.selector; | ||
50 | } | ||
51 | }; | ||
52 | }; | ||
53 | |||
44 | /** | 54 | /** |
45 | * Main Casper object. | 55 | * Main Casper object. |
46 | * | 56 | * |
... | @@ -479,9 +489,6 @@ Casper.prototype.fetchText = function fetchText(selector) { | ... | @@ -479,9 +489,6 @@ Casper.prototype.fetchText = function fetchText(selector) { |
479 | */ | 489 | */ |
480 | Casper.prototype.fill = function fill(selector, vals, submit) { | 490 | Casper.prototype.fill = function fill(selector, vals, submit) { |
481 | submit = submit === true ? submit : false; | 491 | submit = submit === true ? submit : false; |
482 | if (!utils.isString(selector) || !selector.length) { | ||
483 | throw new CasperError("Form selector must be a non-empty string"); | ||
484 | } | ||
485 | if (!utils.isObject(vals)) { | 492 | if (!utils.isObject(vals)) { |
486 | throw new CasperError("Form values must be provided as an object"); | 493 | throw new CasperError("Form values must be provided as an object"); |
487 | } | 494 | } |
... | @@ -507,6 +514,10 @@ Casper.prototype.fill = function fill(selector, vals, submit) { | ... | @@ -507,6 +514,10 @@ Casper.prototype.fill = function fill(selector, vals, submit) { |
507 | } | 514 | } |
508 | // File uploads | 515 | // File uploads |
509 | if (fillResults.files && fillResults.files.length > 0) { | 516 | if (fillResults.files && fillResults.files.length > 0) { |
517 | if (utils.isObject(selector) && selector.type === 'xpath') { | ||
518 | this.echo('⚠ Filling file upload fields is currently not supported using', 'COMMENT'); | ||
519 | this.echo(' XPath selectors; Please use a CSS selector instead.', 'COMMENT'); | ||
520 | } else { | ||
510 | (function _each(self) { | 521 | (function _each(self) { |
511 | fillResults.files.forEach(function _forEach(file) { | 522 | fillResults.files.forEach(function _forEach(file) { |
512 | var fileFieldSelector = [selector, 'input[name="' + file.name + '"]'].join(' '); | 523 | var fileFieldSelector = [selector, 'input[name="' + file.name + '"]'].join(' '); |
... | @@ -514,6 +525,7 @@ Casper.prototype.fill = function fill(selector, vals, submit) { | ... | @@ -514,6 +525,7 @@ Casper.prototype.fill = function fill(selector, vals, submit) { |
514 | }); | 525 | }); |
515 | })(this); | 526 | })(this); |
516 | } | 527 | } |
528 | } | ||
517 | // Form submission? | 529 | // Form submission? |
518 | if (submit) { | 530 | if (submit) { |
519 | this.evaluate(function _evaluate(selector) { | 531 | this.evaluate(function _evaluate(selector) { | ... | ... |
1 | var fs = require('fs'); | 1 | var fs = require('fs'); |
2 | var clientutils = require('clientutils').create(); | 2 | var clientutils = require('clientutils').create(); |
3 | |||
3 | var testCases = { | 4 | var testCases = { |
4 | 'an empty string': '', | 5 | 'an empty string': '', |
5 | 'a word': 'plop', | 6 | 'a word': 'plop', |
... | @@ -20,19 +21,4 @@ for (var what in testCases) { | ... | @@ -20,19 +21,4 @@ for (var what in testCases) { |
20 | casper.test.assertEquals(clientutils.decode(encoded), source, 'ClientUtils can encode and decode ' + what); | 21 | casper.test.assertEquals(clientutils.decode(encoded), source, 'ClientUtils can encode and decode ' + what); |
21 | } | 22 | } |
22 | 23 | ||
23 | casper.test.comment('XPath'); | 24 | casper.test.done(); |
24 | |||
25 | casper.start('tests/site/index.html', function() { | ||
26 | this.test.assertExists({ | ||
27 | type: 'xpath', | ||
28 | path: '/html/body/ul/li[2]' | ||
29 | }, 'XPath selector can find an element'); | ||
30 | this.test.assertDoesntExist({ | ||
31 | type: 'xpath', | ||
32 | path: '/html/body/ol/li[2]' | ||
33 | }, 'XPath selector does not retrieve an unexistent element'); | ||
34 | }); | ||
35 | |||
36 | casper.run(function() { | ||
37 | this.test.done(); | ||
38 | }); | ... | ... |
tests/suites/casper/xpath.js
0 → 100644
1 | var x = require('casper').selectXPath; | ||
2 | |||
3 | casper.test.comment('XPath'); | ||
4 | |||
5 | casper.start('tests/site/index.html', function() { | ||
6 | this.test.assertExists({ | ||
7 | type: 'xpath', | ||
8 | path: '/html/body/ul/li[2]' | ||
9 | }, 'XPath selector can find an element'); | ||
10 | this.test.assertDoesntExist({ | ||
11 | type: 'xpath', | ||
12 | path: '/html/body/ol/li[2]' | ||
13 | }, 'XPath selector does not retrieve an unexistent element'); | ||
14 | this.test.assertExists(x('/html/body/ul/li[2]'), 'selectXPath() shortcut can find an element as well'); | ||
15 | this.test.assertEvalEquals(function() { | ||
16 | return __utils__.findAll({type: 'xpath', path: '/html/body/ul/li'}).length; | ||
17 | }, 3, 'Correct number of elements are found'); | ||
18 | }); | ||
19 | |||
20 | casper.thenClick(x('/html/body/a[2]'), function() { | ||
21 | this.test.assertTitle('CasperJS test form', 'Clicking XPath works as expected'); | ||
22 | this.fill(x('/html/body/form'), { | ||
23 | email: 'chuck@norris.com' | ||
24 | }); | ||
25 | this.test.assertEvalEquals(function() { | ||
26 | return document.querySelector('input[name="email"]').value; | ||
27 | }, 'chuck@norris.com', 'Casper.fill() can fill an input[type=text] form field'); | ||
28 | }); | ||
29 | |||
30 | casper.run(function() { | ||
31 | this.test.done(); | ||
32 | }); |
-
Please register or sign in to post a comment