3 var chai = require("chai");
4 var getport = require("getport");
5 var net = require("net");
6 var expect = chai.expect;
7 var spawn = require("../lib/spawn.js");
8 var phridge = require("../lib/main.js");
9 var Phantom = require("../lib/Phantom.js");
10 var slow = require("./helpers/slow.js");
11 var lift = require("../lib/lift.js");
13 require("./helpers/setup.js");
15 getport = lift(getport);
17 describe("spawn(config?)", function () {
20 function blockGhostDriverPort() {
22 .then(function (freePort) {
23 var server = net.createServer();
24 var listen = lift(server.listen);
28 // We're blocking the GhostDriver port so phantomjs crashes on startup.
29 // Otherwise the phantomjs processes can't be killed because it doesn't
30 // listen on our commands in GhostDriver-mode.
31 return listen.call(server, freePort);
35 after(slow(function () {
36 return phridge.disposeAll();
39 it("should resolve to an instance of Phantom", slow(function () {
40 return expect(spawn()).to.eventually.be.an.instanceOf(Phantom);
43 it("should pass the provided config to phantomjs", slow(function (done) {
44 // We're setting the webdriver option to test if the config is recognized
45 // Setting this option does not make any sense because phantomjs is
46 // unusable with phantomjs in GhostDriver-mode. But it prints a nice
47 // message to the console which causes the promise to be rejected
48 blockGhostDriverPort()
50 // Prevent PhantomJS from printing a disturbing error message to the console
51 phridge.config.stdout = null;
52 phridge.config.stderr = null;
54 return expect(phridge.spawn({
55 webdriver: "localhost:" + port
56 })).to.be.rejectedWith(/GhostDriver - main\.fail/);
59 phridge.config.stdout = process.stdout;
60 phridge.config.stderr = process.stderr;
62 // Give phantomjs some time to exit
63 setTimeout(done, 100);
67 it("should also allow CLI style config options", slow(function (done) {
68 // We're setting the webdriver option to test if the config is recognized
69 // Setting this option does not make any sense because phantomjs is
70 // unusable with phantomjs in GhostDriver-mode. But it prints a nice
71 // message to the console which causes the promise to be rejected
72 blockGhostDriverPort()
74 // Prevent PhantomJS from printing a disturbing error message to the console
75 phridge.config.stdout = null;
76 phridge.config.stderr = null;
78 return expect(phridge.spawn({
79 "--load-images": "true",
80 "--webdriver": "localhost:" + port
81 })).to.be.rejectedWith("GhostDriver");
84 phridge.config.stdout = process.stdout;
85 phridge.config.stderr = process.stderr;
87 // Give phantomjs some time to exit
88 setTimeout(done, 100);