3 /* global pages, config */
5 var chai = require("chai");
6 var Page = require("../lib/Page.js");
7 var expect = chai.expect;
8 var phridge = require("../lib/main.js");
9 var slow = require("./helpers/slow.js");
11 require("./helpers/setup.js");
13 describe("Page", function () {
15 describe(".prototype", function () {
19 function createPage() {
20 page = phantom.createPage();
23 function disposePage() {
27 before(slow(function () {
28 return phridge.spawn({}).then(function (newPhantom) {
33 after(slow(function () {
34 return phantom.dispose();
37 describe(".constructor(phantom, id)", function () {
39 it("should return an instance of Page with the given arguments applied", function () {
40 page = new Page(phantom, 1);
41 expect(page).to.be.an.instanceOf(Page);
42 expect(page.phantom).to.equal(phantom);
43 expect(page._id).to.equal(1);
48 describe(".phantom", function () {
50 it("should be null by default", function () {
51 expect(Page.prototype.phantom).to.equal(null);
56 describe(".run(arg1, arg2, arg3, fn)", function () {
61 describe("with fn being an asynchronous function", function () {
63 it("should provide a resolve function", function () {
64 return expect(page.run(function (resolve) {
65 resolve("everything ok");
66 })).to.eventually.equal("everything ok");
69 it("should provide the possibility to resolve with any stringify-able data", function () {
71 expect(page.run(function (resolve) {
73 })).to.eventually.equal(undefined),
74 expect(page.run(function (resolve) {
76 })).to.eventually.equal(true),
77 expect(page.run(function (resolve) {
79 })).to.eventually.equal(2),
80 expect(page.run(function (resolve) {
82 })).to.eventually.equal(null),
83 expect(page.run(function (resolve) {
85 })).to.eventually.deep.equal([1, 2, 3]),
86 expect(page.run(function (resolve) {
91 })).to.eventually.deep.equal({
98 it("should provide a reject function", function () {
99 return page.run(function (resolve, reject) {
100 reject(new Error("not ok"));
101 }).catch(function (err) {
102 expect(err.message).to.equal("not ok");
108 describe("with fn being a synchronous function", function () {
110 it("should resolve to the returned value", function () {
111 return expect(page.run(function () {
112 return "everything ok";
113 })).to.eventually.equal("everything ok");
116 it("should provide the possibility to resolve with any stringify-able data", function () {
118 expect(page.run(function () {
120 })).to.eventually.equal(undefined),
121 expect(page.run(function () {
123 })).to.eventually.equal(true),
124 expect(page.run(function () {
126 })).to.eventually.equal(2),
127 expect(page.run(function () {
129 })).to.eventually.equal(null),
130 expect(page.run(function () {
132 })).to.eventually.deep.equal([1, 2, 3]),
133 expect(page.run(function () {
138 })).to.eventually.deep.equal({
145 it("should reject the promise if fn throws an error", function () {
146 return page.run(function () {
147 throw new Error("not ok");
148 }).catch(function (err) {
149 expect(err.message).to.equal("not ok");
155 it("should provide all phantomjs default modules as convenience", function () {
156 return expect(page.run(function () {
157 return Boolean(webpage && system && fs && webserver && child_process); // eslint-disable-line
158 })).to.eventually.equal(true);
161 it("should provide the config object to store all kind of configuration", function () {
162 return expect(page.run(function () {
164 })).to.eventually.deep.equal({});
167 it("should provide the possibility to pass params", function () {
176 return expect(page.run(params, params, params, function (params1, params2, params3) {
177 return [params1, params2, params3];
178 })).to.eventually.deep.equal([params, params, params]);
181 it("should report errors", function () {
182 return expect(page.run(function () {
183 undefinedVariable; // eslint-disable-line
184 })).to.be.rejectedWith("Can't find variable: undefinedVariable");
187 it("should preserve all error details like stack traces", function () {
190 .run(function brokenFunction() {
191 undefinedVariable; // eslint-disable-line
192 }).catch(function (err) {
193 expect(err).to.have.property("message", "Can't find variable: undefinedVariable");
194 expect(err).to.have.property("stack");
195 //console.log(err.stack);
198 .run(function (resolve, reject) {
199 reject(new Error("Custom Error"));
201 .catch(function (err) {
202 expect(err).to.have.property("message", "Custom Error");
203 expect(err).to.have.property("stack");
204 //console.log(err.stack);
209 it("should run the function with the page as context", function () {
210 return page.run(/** @this WebPage */function () {
211 if (!this.clipRect) {
212 throw new Error("The function's context is not the web page");
219 describe(".dispose()", function () {
221 beforeEach(createPage);
223 it("should remove the page from the pages-object", function () {
224 var pageId = page._id;
226 function checkForPage(pageId) {
228 throw new Error("page is still present in the page-object");
232 return page.dispose().then(function () {
233 return phantom.run(pageId, checkForPage);
237 it("should remove the phantom reference", function () {
238 return page.dispose().then(function () {
239 expect(page.phantom).to.equal(null);