Using PhantomJS with Karma (Win7 x64)

Does anyone have a simple getting started guide on setting up Karma to use PhantomJS?

Using the phonecat sample, I have Karma working with Chrome perfectly, and although the Karma docs mention PhantomJS (which I just installed), I cannot figure out how to modify the configuration file to run it.

I tried putting PhantomJS into the browsers array of testacular.conf.js, but I get:

  { [Error: spawn OK] code: 'OK', errno: 'OK', syscall: 'spawn' } 

I think it launches OK, but it seems to me that (like PhantomJS noob) another command line is needed. I also downloaded phantomjs-launcher , but it is unclear how to use this.

(I am using the 64-bit version of Windows 7, if that matters.)

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 did not start at all, so to circumvent environmental issues, I have since made corrections to my configuration like this:

 browsers = ['Chrome','%USERPROFILE%\\AppData\\Roaming\\npm\\phantomjs.cmd']; 

where %userprofile% expands, which seems to start 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) 

Now this error appears from PhantomJS.exe.

+6
source share
4 answers

Install PhantomJS first with npm:

npm install -g phantomjs

You may then need to specify the location of the PhantomJS executable for karma. Installing npm will tell you where it places the executable. For me, while working with 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'` 

These two steps, plus adding PhantomJS to the browsers entry, were enough to make karma successfully call Phantom and run all my tests.

+11
source

Use the PhantomJS launcher and set the env PHANTOMJS_BIN to the correct location of the phantomjs binary. This is phantomjs.exe in windows, not a .cmd file (cmd file is just an npm shell on windows).

In your code, you use a launch script browser (a custom shell script to launch a browser). It is possible, but the script should accept a single argument, which is the URL that it should open. PhantomJS does not behave this way.

+3
source

I saw this error when using relative path in node function child_process.spawn () -

 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 the node script was called from the Windows command prompt, but not when the node script was called in Windows PowerShell.

+1
source

Are you using cmd.exe or Powershell? Try manually adding PHANTOMJS_BIN and point it to phantomjs.exe and not to .cmd.

0
source

Source: https://habr.com/ru/post/944052/


All Articles