From 8c8556c63dc38333620c7e7483ec760d734ae7e2 Mon Sep 17 00:00:00 2001 From: Johnathan Free Wortley Date: Thu, 22 Dec 2016 16:46:41 -0800 Subject: [PATCH 1/2] Add setContent method --- lib/actions.js | 19 +++++++++++++++++++ test/files/set-content-test.html | 10 ++++++++++ test/index.js | 25 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 test/files/set-content-test.html diff --git a/lib/actions.js b/lib/actions.js index e40fa0b..c25d8c5 100644 --- a/lib/actions.js +++ b/lib/actions.js @@ -86,6 +86,25 @@ 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, From 1d143a7d52c7cfa4db67059dc0016333f20c7d29 Mon Sep 17 00:00:00 2001 From: Johnathan Free Wortley Date: Thu, 22 Dec 2016 17:19:10 -0800 Subject: [PATCH 2/2] Clear extra whitespace --- lib/actions.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/actions.js b/lib/actions.js index c25d8c5..c6f6daa 100644 --- a/lib/actions.js +++ b/lib/actions.js @@ -97,7 +97,6 @@ exports.setContent = function(html, url) { self.targetUrl = url; return this.ready.then(function() { - debug('.setContent()', html, url); return HorsemanPromise.fromCallback(function(done) { self.page.setContent(html, url, done);