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
### Bugfixes & enhancements
- [Casper.resourceExists()](http://casperjs.org/api.html#casper.resourceExists) and related functions now checks for non HTTP-404 received responses.
- 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)
- 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)
- fixed [#250](https://github.com/n1k0/casperjs/issues/250) - prevent self tests to be run using the standard `casper test` command
- 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) {
/**
* Logs the HTML code of the current page.
*
* @param String selector A DOM CSS3/XPath selector (optional)
* @param Boolean outer Whether to fetch outer HTML contents (default: false)
* @return Casper
*/
Casper.prototype.debugHTML = function debugHTML() {
Casper.prototype.debugHTML = function debugHTML(selector, outer) {
"use strict";
this.echo(this.page.content);
return this;
return this.echo(this.getHTML(selector, outer));
};
/**
......@@ -592,7 +593,7 @@ Casper.prototype.exit = function exit(status) {
};
/**
* Fetches innerText within the element(s) matching a given CSS3
* Fetches plain text contents contained in the DOM element(s) matching a given CSS3/XPath
* selector.
*
* @param String selector A DOM CSS3/XPath selector
......@@ -600,6 +601,9 @@ Casper.prototype.exit = function exit(status) {
*/
Casper.prototype.fetchText = function fetchText(selector) {
"use strict";
if (!this.started) {
throw new CasperError("Casper not started, can't fetchText()");
}
return this.evaluate(function _evaluate(selector) {
return window.__utils__.fetchText(selector);
}, { selector: selector });
......@@ -814,6 +818,31 @@ Casper.prototype.getGlobal = function getGlobal(name) {
};
/**
* Retrieves current HTML code matching the provided CSS3/XPath selector.
* Returns the HTML contents for the whole page if no arg is passed.
*
* @param String selector A DOM CSS3/XPath selector
* @param Boolean outer Whether to fetch outer HTML contents (default: false)
* @return String
*/
Casper.prototype.getHTML = function getHTML(selector, outer) {
"use strict";
if (!this.started) {
throw new CasperError("Casper not started, can't getHTML()");
}
if (!selector) {
return this.page.content;
}
if (!this.exists(selector)) {
throw new CasperError("No element matching selector found: " + selector);
}
return this.evaluate(function getSelectorHTML(selector, outer) {
var element = __utils__.findOne(selector);
return outer ? element.outerHTML : element.innerHTML;
}, { selector: selector, outer: !!outer });
};
/**
* Retrieves current page title, if any.
*
* @return String
......
casper.start('tests/site/index.html', function() {
this.test.assertEquals(this.getHTML('ul li'), 'one', 'Casper.getHTML() retrieves inner HTML by default');
this.test.assertEquals(this.getHTML('ul li', true), '<li>one</li>', 'Casper.getHTML() can retrieve outer HTML');
});
casper.run(function() {
casper.test.done();
});