I am trying to run karma tests on my angular project. I used yeoman to create the angular project. However when I am trying to run tests, I am getting following errors
grunt test
Running "clean:server" (clean) task
Running "concurrent:test" (concurrent) task
Running "coffee:dist" (coffee) task
Running "coffee:test" (coffee) task
Done, without errors.
Running "connect:test" (connect) task
Started connect web server on localhost:9000.
Running "karma:unit" (karma) task
INFO [karma]: Karma server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
WARN [watcher]: Pattern "/home/arpit/proj/proj1/views/test/mock/**/*.js" does not
match any file.
ERROR [launcher]: Cannot start Chrome
Error: spawn EACCES
INFO [launcher]: Trying to start Chrome again.
ERROR [launcher]: Cannot start Chrome
Error: spawn EACCES
INFO [launcher]: Trying to start Chrome again.
ERROR [launcher]: Cannot start Chrome
Error: spawn EACCES
Warning: Task "karma:unit" failed. Use --force to continue.
Aborted due to warnings.
I can run chrome from bash using same account successfully with which I am trying to run grunt test
Also, sudo grunt test is running fine.
On the Mac, it's important to set CHROME_CANARY_BIN
to binary file, not just to app package.
In my case, problem emerged from installing Chrome via brew cask, so I had to set
export CHROME_CANARY_BIN="/opt/homebrew-cask/Caskroom/google-chrome-canary/latest/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"
In most cases you have to set a environment variable "CHROME_BIN" to point at your chrome.exe
Windows:
set CHROME_BIN=C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
Mac 10.7.5
I was able to set it using the following
export CHROME_BIN=/Applications/Chrome.app/Contents/MacOS/Google\ Chrome
You get to the exec file by right clicking on the .app and selecting the Show Package Contents option. Follow this down to the executable and click it. It brought up the path in the terminal.
Related
I am facing issue while running grunt, it throwing me following issue:
ERROR [launcher]: PhantomJS2 failed 2 times (timeout). Giving up.
DEBUG [karma]: Run complete, exiting.
DEBUG [launcher]: Disconnecting all browsers
Warning: Task "karma:continuous" failed. Use --force to continue.
I tried to run npm install phantomjs
Which is successful. After that i run npm install as well but again getting above issue
This is because you will need to run the PhantomJS standalone version for karma to connect to it.
I would suggest a better option according to my opinion, which is to use karma-phantomjs2-launcher. You can get that by: npm install --save-dev karma-phantomjs2-launcher.
Please note that it is not a stable version. If it suits your requirement, you can use karma-phantomjs-launcher, which works perfectly fine. But it does not have support for PhantomJS 2.
I got solution for this, if we get this error so first need to check karm.conf.js file, there you have to search for key browsers and in that section you have to see whether PhantomJS2 is defined or not.It would be something like this:
browsers : [
'PhantomJS2'
],
In my case it was there but still i was getting error, i then checked my hosts file which you can find in C:\Windows\System32\drivers\etc if you are using windows machine and i placed
127.0.0.1 localhost and there i go.... its worked for me.
I am following the Angular Tutorial at https://docs.angularjs.org/tutorial/step_02.
I want to run the following command:
npm test
karma start karma.conf.js
And getting the following error:
> karma start karma.conf.js
WARN [watcher]: Pattern "/var/angularapp/angular-phonecat/app/**/*.module.js" does not match any file.
WARN [watcher]: Pattern "/var/angularapp/angular-phonecat/app/!(bower_components)/**/*!(.module|.spec).js" does not match any file.
WARN [karma]: No captured browser, open http://localhost:9876/
INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
INFO [launcher]: Starting browser Firefox
INFO [launcher]: Starting browser Chrome
ERROR [launcher]: Cannot start Firefox
INFO [launcher]: Trying to start Firefox again (1/2).
ERROR [launcher]: Cannot start Firefox
INFO [launcher]: Trying to start Firefox again (2/2).
17 08 2016 18:46:51.434:ERROR [launcher]: Cannot start Firefox
ERROR [launcher]: Firefox failed 2 times (cannot start). Giving up.
ERROR [launcher]: Cannot start Chrome
INFO [launcher]: Trying to start Chrome again (1/2).
ERROR [launcher]: Cannot start Chrome
[launcher]: Trying to start Chrome again (2/2).
[launcher]: Cannot start Chrome
ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up.
karma.config.js:
browsers: ['Firefox', 'Chrome'],
I installed Firefox and Chrome using the following commands:
sudo apt-install chromium-browser
sudo apt-install firefox
And set the env variables in ~/.bachrc:
export FIREFOX_BIN=/usr/bin/firefox
export CHROME_BIN=/usr/bin/chromium-browser
I even typed the following command, but did not help:
source ~/.bashrc
My node version is 0.12.15 and npm is 2.15.1. Please note that this is a vagrant box that I am talking about.
Please help :)
Someone suggested that I should use Phantomjs instead! So I did it and below is what I did exactly and made the test pass:
Install PhantomJS:
sudo npm install -g phantomjs-prebuilt
Update the env variable by adding the following line to ~/.bashrc:
export PHANTOMJS_BIN=/usr/bin/phantomjs
Add the following line to devdependencies in package.json file within the project:
"karma-phantomjs-launcher": "^0.2.0",
Add the following line to karma.config.js file:
plugins: [
...
'karma-phantomjs-launcher',
...
]
And finally change the browsers in karma.config.js to be:
browsers: ['PhantomJS'],
However, I still do not know why Chrome and Firefox did not work, and Phantomjs did the job. The way I view it is my vagrant box is just an Ubuntu machine, and should not be different than any other machines! It would be great if someone explains to me the reasons. And please consider that I am a newbie.
I've posted an answer in the related thread:
Karma - Chrome failed 2 times (cannot start). Giving up
Basically, for me chrome was not able to start because fonts were missing.
The tests weren't running for me either. I changed the following in karma.conf.js to so that Karma would only try to use Chrome:
browsers: ['Chrome'],
plugins: [
'karma-chrome-launcher',
'karma-jasmine'
]
I'm assuming that not having Firefox installed on my machine was causing Karma to fail.
Hi I'm trying to get the test script running on the angular tutorial (step 2, writing and running test part 1) but I'm stuck at trying to run the karma server but when I run the command in the GitBash terminal I get this error
John#JOHN-PC ~/angular-phonecat ((step-2))
$ ./scripts/test.sh
Starting Karma Server (http://karma-runner.github.io)
-------------------------------------------------------------------
INFO [karma]: Karma v0.10.2 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
ERROR [launcher]: Cannot start Chrome
Can not find the binary C:\Users\John\AppData\Local\Google\Chrome\Applic
ation\chrome.exe
Please set env variable CHROME_BIN
any idea what this means
here's the link to the tutorial http://docs.angularjs.org/tutorial/step_02
As Cristian Ternus had already pointed out you will have to set the correct value of the CHROME_BIN env variable. Dont copy the path as it is provided in the comment. You will have to find out where crome.exe is present on your machine. Please refer http://bardevblog.wordpress.com/2013/07/28/setting-up-angularjs-angular-seed-node-js-and-karma/ which is a nice tutorial about running angular test cases. Step 8 in the tutorial is addressing your issue.
Do you have Google Chrome installed? If not, install it.
If it's installed, is it in that path (C:\Users\John\AppData\Local\Google\Chrome\Applic
ation\chrome.exe)?
If not, set the environment variable CHROME_BIN to the path of chrome.exe.
I usually use browsers=['PhantomJS'] for running my specs with karma because it's faster than chrome and I find a bit annoying when chrome opens the browser in front of the window on every boot.
But when I have a bug and I need to debug, I change karma.conf.js browsers to ['Chrome'].
I would like to set this property when I start karma, something like: 'karma start --browsers=[Chrome]'.
Is this posible?
you just have to use the --browsers parameter and it'll overide your config file browsers part.
$ karma start config/testacular.conf.js --browsers Chrome
where Chrome is the name of the browser or a path to a binary.
For example, I have only a small Qt browser configured. if I use the line above, it runs Chrome instead.
INFO [karma]: Karma server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 24.0 (Linux)]: Connected on socket id DxbVJNX0jIoe1CbaWf9V
Chrome 24.0 (Linux): Executed 74 of 74 SUCCESS (0.443 secs / 0.232 secs)
...
or, for example, I have firefox beta, aurora and release in my home. I run beta with:
$ karma start config/testacular.conf.js --browsers ~/firefox/firefox
$ karma start --help
Karma - Spectacular Test Runner for JavaScript.
START - Start the server / do a single run.
Usage:
karma start [<configFile>] [<options>]
Options:
--port <integer> Port where the web server is running.
--runner-port <integer> Port where the server is listening for runner.
--auto-watch Auto watch source files and run on change.
--no-auto-watch Do not watch source files.
--log-level <disable | error | warn | info | debug> Level of logging.
--colors Use colors when reporting and printing logs.
--no-colors Do not use colors when reporting or printing logs.
--reporters List of reporters (available: dots, progress, junit).
--browsers List of browsers to start (eg. --browsers Chrome,ChromeCanary,Firefox).
--capture-timeout <integer> Kill browser if does not capture in given time [ms].
--single-run Run the test when browsers captured and exit.
--no-single-run Disable single-run.
--report-slower-than <integer> Report tests that are slower than given time [ms].
--help Print usage and options.
--version Print current version.
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.
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.
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.
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.
Are you using cmd.exe or Powershell?
Try manually adding the PHANTOMJS_BIN and point it to the phantomjs.exe, not the .cmd.