Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Adds methods: exec() and run() #588

Merged
merged 3 commits into from
Aug 3, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/phantomjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

var fs = require('fs')
var path = require('path')
var spawn = require('child_process').spawn
var Promise = require('es6-promise').Promise


/**
Expand Down Expand Up @@ -61,3 +63,34 @@ if (exports.path) {
// We did our best. Likely because phantomjs was already installed.
}
}

/**
* Executes a script or just runs PhantomJS
*/
exports.exec = function () {
var args = Array.prototype.slice.call(arguments)
return spawn(exports.path, args)
}

/**
* Runs PhantomJS with provided options
* @example
* // handy with WebDriver
* phantomjs.run('--webdriver=4444').then(program => {
* // do something
* program.kill()
* })
* @returns {Promise} the process of PhantomJS
*/
exports.run = function () {
var program = exports.exec.apply(null, arguments)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens right now if you pass bogus arguments to the binary? does it resolve or just hang?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, for example, --bogus makes it hang (without error messages). Is it better to be guarded like this?
http://phantomjs.org/api/command-line.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, i was just imagining that we'd reject the promise if the process existed before the promise resolved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, meant to say "if the process exited before the promise resolved". if it's not easy to do, don't worry about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. I'll have a look. Wait a bit. Thanks.

var isFirst = true
return new Promise(function (resolve) {
program.stdout.on('data', function () {
// This detects PhantomJS instance get ready.
if (!isFirst) return
isFirst = false
resolve(program)
})
})
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"test": "nodeunit --reporter=minimal test/tests.js && eslint ."
},
"dependencies": {
"es6-promise": "^3.2.1",
"extract-zip": "~1.5.0",
"fs-extra": "~0.30.0",
"hasha": "^2.2.0",
Expand All @@ -50,7 +51,8 @@
},
"devDependencies": {
"eslint": "2.7.0",
"nodeunit": "0.9.1"
"nodeunit": "0.9.1",
"webdriverio": "^4.2.3"
},
"bundledDependencies": [
"extract-zip",
Expand Down
27 changes: 26 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
var childProcess = require('child_process')
var fs = require('fs')
var path = require('path')
var webdriverio = require('webdriverio')
var phantomjs = require('../lib/phantomjs')
var util = require('../lib/util')


exports.testDownload = function (test) {
test.expect(1)
test.ok(fs.existsSync(phantomjs.path), 'Binary file should have been downloaded')
Expand Down Expand Up @@ -98,3 +98,28 @@ exports.testSuccessfulVerifyChecksum = function (test) {
test.done()
})
}

exports.testPhantomExec = function (test) {
test.expect(1)
var p = phantomjs.exec(path.join(__dirname, 'exit.js'))
p.on('exit', function (code) {
test.equals(code, 123, 'Exit code should be returned from phantom script')
test.done()
})
}

exports.testPhantomRun = function (test) {
test.expect(1)
var wdOpts = { desiredCapabilities: { browserName: 'phantomjs' } }
phantomjs.run('--webdriver=4444').then(function (p) {
webdriverio.remote(wdOpts).init()
.url('https://developer.mozilla.org/en-US/')
.getTitle().then(function (title) {
test.equals(title, 'Mozilla Developer Network', 'Page title')
})
.then(function () {
p.kill()
test.done()
})
})
}