Using PhantomJS with Karma (Win7 x64)
Solution 1
First, install PhantomJS using npm:
npm install -g phantomjs
You may then need to specify the location of the PhantomJS executable for karma. The npm install will tell you where it put the executable. For me, running karma in Git Bash, I added the following to ~/.profile:
export PHANTOMJS_BIN ='C:/Users/JohnSmith/AppData/Roaming/npm/node_modules/phantomjs/lib/phantom/phantomjs.exe'`
Those two steps, plus adding PhantomJS to the browsers
entry, were sufficient to get karma to successfully invoke Phantom and run all of my tests.
Solution 2
Use PhantomJS launcher and set env PHANTOMJS_BIN
to the correct location of your phantomjs binary. That is phantomjs.exe
on windows, not the .cmd
file (the cmd file is just a npm wrapper on windows).
In your code, you are using a script browser launcher (a custom shell script to launch a browser). That is possible, but the script has to accept a single argument which is the url that it should open. PhantomJS does not behave like that.
Solution 3
I was seeing this error when using a relative path in node's child_process.spawn() function --
var spawn = require('child_process').spawn;
var child = spawn('phantomjs', ['./suspendmonitors.js']);
The solution for me was to use the absolute path:
var spawn = require('child_process').spawn;
var child = spawn('phantomjs', ['C:/Users/kkhalsa/workspace/misc_scripts/phantomjs/suspendmonitors.js']);
For some reason, the relative path worked when calling the node script from a windows command prompt, but didn't when invoking the node script in Windows powershell.
cirrus
Updated on June 14, 2022Comments
-
cirrus almost 2 years
Does anyone have a simple getting started guide on how to configure Karma to use PhantomJS?
Using the phonecat sample, I have Karma running with Chrome fine and although the Karma docs mention PhantomJS (which I now have installed) I can't figure out how to amend the config file to get it to run.
I've tried putting PhantomJS in the the
browsers
array of testacular.conf.js but I get;{ [Error: spawn OK] code: 'OK', errno: 'OK', syscall: 'spawn' }
Which I think means it's launching OK but it appears to me that (as a PhantomJS noob) it requires a different command line. I've also downloaded phantomjs-launcher but it's not obvious how to use that.
(I'm running Windows 7 64-Bit if that makes a difference.)
test.bat
@echo off REM Windows script for running unit tests REM You have to run server and capture some browser first REM REM Requirements: REM -NodeJS (http://nodejs.org/) REM -Testacular (npm install -g karma) set BASE_DIR= % ~dp0 karma start "%BASE_DIR%\..\config\testacular.conf.js" %*
testacular.conf.js
basePath = '../'; files =[ JASMINE, JASMINE_ADAPTER, 'app/lib/angular/angular.js', 'app/lib/angular/angular-*.js', 'test/lib/angular/angular-mocks.js', 'app/js/**/*.js', 'test/unit/**/*.js' ]; autoWatch = true; browsers =['Chrome', 'phantomjs']; junitReporter = { outputFile: 'test_out/unit.xml', suite: 'unit' };
According to procmon.exe PhantomJS wasn't launching at all, so to circumvent environmental issues, I've since amended my config thus;
browsers = ['Chrome','%USERPROFILE%\\AppData\\Roaming\\npm\\phantomjs.cmd'];
where
%userprofile%
is expanded, which seems to launch it, but now I get this;INFO [launcher]: Starting browser %USERPROFILE%\AppData\Roaming\npm\phantomjs.cmd ERROR [launcher]: Cannot start %USERPROFILE%\AppData\Roaming\npm\phantomjs.cmd Can't open 'http://localhost:9876/?id=16572367' events.js:72 throw er; // Unhandled 'error' event ^ Error: spawn OK at errnoException (child_process.js:975:11) at Process.ChildProcess._handle.onexit (child_process.js:766:34)
That error seems to be coming from PhantomJS.exe now.
-
cirrus almost 11 yearsNo luck. I missed the npm install step previously, I'd downloaded it instead so I thought that might be it. The first time it ran though it complained of a port collision at 9100 but every time after that I just got the same error as before.
-
S McCrohan almost 11 yearsShow us your karma config file, I guess?
-
cirrus almost 11 yearsAdded. Nothing special there I don't think?
-
Vojta over 10 yearsAlso note that in the upcoming version of Karma (0.10), launchers are separate plugins and karma-phantomjs-launcher uses phantomjs installed from NPM, so you actually don't need to do anything and it should just work, even on the great OS called Windows ;-)