| 1 | /** |
| 2 | * Nodeunit functional tests. Requires internet connection to validate phantom |
| 3 | * functions correctly. |
| 4 | */ |
| 5 | |
| 6 | var childProcess = require('child_process') |
| 7 | var fs = require('fs') |
| 8 | var path = require('path') |
| 9 | var webdriverio = require('webdriverio') |
| 10 | var phantomjs = require('../lib/phantomjs') |
| 11 | var util = require('../lib/util') |
| 12 | |
| 13 | exports.testDownload = function (test) { |
| 14 | test.expect(1) |
| 15 | test.ok(fs.existsSync(phantomjs.path), 'Binary file should have been downloaded') |
| 16 | test.done() |
| 17 | } |
| 18 | |
| 19 | |
| 20 | exports.testPhantomExecutesTestScript = function (test) { |
| 21 | test.expect(1) |
| 22 | |
| 23 | var childArgs = [ |
| 24 | path.join(__dirname, 'loadspeed.js'), |
| 25 | 'http://www.google.com/' |
| 26 | ] |
| 27 | |
| 28 | childProcess.execFile(phantomjs.path, childArgs, function (err, stdout) { |
| 29 | var value = (stdout.indexOf('msec') !== -1) |
| 30 | test.ok(value, 'Test script should have executed and returned run time') |
| 31 | test.done() |
| 32 | }) |
| 33 | } |
| 34 | |
| 35 | |
| 36 | exports.testPhantomExitCode = function (test) { |
| 37 | test.expect(1) |
| 38 | childProcess.execFile(phantomjs.path, [path.join(__dirname, 'exit.js')], function (err) { |
| 39 | test.equals(err.code, 123, 'Exit code should be returned from phantom script') |
| 40 | test.done() |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | |
| 45 | exports.testBinFile = function (test) { |
| 46 | test.expect(1) |
| 47 | |
| 48 | var binPath = process.platform === 'win32' ? |
| 49 | path.join(__dirname, '..', 'lib', 'phantom', 'phantomjs.exe') : |
| 50 | path.join(__dirname, '..', 'bin', 'phantomjs') |
| 51 | |
| 52 | childProcess.execFile(binPath, ['--version'], function (err, stdout) { |
| 53 | test.equal(phantomjs.version, stdout.trim(), 'Version should be match') |
| 54 | test.done() |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | |
| 59 | exports.testCleanPath = function (test) { |
| 60 | test.expect(5) |
| 61 | test.equal('/Users/dan/bin', phantomjs.cleanPath('/Users/dan/bin:./bin')) |
| 62 | test.equal('/Users/dan/bin:/usr/bin', phantomjs.cleanPath('/Users/dan/bin:./bin:/usr/bin')) |
| 63 | test.equal('/usr/bin', phantomjs.cleanPath('./bin:/usr/bin')) |
| 64 | test.equal('', phantomjs.cleanPath('./bin')) |
| 65 | test.equal('/Work/bin:/usr/bin', phantomjs.cleanPath('/Work/bin:/Work/phantomjs/node_modules/.bin:/usr/bin')) |
| 66 | test.done() |
| 67 | } |
| 68 | |
| 69 | exports.testBogusReinstallLocation = function (test) { |
| 70 | util.findValidPhantomJsBinary('./blargh') |
| 71 | .then(function (binaryLocation) { |
| 72 | test.ok(!binaryLocation, 'Expected link to fail') |
| 73 | test.done() |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | exports.testSuccessfulReinstallLocation = function (test) { |
| 78 | util.findValidPhantomJsBinary(path.resolve(__dirname, '../lib/location')) |
| 79 | .then(function (binaryLocation) { |
| 80 | test.ok(binaryLocation, 'Expected link to succeed') |
| 81 | test.done() |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | exports.testBogusVerifyChecksum = function (test) { |
| 86 | util.verifyChecksum(path.resolve(__dirname, './exit.js'), 'blargh') |
| 87 | .then(function (success) { |
| 88 | test.ok(!success, 'Expected checksum to fail') |
| 89 | test.done() |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | exports.testSuccessfulVerifyChecksum = function (test) { |
| 94 | util.verifyChecksum(path.resolve(__dirname, './exit.js'), |
| 95 | '217b7bccebefe5f5e267162060660b03de577867b6123ecfd3b26b5c6af2e92b') |
| 96 | .then(function (success) { |
| 97 | test.ok(success, 'Expected checksum to succeed') |
| 98 | test.done() |
| 99 | }) |
| 100 | } |
| 101 | |
| 102 | exports.testPhantomExec = function (test) { |
| 103 | test.expect(1) |
| 104 | var p = phantomjs.exec(path.join(__dirname, 'exit.js')) |
| 105 | p.on('exit', function (code) { |
| 106 | test.equals(code, 123, 'Exit code should be returned from phantom script') |
| 107 | test.done() |
| 108 | }) |
| 109 | } |
| 110 | |
| 111 | exports.testPhantomRun = function (test) { |
| 112 | test.expect(1) |
| 113 | var wdOpts = { desiredCapabilities: { browserName: 'phantomjs' } } |
| 114 | phantomjs.run('--webdriver=4444').then(function (p) { |
| 115 | webdriverio.remote(wdOpts).init() |
| 116 | .url('https://developer.mozilla.org/en-US/') |
| 117 | .getTitle().then(function (title) { |
| 118 | test.equals(title, 'Mozilla Developer Network', 'Page title') |
| 119 | }) |
| 120 | .then(function () { |
| 121 | p.kill() |
| 122 | test.done() |
| 123 | }) |
| 124 | }) |
| 125 | } |
| 126 | |
| 127 | exports.testPhantomRunError = function (test) { |
| 128 | test.expect(1) |
| 129 | phantomjs.run('--bogus').then(function () { |
| 130 | test.ok(false, 'Expected not to start') |
| 131 | test.done() |
| 132 | }, function (err) { |
| 133 | test.equal('Error: Unknown option: bogus\n', err.message) |
| 134 | test.done() |
| 135 | }) |
| 136 | } |