Commit 6e2efd07 6e2efd07078420590b6604f656a9484fa5bcb0c5 by Nicolas Perriault

closes #205 - debugHTML() can have a selector passed; added getHTML()

1 parent a68facbf
...@@ -64,6 +64,7 @@ The documentation has been [updated accordingly](http://casperjs.org/api.html#ca ...@@ -64,6 +64,7 @@ The documentation has been [updated accordingly](http://casperjs.org/api.html#ca
64 ### Bugfixes & enhancements 64 ### Bugfixes & enhancements
65 65
66 - [Casper.resourceExists()](http://casperjs.org/api.html#casper.resourceExists) and related functions now checks for non HTTP-404 received responses. 66 - [Casper.resourceExists()](http://casperjs.org/api.html#casper.resourceExists) and related functions now checks for non HTTP-404 received responses.
67 - closes [#205](https://github.com/n1k0/casperjs/issues/205) - [`debugHTML()`](http://casperjs.org/api.html#casper.debugHTML) can have a selector passed; added [`getHTML()`](http://casperjs.org/api.html#casper.getHTML)
67 - closes [#230](https://github.com/n1k0/casperjs/issues/230) - added [`ClientUtils.getElementsBound()`](http://casperjs.org/api.html#clientutils.getElementsBounds) and [`Casper.getElementsBound()`](http://casperjs.org/api.html#casper.getElementsBounds) 68 - closes [#230](https://github.com/n1k0/casperjs/issues/230) - added [`ClientUtils.getElementsBound()`](http://casperjs.org/api.html#clientutils.getElementsBounds) and [`Casper.getElementsBound()`](http://casperjs.org/api.html#casper.getElementsBounds)
68 - fixed [#250](https://github.com/n1k0/casperjs/issues/250) - prevent self tests to be run using the standard `casper test` command 69 - fixed [#250](https://github.com/n1k0/casperjs/issues/250) - prevent self tests to be run using the standard `casper test` command
69 - fixes [#254](https://github.com/n1k0/casperjs/issues/254) - fix up one use of qsa, hit when filling forms with missing elements 70 - fixes [#254](https://github.com/n1k0/casperjs/issues/254) - fix up one use of qsa, hit when filling forms with missing elements
......
...@@ -403,12 +403,13 @@ Casper.prototype.createStep = function createStep(fn, options) { ...@@ -403,12 +403,13 @@ Casper.prototype.createStep = function createStep(fn, options) {
403 /** 403 /**
404 * Logs the HTML code of the current page. 404 * Logs the HTML code of the current page.
405 * 405 *
406 * @param String selector A DOM CSS3/XPath selector (optional)
407 * @param Boolean outer Whether to fetch outer HTML contents (default: false)
406 * @return Casper 408 * @return Casper
407 */ 409 */
408 Casper.prototype.debugHTML = function debugHTML() { 410 Casper.prototype.debugHTML = function debugHTML(selector, outer) {
409 "use strict"; 411 "use strict";
410 this.echo(this.page.content); 412 return this.echo(this.getHTML(selector, outer));
411 return this;
412 }; 413 };
413 414
414 /** 415 /**
...@@ -592,7 +593,7 @@ Casper.prototype.exit = function exit(status) { ...@@ -592,7 +593,7 @@ Casper.prototype.exit = function exit(status) {
592 }; 593 };
593 594
594 /** 595 /**
595 * Fetches innerText within the element(s) matching a given CSS3 596 * Fetches plain text contents contained in the DOM element(s) matching a given CSS3/XPath
596 * selector. 597 * selector.
597 * 598 *
598 * @param String selector A DOM CSS3/XPath selector 599 * @param String selector A DOM CSS3/XPath selector
...@@ -600,6 +601,9 @@ Casper.prototype.exit = function exit(status) { ...@@ -600,6 +601,9 @@ Casper.prototype.exit = function exit(status) {
600 */ 601 */
601 Casper.prototype.fetchText = function fetchText(selector) { 602 Casper.prototype.fetchText = function fetchText(selector) {
602 "use strict"; 603 "use strict";
604 if (!this.started) {
605 throw new CasperError("Casper not started, can't fetchText()");
606 }
603 return this.evaluate(function _evaluate(selector) { 607 return this.evaluate(function _evaluate(selector) {
604 return window.__utils__.fetchText(selector); 608 return window.__utils__.fetchText(selector);
605 }, { selector: selector }); 609 }, { selector: selector });
...@@ -814,6 +818,31 @@ Casper.prototype.getGlobal = function getGlobal(name) { ...@@ -814,6 +818,31 @@ Casper.prototype.getGlobal = function getGlobal(name) {
814 }; 818 };
815 819
816 /** 820 /**
821 * Retrieves current HTML code matching the provided CSS3/XPath selector.
822 * Returns the HTML contents for the whole page if no arg is passed.
823 *
824 * @param String selector A DOM CSS3/XPath selector
825 * @param Boolean outer Whether to fetch outer HTML contents (default: false)
826 * @return String
827 */
828 Casper.prototype.getHTML = function getHTML(selector, outer) {
829 "use strict";
830 if (!this.started) {
831 throw new CasperError("Casper not started, can't getHTML()");
832 }
833 if (!selector) {
834 return this.page.content;
835 }
836 if (!this.exists(selector)) {
837 throw new CasperError("No element matching selector found: " + selector);
838 }
839 return this.evaluate(function getSelectorHTML(selector, outer) {
840 var element = __utils__.findOne(selector);
841 return outer ? element.outerHTML : element.innerHTML;
842 }, { selector: selector, outer: !!outer });
843 };
844
845 /**
817 * Retrieves current page title, if any. 846 * Retrieves current page title, if any.
818 * 847 *
819 * @return String 848 * @return String
......
1 casper.start('tests/site/index.html', function() {
2 this.test.assertEquals(this.getHTML('ul li'), 'one', 'Casper.getHTML() retrieves inner HTML by default');
3 this.test.assertEquals(this.getHTML('ul li', true), '<li>one</li>', 'Casper.getHTML() can retrieve outer HTML');
4 });
5
6 casper.run(function() {
7 casper.test.done();
8 });