Given the e2e config:
module.exports = function(config) {
config.set({
basePath: '../',
files: [
'E2E/**/*.js'
],
autoWatch: false,
browsers: ['Chrome'],
frameworks: ['ng-scenario'],
singleRun: true,
proxies: {
'/': 'http://localhost:8000/'
},
plugins: [
'karma-junit-reporter',
'karma-chrome-launcher',
'karma-jasmine',
'karma-ng-scenario'
],
junitReporter: {
outputFile: 'test_out/e2e.xml',
suite: 'e2e'
},
urlRoot: '/_karma_/'
});
};
and the scenario:
'use strict';
/* http://docs.angularjs.org/guide/dev_guide.e2e-testing */
describe('Mailing App', function () {
it('should filter the phone list as user types into the search box', function () {
expect(true).toBe(true);
});
it('should filter the phone list as user types into the search box', function () {
expect(Element('foo').count()).toEqual(1);
});
describe('Sample Test', function () {
beforeEach(function () {
browser().navigateTo('../../Client/Views/Shared/_Layout.cshtml');
});
it('should filter the phone list as user types into the search box', function () {
pause();
expect(Element('.ng-binding')).toBeDefined();
});
});
});
The programs finds the 3 tests but does not pass or fail them, rather skips them.
Running the script (with windows 8.1, git bash) returns says:
Karma v0.12.1 server started atHTTP://localhost:9876/karma/"
starting Chrome Connected on Socket Chrome 31.0.1650 Executed 0 of 3
(skipped 3) Error
Any idea why tests that don't even need to traverse the site or look at the DOM etc can be found but can not be run?
Ok, not sure if this will help anyone but basically the issue was I didn't realise that angular-scenario.js is not part of the Karma test running suite, it is from before karma when tests were running with testacular.
angular-scenario.js was being included as part of the file includes with the */.js wildcards.
Once I changed it to no longer see that it seems to now be working, guess I would have expect some kind of conflicting functions or stuff not defined to be thrown, if classes were messing each other up.
Related
I am running Karma/Jasmine/Angular 2.0 tests on my development box. Just recently, Jasmine on my development box decided to start running my tests three times. Yes, exactly three times, every time.
On the first run, everything passes as expected. However, on the second and third pass, all of the same things fail. It always acknowledges that there are 7 tests, but runs 21, and 10 fails (first-grade math out the window)????
This also fails on Travis with SauceLabs. (Note: That links to an older build with 3 tests, but ran 9, and 5 fail???)
I have a screenshot, karma.conf.js file, and one suite which started this whole thing. Any help with be greatly appreciated.
Culprit [TypeScript] (Remove this and problem solved on my dev box):
Full source
describe('From the Conductor Service', () => {
let arr: Array<ComponentStatusModel> = null;
let svc: ConductorService = null;
beforeEach(() => {
arr = [/* Inits the array*/];
svc = new ConductorService();
});
describe('when it is handed a container to hold objects which need to be loaded', () => {
// More passing tests...
/// vvvvv The culprit !!!!!
describe('then when you need to access the container', () => {
beforeEach(() => {
svc.loadedContainer = arr;
});
it('it should always be available', () => {
assertIsLocalDataInTheService(arr, svc.loadedContainer);
});
});
/// ^^^^^ End of culprit !!!!!
});
// More passing tests...
});
Failing Tests:
Browser Screenshots:
Not sure if this is related, but before all of the errors happen, the Jasmine call stack is smaller (left, observe scrollbar). After the errors start, the stack just gets bigger with repeating calls to the same functions (right, observe scrollbar).
Suite Stack is Wrong:
In my test, the Nanobar and Conductor spec files are totally separate. However, you can see the suites array includes stuff from the Nanobar and Conductor specs. Somehow Jasmine mashed these two spec files together (after everything started failing), and resulted in my describe() statements not making any sense when published to the console.
Simplified karma.conf.js:
Full source
module.exports = function (config) {
config.set({
autoWatch: false,
basePath: '.',
browsers: ['Chrome'],
colors: true,
frameworks: ['jasmine'],
logLevel: config.LOG_INFO,
port: 9876,
reporters: ['coverage', 'progress'],
singleRun: true,
coverageReporter: {
// Code coverage config
},
files: [
// Loads everything I need to work
],
plugins: [
'karma-chrome-launcher',
'karma-coverage',
'karma-jasmine'
],
preprocessors: {
'app/**/*.js': ['coverage']
},
proxies: {
// Adjust the paths
}
})
}
Can you try refreshing your browser in your first assertion in each of your test files?
Try this:
browser.restart();
I had the same problem and this fixed it for me.
The first thing is these test run randomly. If you pass some data in any test case if you think you can resue that it is not possible.
You have to declare the data in before each so all test cases get data.
All test cases run independently.
If you are using array or object you must have to use this after deep cloning because array and object works on the reference. If you manipulate any value it will also change the original array.
In most of the cases if the test fails there may be an error of data you are passing in test cases.
I would try to debug this and pinpoint the exact cause.
Usually happens when I have redirection code or any reload code inside the functions I'm testing.
You can try adding an f to the prefix of describe and it (i.e. fdescribe and fit)
I am a newbie using protractor for angularjs app e2e testing.
I have the latest version of protractor setup and using Visual studio 2015 as the IDE.
The function browser.get() mentioned in my tests doesn't work and only opens up a browser window with "Data:,".
Here is my config.js file :
'use strict';
exports.config = {
directConnect: true,
chromeDriver: './node_modules/protractor/selenium/chromedriver.exe',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
},
// Spec patterns are relative to the current working directly when
// protractor is called.
specs: ['example_spec.js'],
framework: 'jasmine',
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
},
onPrepare: function () {
browser.driver.manage().window().maximize();
}
};
Here is my spec.js file :
describe('angularjs homepage', function () {
it('should have a title', function () {
browser.get('http://angularjs.org/');
expect(browser.getTitle()).toContain('AngularJS');
});
});
Am i missing out on anything? Please help! i have been trying to fix this since 3 days now.
The first thing,
chromeDriver: './node_modules/protractor/selenium/chromedriver.exe'
is not needed.Your web-driver manager manage it.
If your protractor version is >3.0 then install node > 4.0.
UPDATE :
Please fire following command :
webdriver-manager update
First attempt at using Protractor. I would like to be able to run multiple suites in succession.
I have an application that is one big angular form with different scenarios.
I have expected results for each scenario and would like to enter one command and run through each test.
I thought I could just use comma separated like:
protractor config.js --suite=rf1_breast, rf1_ovarian, rf1_pancreatic
But I am getting the error:
Error: more than one config file specified
Which is strange as there is only the one config file which is in the directory where I am running protractor.
Here is my config.js:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: { 'browserName': 'chrome' },
framework: 'jasmine2',
suites: {
rf1_breast: './rf1-ashkenazi-hboc/Breast/specs/*_spec.js',
rf1_ovarian: './rf1-ashkenazi-hboc/Ovarian/specs/*_spec.js',
rf1_bladder_fail: './rf1-ashkenazi-hboc/Bladder-expected-fail/specs/*_spec.js',
rf1_pancreatic: './rf1-ashkenazi-hboc/Pancreatic/specs/*_spec.js',
rf1_prostate: './rf1-ashkenazi-hboc/Prostate/specs/*_spec.js'
},
onPrepare: function() {
/* global angular: false, browser: false, jasmine: false */
browser.manage().window().setSize(1600, 1600);
// Disable animations so e2e tests run more quickly
var disableNgAnimate = function() {
angular.module('disableNgAnimate', []).run(['$animate', function($animate) {
$animate.enabled(false);
}]);
};
browser.addMockModule('disableNgAnimate', disableNgAnimate);
},
jasmineNodeOpts: { showColors: true }
};
Is there a better way around getting each scenario run?
Don't put spaces after commas:
protractor config.js --suite rf1_breast,rf1_ovarian,rf1_pancreatic
In your config.js
If you want to run the script as a suite comment out the spec line
// specs: ['src/com/sam/scriptjs/alertexamplespec.js']
give the suite name and location
suites: {
//Suite name and location give * to run all the specs or provide the name
smoke: ['./smoke/*.spec.js'],
endtoend: ['src/com/sam/scriptjs/*.spec.js'],
//Futhermore you can select few test specs
testfew: ['./smoke/editorder.spec.js','./smoke/cancelorder.spec.js']
},
then in cmd type protractor filenameconfig.js
Try to make your suite composed by multiple files. I Have a line for the test I currently work on and another with the entire suite of tests:
exports.config = {
allScriptsTimeout: 11000,
specs: [
'./e2e/**/1.Landing.Page.ts',
'./e2e/**/2.Confirmation.Page.ts',
'./e2e/**/3.PersonalData.Page.ts',
'./e2e/**/4.sms.Page.ts',
'./e2e/**/5.idCard.Page.ts'
],
}
This works for me.
i have inhereted a angular project that uses npm, grunt, bower ... and karma + jasmin.
i have been asked to setup some tests for the project using karma and jasmin.
karma has already been setup in the project but never used.
when i run 'grunt test' i get injection errors on all of the services, like the following.
Error: [$injector:unpr] Unknown provider: excelparserserviceProvider <- excelparserservice
http://errors.angularjs.org/1.2.6/$injector/unprp0=excelparserserviceProvider%20%3C-%20excelparserservice
there is already a karma.conf.js looking like this.
i havent changed anything in the karma.conf file, except adding some of the libaries that used in the project into the list under Files: [].
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: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/bower_components/angular-route/angular-route.js',
// i manually added the ones from here
'app/bower_components/jquery/dist/jquery.js',
'app/bower_components/geocoder-js/dist/geocoder.js',
'app/bower_components/js-xlsx/dist/xlsx.core.min.js',
'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
'app/bower_components/d3/d3.js',
'app/bower_components/angular-file-upload/angular-file-upload.js',
// to here.
'app/scripts/*.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.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: ['Chrome'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
the paths,
'app/scripts/*.js' --> leads to app.js and a config.js
'app/scripts/**/*.js' -->leads to all services controllers and directives
'test/mock/**/*.js' --> is non existent
'test/spec/**/*.js'--> contains all the test files
there are test files corresponding to every part of the applikation. Which i have been told has been auto generated. So i find it weird if they should contain the error. but the one related to the excpelparserservice injection error is.
'use strict';
describe('Service: excelparserservice', function () {
// load the service's module
beforeEach(module('batchUploadApp'));
// instantiate service
var Excelparserservice;
beforeEach(inject(function (_excelparserservice_) {
Excelparserservice = _excelparserservice_;
}));
it('should do something', function () {
expect(!!Excelparserservice).toBe(true);
});
});
the declaration of the service looks like this.
'use strict';
angular.module('batchUploadApp')
.service('ExcelParserService',
function ExcelParserService($q, ExcelvalidationService, GeoLocationService) {
the application in general works.
hope i my explication is usefull :)
thank you.
You define and register your service like this:
angular.module('batchUploadApp').service('ExcelParserService', ...
meaning that you register it under the name ExcelParserService. On the other hand, when you try to inject the service into you test, you use its name in lowercase:
beforeEach(inject(function (_excelparserservice_) {
Both names must match, thus the solution is to change the name of the parameter:
beforeEach(inject(function (_ExcelParserService_) {
I know this question has been asked many times, and I know that in most cases people are missing the angular-mocks.js file.
I'm running into the same issue, attempting to test a factory on a module. Unfortunately, I keep running into issues with the tests (why, Angular, oh why must you assume a window and document object?), which state that module is not defined. I'm at a loss. I've also tried using angular.mocks.module, but then I get a message saying Angular isn't defined. What am I doing wrong?
Of note, I'm using gulp as a task runner. My gulpfile (not yet perfect, tasks aren't linked):
var gulp = require('gulp'),
uglify = require('gulp-uglify'),
jshint = require('gulp-jshint'),
jasmine = require('gulp-jasmine'),
karma = require('gulp-karma'),
paths = {
scripts: "scripts/*.js",
spec: "spec/*.js",
dist: "dist"
};
gulp.task('prepare', function () {
return gulp.src(paths.scripts)
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(uglify())
.pipe(gulp.dest(paths.dist))
});
gulp.task('test', function () {
gulp.src([paths.scripts, paths.spec])
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}));
});
gulp.task('default', ['prepare', 'test']);
My karma.conf.js, generated by karma init:
// Karma configuration
// Generated on Fri Mar 14 2014 14:24:30 GMT-0400 (EDT)
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/angular/angular.min.js',
'./lib/angular-mocks/angular-mocks.js',
'./src/*.js',
'./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'],
// 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: false
});
};
And finally, my test suite (nothing's set up yet, if I can pass this hurdle, we'll be good):
/* Tests for memento.js. */
describe('memento core test suite', function () {
var memento;
beforeEach(module('Memento'));
beforeEach(function() {
inject(function(_memento_) {
memento = _memento_;
});
});
// Check functions.
// check to see if it has the expected function
it('should match expected interface', function () {
expect(angular.isFunction(memento.canUndo)).toBe(true);
expect(angular.isFunction(memento.canRedo)).toBe(true);
expect(angular.isFunction(memento.undo)).toBe(true);
expect(angular.isFunction(memento.redo)).toBe(true);
expect(angular.isFunction(memento.push)).toBe(true);
});
it('should initialize', function () {
this.fail(Error('Test not implemented'));
});
it('should push() a changed object', function () {
this.fail(Error('Test not implemented'));
});
it('should not push() an unchanged object', function () {
this.fail(Error('Test not implemented'));
});
it('should return original object on undo()', function () {
this.fail(Error('Test not implemented'));
});
it('should return modified object on redo()', function () {
this.fail(Error('Test not implemented'));
});
it('should not undo() if at beginning of stack', function () {
this.fail(Error('Test not implemented'));
});
it('should not redo() if at end of stack', function () {
this.fail(Error('Test not implemented'));
});
// TODO: Implement revert to original, clearing history.
//
// it('should return seed object on revert()', function () {
// this.fail(Error('Test not implemented'));
// });
// it('should clear the stack on clear()', function () {
// this.fail(Error('Test not implemented'));
// });
});
Does anyone see anything awry? I'm not sure if there's something really obvious I'm missing - I could use an extra set of eyes, or many. I originally thought I'd be able to run this as a simple Jasmine test suite without Karma, but due to Angular, that has problems. If I can't get this to work, I might just use npm's Angular package and alter my Angular module so that it supports CommonJS...
Thanks, everyone! Hope I'm not crazy.
Edit: I've included my devdependencies.
"devDependencies": {
"gulp": "~3.5.6",
"gulp-uglify": "~0.2.1",
"gulp-jshint": "~1.5.0",
"gulp-jasmine": "~0.2.0",
"angular": "~1.2.10",
"karma": "~0.12.0",
"gulp-karma": "0.0.4",
"karma-jasmine": "~0.2.2",
"karma-chrome-launcher": "~0.1.2"
}
The message stating that module/angular is not defined means that your angular-mocks.js file is not being loaded, despite the fact you have it listed in your karma.conf.js file.
The problem you're experiencing is gulp-karma ignoring your karma.conf.js files array. This happens when you pass a string or glob into gulp.src in the gulpfile.
To work around this, pass gulp.src a string for a bogus file, "./foobar" for instance, and this will cause the files array in the karma.conf.js file to be used instead.
gulp.task('test', function () {
gulp.src('./foobar')
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}));
});
Hope this helps!
Reference:
https://github.com/lazd/gulp-karma/issues/9
This is not the answer to your particular issue,
but I had a similar symptom in a very similar scenario.
However, the root cause was different,
so I'll share it here in case others find this post and they have the same issue I had.
In my case, because I introduced tests later in the game,
there was a mismatch between the versions of angular (1.4.7) and angular-mocks (1.6.9).
I found out this was the root cause
by using the debug of Karma in the browser.
Lowering the version of angular-mocks to match angular solved the issue.