diff --git a/lib/actions.js b/lib/actions.js index e40fa0b..c6f6daa 100644 --- a/lib/actions.js +++ b/lib/actions.js @@ -86,6 +86,24 @@ exports.open = function(url, method) { }); }; +/** + * Load HTML content with fake URL in Phantom. + * @param {string} html HTML Content to set + * @param {string} url URL to use for page + * @see {@link http://phantomjs.org/api/webpage/method/set-content.html|PhantomJS API} + */ +exports.setContent = function(html, url) { + var self = this; + self.targetUrl = url; + + return this.ready.then(function() { + debug('.setContent()', html, url); + return HorsemanPromise.fromCallback(function(done) { + self.page.setContent(html, url, done); + }); + }); +}; + /** * Set headers sent to the remote server during an 'open'. * @param {Object[]} headers diff --git a/test/files/set-content-test.html b/test/files/set-content-test.html new file mode 100644 index 0000000..a35b2ee --- /dev/null +++ b/test/files/set-content-test.html @@ -0,0 +1,10 @@ + + + + + Testing Page + + +

Hello World!

+ + diff --git a/test/index.js b/test/index.js index 3fca99b..2ccbf8b 100644 --- a/test/index.js +++ b/test/index.js @@ -197,6 +197,31 @@ function navigation(bool) { .have.properties(size); }); + it('should set content and url', function() { + var horseman = new Horseman({ + timeout: defaultTimeout, + injectJquery: bool + }); + + var fakeHtml = fs.readFileSync(__dirname + '/files/set-content-test.html').toString(); + var fakeDomain = 'http://fakedomain.com/'; + + return horseman + .setContent(fakeHtml, fakeDomain) + .evaluate(function() { + return { + href: location.href, + title: document.title + }; + }) + .close() + .should.eventually + .have.properties({ + href: fakeDomain, + title: 'Testing Page' + }); + }); + it('should let you scroll', function() { var horseman = new Horseman({ timeout: defaultTimeout,