I'm integrating Jasmine and karma for unit testing my ionic app.
I'm following this blog.
When i run the karma start command i keep getting this result.
23 02 2017 16:39:18.508:WARN [karma]: No captured browser, open
23 02 2017 16:39:18.517:INFO [karma]: Karma v1.5.0 server started at http://0.0.0.0:9876/
23 02 2017 16:39:18.517:INFO [launcher]: Launching browser Chrome with unlimited concurrency
23 02 2017 16:39:18.536:INFO [launcher]: Starting browser Chrome
23 02 2017 16:40:18.537:WARN [launcher]: Chrome have not captured in 60000 ms, killing.
23 02 2017 16:40:18.910:INFO [launcher]: Trying to start Chrome again (1/2).
23 02 2017 16:41:18.911:WARN [launcher]: Chrome have not captured in 60000 ms, killing.
23 02 2017 16:41:19.357:INFO [launcher]: Trying to start Chrome again (2/2).
Tried with PhantomJS also
Same error
Launching browser PhantomJS with unlimited concurrency
23 02 2017 17:44:30.214:INFO [launcher]: Starting browser PhantomJS
23 02 2017 17:45:30.215:WARN [launcher]: PhantomJS have not captured in 60000 ms, killing.
23 02 2017 17:45:30.238:INFO [launcher]: Trying to start PhantomJS again (1/2).
23 02 2017 17:46:30.239:WARN [launcher]: PhantomJS have not captured in 60000 ms, killing.
23 02 2017 17:46:30.246:INFO [launcher]: Trying to start PhantomJS again (2/2).
23 02 2017 17:47:30.247:WARN [launcher]: PhantomJS have not captured in 60000 ms, killing.
23 02 2017 17:47:30.254:ERROR [launcher]: PhantomJS failed 2 times (timeout). Giving up.
Config
// Karma configuration
// Generated on Thu Feb 23 2017 10:39:27 GMT+0530 (IST)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'../www/lib/ionic/js/ionic.bundle.js',
'../www/js/**/*.js',
'../tests/unit-tests/**/*.js',
'../node_modules/angular-mocks/angular-mocks.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
// browsers: ['PhantomJS'],
browsers: ['Chrome'],
// customLaunchers: {
// Chrome_no_sandbox: {
// base: 'Chrome',
// flags: ['--no-sandbox']
// }
// },
plugins:[
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-chrome-launcher'
],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
})
}
Please help
Thanks!
This is the top Google result for this question, so even though it's old I hope my answer helps someone.
In our case the issue was caused by multiple files importing from the wrong library. This seems strange since you would think that Karma would throw an error rather than just refusing to open Chrome, but altering our import statements got us past the issue.
import { ChangeDetectionStrategy } from "#angular/core";
NOT AS OUR FILES HAD IT: import { ChangeDetectionStrategy } from "#angular/compiler/src/core";
This says you should try add the below code to karma.conf.js file:
browsers: ['Chrome_no_sandbox'],
customLaunchers: {
Chrome_no_sandbox: {
base: 'Chrome',
flags: ['--no-sandbox']
}
}
Try increasing browserDisconnectTolerance. Reasons could be different, from the wrong path in karma.config to some flaky npm run.
Related
I'm encountering an issue when running tests in my CI environment. The configuration works fine on MacOS 10.11.6 (El Capitan), with Node v6.0.0 / npm 3.8.6, but on a TeamCity 9.1.6 agent running Windows with Node v4.2.2 / npm 2.14.7, it fails miserably with an error of PhantomJS have not captured in 60000 ms.
Here's an example stacktrace on TeamCity:
[08:58:40][exec] 02 11 2016 08:58:41.095:DEBUG [config]: autoWatch set to false, because of singleRun
[08:58:40][exec] 02 11 2016 08:58:41.102:DEBUG [plugin]: Loading plugin karma-jasmine.
[08:58:40][exec] 02 11 2016 08:58:41.107:DEBUG [plugin]: Loading plugin karma-chrome-launcher.
[08:58:40][exec] 02 11 2016 08:58:41.121:DEBUG [plugin]: Loading plugin karma-firefox-launcher.
[08:58:40][exec] 02 11 2016 08:58:41.128:DEBUG [plugin]: Loading plugin karma-phantomjs-launcher.
[08:58:40][exec] 02 11 2016 08:58:41.263:DEBUG [web-server]: Instantiating middleware
[08:58:41][exec] 02 11 2016 08:58:41.595:INFO [karma]: Karma v1.3.0 server started at http://localhost:9877/
[08:58:41][exec] 02 11 2016 08:58:41.596:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
[08:58:41][exec] 02 11 2016 08:58:41.616:INFO [launcher]: Starting browser PhantomJS
[08:58:41][exec] 02 11 2016 08:58:41.618:DEBUG [temp-dir]: Creating temp dir at E:\TCA\3\temp\buildTmp\karma-47802965
[08:58:41][exec] 02 11 2016 08:58:41.631:DEBUG [launcher]: E:\TCA\3\work\f900f2fe23d6c9b0\Preview\node_modules\phantomjs-prebuilt\lib\phantom\bin\phantomjs E:\TCA\3\temp\buildTmp\karma-47802965/capture.js
[08:59:41][exec] 02 11 2016 08:59:41.621:WARN [launcher]: PhantomJS have not captured in 60000 ms, killing.
[08:59:43][exec] 02 11 2016 08:59:43.624:WARN [launcher]: PhantomJS was not killed in 2000 ms, sending SIGKILL.
[08:59:45][exec] 02 11 2016 08:59:45.627:WARN [launcher]: PhantomJS was not killed by SIGKILL in 2000 ms, continuing.
[08:59:45][exec] 02 11 2016 08:59:45.628:DEBUG [launcher]: Process PhantomJS exited with code -1
[08:59:45][exec] 02 11 2016 08:59:45.629:DEBUG [temp-dir]: Cleaning temp dir E:\TCA\3\temp\buildTmp\karma-47802965
[08:59:45][exec] 02 11 2016 08:59:45.637:DEBUG [launcher]: PhantomJS failed (timeout). Not restarting.
[08:59:45][exec] 02 11 2016 08:59:45.638:DEBUG [karma]: Run complete, exiting.
[08:59:45][exec] 02 11 2016 08:59:45.639:DEBUG [launcher]: Disconnecting all browsers
[08:59:45][exec] Warning: Task "karma:unit" failed. Use --force to continue.
[08:59:45][exec]
[08:59:45][exec] Aborted due to warnings.
[08:59:45][exec] npm ERR! Test failed. See above for more details.
Here's a snippet of package.json:
"devDependencies": {
"bower": "latest",
"grunt": "^1.0.1",
"grunt-cli": "^1.2.0",
"grunt-contrib-jshint": "^1.0.0",
"grunt-contrib-watch": "^1.0.0",
"grunt-istanbul-coverage": "^0.1.4",
"grunt-karma": "^2.0.0",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-firefox-launcher": "^1.0.0",
"karma-jasmine": "^1.0.2",
"karma-phantomjs-launcher": "^1.0.2",
"jasmine-core": "^2.5.2",
"matchdep": "^1.0.1"
},
"scripts": {
"postinstall": "node ./node_modules/bower/bin/bower install",
"test": "node ./node_modules/.bin/grunt default"
}
And here's a snippet of karma.conf.js:
module.exports = function (config) {
'use strict';
config.set({
frameworks: ['jasmine'],
plugins: [
'karma-jasmine',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-phantomjs-launcher'
],
files: [
<...>
],
exclude: [],
preprocessors: {
},
reporters: ['progress']
port: 9877,
runnerPort: 9101,
colors: true,
logLevel: config.LOG_DEBUG,
autoWatch: true,
browsers: ['PhantomJS'],
captureTimeout: 60000,
singleRun: true
});
Issue solved! In our CI environment, we aren't getting the node modules by running npm install - we are zipping the node_modules directory locally and adding it to our source code.
Zipping it on a Mac and exploding it on Windows and attempting to use those modules, especially phantomjs-prebuilt, is the reason why it wasn't capturing.
I zipped it from Windows instead and it worked perfectly.
I've had the PhantomJS have not captured in 60000 ms, killing problem locally and after some experimenting it turned out that it may have been caused by:
Clash between PhantomJS available from classpath vs
phatomjs-prebuilt downloaded to project's node_modules. I just removed the globally available one.
Missing PHANTOMJS_CDNURL environment (system) variable, pointing to a maven repository, check where to get PhantomJS.
I am using Karma and PhantomJS with Jasmine to test my AngularJS. When I run my gulp test locally it works fine, launches PhantomJS and runs all my tests, but when the tests get run on the build server it fails with the following errors:
Note that I am using VSTS and using their built in cloud build system (hosted agent). Having said that its pretty much a task runner and I only use it to run gulp tasks (build, test etc) on checkin
[32m13 07 2016 15:21:32.260:INFO [karma]: [39mKarma v1.1.1 server started at http://localhost:9876/
[32m13 07 2016 15:21:32.262:INFO [launcher]: [39mLaunching browser PhantomJS with unlimited concurrency
[32m13 07 2016 15:21:32.284:INFO [launcher]: [39mStarting browser PhantomJS
[33m13 07 2016 15:22:32.285:WARN [launcher]: [39mPhantomJS have not captured in 60000 ms, killing.
[33m13 07 2016 15:22:34.287:WARN [launcher]: [39mPhantomJS was not killed in 2000 ms, sending SIGKILL.
[33m13 07 2016 15:22:36.288:WARN [launcher]: [39mPhantomJS was not killed by SIGKILL in 2000 ms, continuing.
My Karma config is:
var configuration = {
basePath: "",
files: listFiles(),
singleRun: true,
autoWatch: false,
//usePolling: true,
//transports: ["xhr-polling", "jsonp-polling"],
frameworks: ["jasmine", "angular-filesort"],
angularFilesort: {
whitelist: [path.join(conf.paths.src, "/**/!(*.html|*.spec|*.mock).js")]
},
ngHtml2JsPreprocessor: {
stripPrefix: "src/",
moduleName: "veriApp"
},
browsers: ["PhantomJS"],
//browsers: ["Chrome"],
plugins: [
"karma-chrome-launcher",
"karma-phantomjs-launcher",
"karma-angular-filesort",
"karma-jasmine",
"karma-ng-html2js-preprocessor"
],
preprocessors: {
"src/**/*.html": ["ng-html2js"]
}
};
Here are the relevant parts of my package.json. Note that an NPM install is performed before the test is run
"karma": "1.1.1",
"karma-angular-filesort": "1.0.0",
"karma-jasmine": "1.0.2",
"karma-ng-html2js-preprocessor": "1.0.0",
"phantomjs-prebuilt": "2.1.4",
"karma-phantomjs-launcher": "1.0.0",
"karma-chrome-launcher": "1.0.1",
I've tried following advice from various answers regarding this but none seem to work! Perhaps I am missing something?
So... the solution is pretty easy but took a while to find:
Under the Variables tab of the build setup, add an environment variable:
PHANTOMJS_BIN C:\NPM\Modules\PhantomJS.cmd
Thanks to this link and this link.
karma config errors
I'm trying to configure my test environment, and getting the following bugs when I try to run karma.
karma start
06 10 2015 13:13:36.859:DEBUG [plugin]: Loading plugin karma-jasmine.
06 10 2015 13:13:36.917:DEBUG [karma]: List of files has changed, trying to execute
06 10 2015 13:13:36.918:WARN [karma]: No captured browser, open http://localhost:9876/
06 10 2015 13:13:36.922:DEBUG [watcher]: Watching ".../representative-summary/node_modules/angular"
06 10 2015 13:13:36.922:DEBUG [watcher]: Watching ".../representative-summary/node_modules/angular-resource"
06 10 2015 13:13:36.923:DEBUG [watcher]: Watching ".../representative-summary/spec"
06 10 2015 13:13:36.927:WARN [launcher]: Can not load "karma-chrome-launcher", it is not registered!
Perhaps you are missing some plugin?
06 10 2015 13:13:36.927:WARN [launcher]: Can not load "karma-firefox-launcher", it is not registered!
Perhaps you are missing some plugin?
06 10 2015 13:13:36.928:WARN [launcher]: Can not load "karma-safari-launcher", it is not registered!
Perhaps you are missing some plugin?
06 10 2015 13:13:36.928:WARN [launcher]: Can not load "karma-ie-launcher", it is not registered!
Perhaps you are missing some plugin?
06 10 2015 13:13:36.928:WARN [launcher]: Can not load "karma-phantomjs-launcher", it is not registered!
Perhaps you are missing some plugin?
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
{pattern: 'node_modules/angular/*.js', included: true},
{pattern: 'node_modules/angular-resource/*.js', included: true},
{pattern: 'spec/*.spec.js', included: true}
],
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_DEBUG,
//config.LOG_INFO,
autoWatch: true,
browsers: [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-safari-launcher',
'karma-phantomjs-launcher'
],
plugins: ['karma-jasmine'],
singleRun: false
})
};
I'm on OSX Yosemite 10.10.5, and yes there is a standalone test in there "expect true to be true" just to get it up and running by itself. The app I am writing is Angular, but I haven't touched any of the tests for that yet. I want just the basic test runner to be working right first.
Any help would be great. Thanks.
Browsers in karma come in English format (Chrome, Firefox, PhantomJS), not as their respective plugin names. The plugins just have to be installed with npm install --save.
ALSO: Karma will "magically" load any library in your node_modules directory starting with karma-, so you don't need to declare plugins: at all in your configuration file.
I'm running unit tests in my angular project, and this works fine if I run it with Chrome but, not so much and I use PhantomJS
I have a karma.config.js file like this:
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'lib/*.js',
'web/js/*.js',
'test/spec/*spec.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'junit'],
// the default configuration
junitReporter: {
outputFile: 'testResult/test-results.xml',
suite: ''
},
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
});
};
and like this (with Chrome) works
Running "karma:unit" (karma) task
INFO [karma]: Karma v0.10.10 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 36.0.1985 (Windows 7)]: Connected on socket oi7YLBSbpVbZSiVI_BPs
Chrome 36.0.1985 (Windows 7): Executed 8 of 8 SUCCESS (0.238 secs / 0.019 secs)
but if I change the browser (to PhantomJS) gives an error
Running "karma:unit" (karma) task
INFO [karma]: Karma v0.10.10 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Windows 7)]: Connected on socket Uy390TQWuDGR2pCi_GRy
PhantomJS 1.9.7 (Windows 7) ERROR
TypeError: '[object NodeList]' is not a valid argument for
'Function.prototype.apply' (evaluating 'push.apply')
at C:/Users/Lino Simões/Documents/bitbucket/jenkins-test/lib/angular.js: 2594
PhantomJS 1.9.7 (Windows 7): Executed 0 of 0 ERROR (0.131 secs / 0 secs)
Warning: Task "karma:unit" failed. Used --force, continuing.
so my question is, why does this work with Chrome but not with PhantomJs and what can I change to make it work with PhantomJS
The '[object NodeList]' is not a valid argument issue, when running in PhantomJS 1.x, is already fixed in angular.js v1.3.0-beta.14, see #7851
Try upgrading the angular.js to v1.3.0-beta.14 or newer.
PS. Another solution would be using PhantomJS 2
I am trying to test
In the grunt file
karma: {
e2e: {
configFile: 'karma-e2e.conf.js',
singleRun: true
}
},
Karma file is basically what Yeoman generated for me. Only change is I swithed Chrome for PhantomJS.
karma-e2e.conf.js:
// Karma configuration
// http://karma-runner.github.io/0.10/config/configuration-file.html
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['ng-scenario'],
// list of files / patterns to load in the browser
files: [
'test/e2e/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
// Uncomment the following lines if you are using grunt's server to run the tests
// proxies: {
// '/': 'http://localhost:9000/'
// },
// URL root prevent conflicts with the site root
// urlRoot: '_karma_'
});
};
I have a very basic test I try to run
describe('E2E: Testing Routes:', function () {
'use strict';
beforeEach(function() {
browser().navigateTo('/');
});
it('test', function() {
browser().navigateTo('#/');
expect(browser().location().path()).toBe("/list_persons");
});
})
This is the output from the test. I googled this error and it suggested that I add a "sleep" in my test. That does not sound like a solution for me.
> Running "karma:e2e" (karma) task
INFO [karma]: Karma v0.12.0 server started at http://local:8080/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Linux)]: Connected on socket i0eExVxerWfwrirxJXue with id 46269744
PhantomJS 1.9.7 (Linux): Executed 0 of 1 SUCCESS (0 secs / 0 secs)
PhantomJS 1.9.7 (Linux) E2E: Testing Routes: should jump to the /videos path when / is accessed FAILED
TypeError: 'undefined' is not a function (evaluating '$document.injector()')
PhantomJS 1.9.7 (Linux): Executed 1 of 1 (1 FAILED) ERROR (0.068 secs / 0.077 secs)
Warning: Task "karma:e2e" failed. Use --force to continue.
Can anyone help me. What I want to do is to run e2e tests from grunt in PhantomJS.It does not have to be on a listener for filechanges. One simple run on "grunt test" is perfect.
Edit: I changed files to include
files: [
'app/vendor/scripts/angular.js',
'app/vendor/scripts/angular-mocks.js',
'app/vendor/scripts/angular-route.js',
'app/vendor/scripts/jstd-scenario-adapter-config.js',
'app/vendor/scripts/jstd-scenario-adapter.js',
'app/vendor/scripts/angular-scenario.js',
'test/e2e/**/*.js'
],
Now getting this:
Running "karma:e2e" (karma) task
INFO [karma]: Karma v0.12.0 server started at http://localhost:8080/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Linux)]: Connected on socket ZbZbabDGNCtvVa4Z-LQk with id 99494524
PhantomJS 1.9.7 (Linux): Executed 0 of 0 ERROR (0.036 secs / 0 secs)
Warning: Task "karma:e2e" failed. Use --force to continue.
You have to add this:
singleRun: true,
To your karma config section in your grunt file.
Details:
https://github.com/karma-runner/grunt-karma