Commit bc0ab989 bc0ab989af6a15ad8b419bf08b384fe3a010c151 by Nicolas Perriault

added a 'submit' option to Casper#fill() for submitting the form

1 parent 455c53ea
...@@ -16,36 +16,32 @@ Casper.js is a navigation utility for [PhantomJS](http://www.phantomjs.org/). It ...@@ -16,36 +16,32 @@ Casper.js is a navigation utility for [PhantomJS](http://www.phantomjs.org/). It
16 In the following example, we'll query google for two terms consecutively, `capser` and `homer`, and aggregate the result links in a standard Array. Running the script will output a standard JSON string containing both the logs and the results: 16 In the following example, we'll query google for two terms consecutively, `capser` and `homer`, and aggregate the result links in a standard Array. Running the script will output a standard JSON string containing both the logs and the results:
17 17
18 ``` javascript 18 ``` javascript
19 phantom.injectJs('path/to/casper.js');
20
21 var links = [];
22 var casper = new phantom.Casper({
23 logLevel: "info"
24 });
25
26 // User defined functions
27 function q() {
28 document.querySelector('input[name="q"]').setAttribute('value', '%term%');
29 document.querySelector('form[name="f"]').submit();
30 }
31
32 function getLinks() { 19 function getLinks() {
33 var links = document.querySelectorAll('h3.r a'); 20 var links = document.querySelectorAll('h3.r a');
34 return Array.prototype.map.call(links, function(e) { 21 return Array.prototype.map.call(links, function(e) {
35 return e.getAttribute('href'); 22 return {
23 title: e.innerText,
24 href: e.getAttribute('href')
25 };
36 }); 26 });
37 } 27 }
38 28
39 // Casper suite 29 var links = [];
40 casper.start('http://google.fr/') 30 var casper = new phantom.Casper({
41 .thenEvaluate(q, { 31 logLevel: "debug",
42 term: 'casper' 32 verbose: true
33 })
34 .start('http://google.fr/')
35 .then(function(self) {
36 self.fill('form[name=f]', {
37 q: 'casperjs'
38 }, true);
43 }) 39 })
44 .then(function(self) { 40 .then(function(self) {
45 links = self.evaluate(getLinks); 41 links = self.evaluate(getLinks);
46 }) 42 self.fill('form[name=f]', {
47 .thenEvaluate(q, { 43 q: 'plop'
48 term: 'homer' 44 }, true);
49 }) 45 })
50 .then(function(self) { 46 .then(function(self) {
51 links = links.concat(self.evaluate(getLinks)); 47 links = links.concat(self.evaluate(getLinks));
...@@ -425,9 +421,9 @@ casper.start('http://www.google.fr/', function(self) { ...@@ -425,9 +421,9 @@ casper.start('http://www.google.fr/', function(self) {
425 }); 421 });
426 ``` 422 ```
427 423
428 ### Casper#fill(String selector, Object values) 424 ### Casper#fill(String selector, Object values, Boolean submit)
429 425
430 Fills the fields of a form with given values. 426 Fills the fields of a form with given values and optionnaly submit it.
431 427
432 Example: 428 Example:
433 429
...@@ -438,8 +434,7 @@ casper.start('http://some.tld/contact.form', function(self) { ...@@ -438,8 +434,7 @@ casper.start('http://some.tld/contact.form', function(self) {
438 'content': 'So be careful.', 434 'content': 'So be careful.',
439 'name': 'Chuck Norris', 435 'name': 'Chuck Norris',
440 'email': 'chuck@norris.com', 436 'email': 'chuck@norris.com',
441 }); 437 }, true);
442 self.click('input[type=submit]');
443 }).then(function(self) { 438 }).then(function(self) {
444 self.evaluateOrDie(function() { 439 self.evaluateOrDie(function() {
445 return /message sent/.test(document.body.innerText); 440 return /message sent/.test(document.body.innerText);
...@@ -449,7 +444,7 @@ casper.start('http://some.tld/contact.form', function(self) { ...@@ -449,7 +444,7 @@ casper.start('http://some.tld/contact.form', function(self) {
449 }); 444 });
450 ``` 445 ```
451 446
452 **WARNING:** Please don't use Casper nor PhantomJS to send spam, or I call the Chuck. 447 **WARNING:** Please don't use CasperJS nor PhantomJS to send spam, or I'll be calling the Chuck. More seriously, please don't.
453 448
454 ### Casper#repeat(int times, function then) 449 ### Casper#repeat(int times, function then)
455 450
......
...@@ -298,16 +298,18 @@ ...@@ -298,16 +298,18 @@
298 * 298 *
299 * @param String selector A CSS3 selector to the target form to fill 299 * @param String selector A CSS3 selector to the target form to fill
300 * @param Object values Field values 300 * @param Object values Field values
301 * @param Boolean submit Submit the form?
301 */ 302 */
302 fill: function(selector, values) { 303 fill: function(selector, values, submit) {
303 if (!typeof(values) === "object") { 304 if (!typeof(values) === "object") {
304 throw "form values must be an object"; 305 throw "form values must be an object";
305 } 306 }
306 return this.evaluate(function() { 307 return this.evaluate(function() {
307 __utils__.fill('%selector%', JSON.parse('%values%')); 308 __utils__.fill('%selector%', JSON.parse('%values%'), JSON.parse('%submit%'));
308 }, { 309 }, {
309 selector: selector.replace("'", "\'"), 310 selector: selector.replace("'", "\'"),
310 values: JSON.stringify(values).replace("'", "\'"), 311 values: JSON.stringify(values).replace("'", "\'"),
312 submit: JSON.stringify(submit)
311 }); 313 });
312 }, 314 },
313 315
...@@ -588,18 +590,18 @@ ...@@ -588,18 +590,18 @@
588 }; 590 };
589 591
590 /** 592 /**
591 * Fills a form with provided field values. 593 * Fills a form with provided field values, and optionnaly submits it.
592 * 594 *
593 * @param HTMLElement|String form A form element, or a CSS3 selector to a form element 595 * @param HTMLElement|String form A form element, or a CSS3 selector to a form element
594 * @param Object vals Field values 596 * @param Object vals Field values
595 */ 597 */
596 this.fill = function(form, vals) { 598 this.fill = function(form, vals, submit) {
599 submit = typeof(submit) !== "undefined" || false;
597 if (!(form instanceof HTMLElement) || typeof(form) === "string") { 600 if (!(form instanceof HTMLElement) || typeof(form) === "string") {
598 form = document.querySelector(form); 601 form = document.querySelector(form);
599 console.log('found via selector')
600 } 602 }
601 if (!form) { 603 if (!form) {
602 console.log('form not found or invalid'); 604 console.log('form not found or invalid selector provided:');
603 return; 605 return;
604 } 606 }
605 for (var name in vals) { 607 for (var name in vals) {
...@@ -614,6 +616,9 @@ ...@@ -614,6 +616,9 @@
614 } 616 }
615 this.setField(field, value); 617 this.setField(field, value);
616 } 618 }
619 if (submit) {
620 form.submit();
621 }
617 }; 622 };
618 623
619 /** 624 /**
...@@ -651,6 +656,8 @@ ...@@ -651,6 +656,8 @@
651 if (!field instanceof HTMLElement) { 656 if (!field instanceof HTMLElement) {
652 console.log('the field must be an HTMLElement'); 657 console.log('the field must be an HTMLElement');
653 return; 658 return;
659 } else {
660 console.log('set "' + field.getAttribute('name') + '" value to ' + value);
654 } 661 }
655 value = value || ""; 662 value = value || "";
656 switch (field.nodeName.toLowerCase()) { 663 switch (field.nodeName.toLowerCase()) {
......