Testing async with karma - karma-runner

I am trying to set up some async tests using karma and jasmine. I am clearly making a very stupid mistake but I need it pointing out to me. After simplifying as much as possible I have the following:
package.json
{
"name": "newtest",
"version": "0.0.0",
"scripts": {
"test": "karma start karma.conf.js"
},
"devDependencies": {
"karma": "^0.12.28",
"karma-chrome-launcher": "^0.1.5",
"karma-jasmine": "^0.2"
}
}
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'tests/**/*.js'
],
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome'],
singleRun: true
});
};
tests/dummy.spec.js
describe("Testing async", function() {
it('should fail this test', function(done) {
setTimeout(function(){
expect(1).toBe(2);
done();
}, 1000);
});
it('should not fail this test', function(done) {
done();
});
});
and I am getting the following:
npm test
> newtest#0.0.0 test /home/mark/Projects/newtest
> karma start karma.conf.js
INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 39.0.2171 (Linux)]: Connected on socket T7j6LvNAwvS89wUdymCb with id 16891024
Chrome 39.0.2171 (Linux) Testing async should not fail this test FAILED
TypeError: undefined is not a function
at null.<anonymous> (/home/mark/Projects/newtest/tests/dummy.spec.js:12:5)
Chrome 39.0.2171 (Linux): Executed 2 of 2 (1 FAILED) (0.007 secs / 0.005 secs)
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
So the test that I think should fail is passing fine, and vice-versa. Can someone point me to my error(s)?

I guess the first one fail because when the timeout is reach the test is allready finish, so both doesn't work.
It's has you'r not using jasmine 2.
The syntax seems good to me, what I can tell you is the difference with my configuration (wich is working) :
I have put karma-jasmine in dependencies with a ~ :
"dependencies": {
...
"karma-jasmine": "~0.2.0"
},
I'm using PhantomJS :
browsers:['PhantomJS'],

I don't know why I am having the problem, but I have tried on another machine and it works as expected.

Related

Protractor tests are passing but didn't execute the tests

I am new to java script protractor cucumber framework.
I can see similar issue raised here an year ago but this was fixed in a protractor release an year ago as well. hence raising a new question.
I am using latest version of cucumber, protractor, protractor-cucumber-framework, chai, chai-as-promised. I am using visual studio code as editor.
Node Version is 6.11.4. Please look at the package.json below for all version details.
When I execute my tests, it finishes of in less than a second with a pass result but it just launches the browser and then doesn't do anything in the browser.
Please look at the files I am using and suggest me where is the problem. I am not sure whether this is a bus in the latest version of protractor.
protractor.conf.js:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['features/*.feature'],
getPageTimeout: 60000,
allScriptsTimeout: 500000,
baseURL: 'http://www.protractortest.org/testapp/ng1/#/form',
framework: 'custom',
frameworkPath: require.resolve('protractor-cucumber-framework'),
capabilities: {
'browserName': 'chrome'
},
cucumberOpts: {
require: [ './features/step_definitions/*.steps.js' ],
tags: false,
profile: false,
'no-source': true
}
};
package.json:
{
"name": "protractor_test",
"version": "1.0.0",
"description": "",
"main": "protractor.conf.js",
"dependencies": {
"protractor": "^5.2.0",
"cucumber": "^3.1.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"#types/chai-as-promised": "^7.1.0",
"#types/cucumber": "^2.0.4",
"#types/protractor": "^4.0.0"
},
"devDependencies": {
"protractor-cucumber-framework": "^4.1.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
test.feature:
Feature: Running Cucumber with Protractor
As a user of Protractor
I should be able to use Cucumber
In order to run my E2E tests
Scenario: Login to myApp
Given I go to Login page
When I login with "username" and "pwd"
Then "You are logged in as ADMIN" is displayed
test.steps.js:
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
var {defineSupportCode} = require('cucumber');
var expect = chai.expect;
chai.use(chaiAsPromised);
defineSupportCode(function({Given, When, Then}) {
Given('I go to DSH Login page', function () {
browser.get("http://localhost/myapp");
expect(element(by.buttonText('Login')).isDisplayed()).to.eventually.equal(true);
});
When('I login with {string} and {string}', function (username, password) {
element(by.id('username')).sendKeys(username);
element(by.id('password')).sendKeys(password);
element(by.buttonText('Login')).click();
});
Then('{string} is displayed', function (headerText) {
expect(element(by.cssContainingText('ng-isolate-scope', headerText)).isDisplayed()).to.eventually.equal(true);
});
});
Console Output:
Debugging with legacy protocol because Node.js v6.11.4 was detected.
'C:\Program Files\nodejs\node.exe' --debug-brk=38084 --nolazy node_modules\protractor\bin\protractor protractor.conf.js
Debugger listening on [::]:38084
[16:48:02] I/launcher - Running 1 instances of WebDriver
[16:48:02] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
...
1 scenario (1 passed)
3 steps (3 passed)
0m00.015s
[16:48:05] I/launcher - 0 instance(s) of WebDriver still running
[16:48:05] I/launcher - chrome #01 passed
Selenium Chrome node console output:
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 11099
Only local connections are allowed.
16:48:04.496 INFO - Found handler: org.openqa.selenium.remote.server.ServicedSession#1418ca00
16:48:04.496 INFO - Handler thread for session ed9fb79d586bc77176974953aaf98ef2 (chrome): Executing POST on /session/ed9fb79d586bc77176974953aaf98ef2/timeouts (handler: ServicedSession)
16:48:04.496 INFO - To upstream: {"script":500000}
16:48:04.496 INFO - To downstream: {"sessionId":"ed9fb79d586bc77176974953aaf98ef2","status":13,"value":{"message":"unknown error: 'ms' must be a double\n (Session info: chrome=60.0.3112.78)\n (Driver info: chromedriver=2.33.506120 (e3e
53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.3.9600 x86_64)"}}
16:48:04.512 INFO - Found handler: org.openqa.selenium.remote.server.ServicedSession#1418ca00
16:48:04.512 INFO - Handler thread for session ed9fb79d586bc77176974953aaf98ef2 (chrome): Executing POST on /session/ed9fb79d586bc77176974953aaf98ef2/timeouts (handler: ServicedSession)
16:48:04.512 INFO - To upstream: {"type":"script","ms":500000}
16:48:04.527 INFO - To downstream: {"sessionId":"ed9fb79d586bc77176974953aaf98ef2","status":0,"value":null}
16:48:05.010 INFO - Found handler: org.openqa.selenium.remote.server.ServicedSession#1418ca00
16:48:05.010 INFO - Found handler: org.openqa.selenium.remote.server.ServicedSession#1418ca00
16:48:05.010 INFO - Handler thread for session ed9fb79d586bc77176974953aaf98ef2 (chrome): Executing DELETE on /session/ed9fb79d586bc77176974953aaf98ef2 (handler: ServicedSession)
16:48:05.010 INFO - Handler thread for session ed9fb79d586bc77176974953aaf98ef2 (chrome): Executing POST on /session/ed9fb79d586bc77176974953aaf98ef2/url (handler: ServicedSession)
16:48:05.026 INFO - To upstream: {"url":"data:text/html,<html></html>"}
16:48:05.088 INFO - To downstream: {"sessionId":"ed9fb79d586bc77176974953aaf98ef2","status":0,"value":null}
16:48:05.088 INFO - Removing session org.openqa.selenium.remote.server.ServicedSession#1418ca00
16:48:05.138 INFO - To downstream: {"sessionId":"","status":0,"value":null}
16:48:05.138 INFO - To downstream: {"sessionId":"","status":6,"value":{"message":"no such session\n (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.3.9600 x86_64)"}}
Can you try using callback argument instead of relying on promises. If it works, then for entering username & password you can make use of Q package to resolve all the promises.
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
var {defineSupportCode} = require('cucumber');
var expect = chai.expect;
chai.use(chaiAsPromised);
defineSupportCode(function({Given, When, Then}) {
Given('I go to DSH Login page', function (callback) {
browser.get("http://localhost/myapp");
expect(element(by.buttonText('Login')).isDisplayed()).to.eventually.equal(true).and.notify(callback);
});
When('I login with {string} and {string}', function (username, password, callback) {
element(by.id('username')).sendKeys(username);
element(by.id('password')).sendKeys(password);
element(by.buttonText('Login')).click().then(function() {
callback();
});
});
Then('{string} is displayed', function (headerText, callback) {
expect(element(by.cssContainingText('ng-isolate-scope', headerText)).isDisplayed()).to.eventually.equal(true).notify(callback);
});
});
Since I am kind of getting used to the Javascript, I thought I will post what I am currently doing.
I have stopped using callbacks. Instead I use async and await so that the control waits for the asynchronous operation to be completed.
for example:
When('I login with {string} and {string}', async function (username, password) {
await element(by.id('username')).sendKeys(username);
await element(by.id('password')).sendKeys(password);
await element(by.buttonText('Login')).click();
});
Hope this helps someone.

Why is Karma not receiving a message from the browser after tests execution completes?

A test was failing with:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
So I added this.timeout(0) to it, then I got:
Disconnected, because no message in 10000 ms.
I went on to add browserNoActivityTimeout: 0 to my config file.
When I try to run the tests, the browser opens and the console shows the passed test.
SUCCESS
Skipped 0 tests
Why is the browser not being closed? What is it waiting for?
browserNoActivityTimeout
How long will Karma wait for a message from a browser before disconnecting from it (in ms).
https://github.com/karma-runner/karma/blob/master/docs/config/01-configuration-file.md#browsernoactivitytimeout
Command:
karma start --single-run
Config:
webpackConfig.module.loaders.push({
test: /\.js$/,
include: /src/,
exclude: /node_modules/,
loader: 'isparta'
});
var karmaConfig = {
frameworks: ['mocha'],
browsers: ['Chrome'],
//browserNoActivityTimeout: 0,
logLevel: 'INFO',
//reporters: ['progress', 'coverage-allsources', 'coverage'],
reporters: ['progress', 'coverage'],
autoWatch: true,
files: [
'test/karma.js'
],
urlRoot: '/karma-runner/',
preprocessors: {
'test/karma.js': ['webpack', 'sourcemap']
},
webpackMiddleware: {
stats: 'minimal',
watchOptions: {
aggregateTimeout: 300
}
},
webpack: webpackConfig,
coverageReporter: {
dir: 'report/coverage',
include: 'src/**/*.js',
// Any .js files that are not imported/required need to be added to the
// exclude:, otherwise you will get a JS error for
// 'Unexpected token in esprima.js'.
// This appears to be a bug with the karma-coverage-allsources repo.
exclude: 'src/init.js',
reporters: [
{'type' : 'cobertura'},
{'type' : 'html'},
{'type': 'text-summary'}
]
}
};
Five minutes after typing the question, I find the answer.
The entry point "test/karma.js" has code that opens the debug tab and that is probably interrupting somehow Karma.
window.open('/karma-runner/debug.html', '_blank');

Karma: browser idle

I am trying Karma for the first time, and after several hours I still can't get it to work.
When I run the test by typing karma start karma.conf.js in the terminal, the browser window opens and displays the following (I have also tried with Chrome with the same result):
This is the terminal output:
29 07 2015 16:27:12.835:INFO [karma]: Karma v0.13.3 server started at http://localhost:9876/
29 07 2015 16:27:12.852:INFO [launcher]: Starting browser Firefox
29 07 2015 16:27:15.866:INFO [Firefox 33.0.0 (Windows 7 0.0.0)]: Connected on socket HA1RSN-QsWuAO7NIAAAA with id 26755366
My karma.conf.js file is located at the root of my Node.js project and looks like this:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'tests/unit/test.js'
],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Firefox'],
singleRun: false
})
}
My test.js file looks like this (example test from a book, my actual tests will the Angular.js tests):
describe("First Test", function () {
var counter;
beforeEach(function () {
counter = 0;
});
it("increments value", function () {
counter++;
expect(counter).toEqual(1);
});
it("decrements value", function () {
counter--;
expect(counter).toEqual(0);
});
});
I'm using Node.js version 0.12.05.
I appreciate any help as I feel really lost here.
You need to trigger a test run, if you want it to execute. There are two ways to do this
Run karma run karma.conf.js in a second terminal window in the same working directory
Change the option singleRun to true, this way it will start, execute the test and then exit.

Selective test execution in Karma Jasmine using pattern matching

I'm having trouble invoking the command line option on karma-jasmine which allows for the execution of only those tests which match a given pattern. My spec reads as follows:
/path/to/single-test/main.spec.js
describe('my first test suite', function() {
it('always passes', function() {
expect(true).toBe(true);
});
it('still always passes', function() {
expect(true).toBe(true);
});
});
I'm assuming the description (for example "still always passes") is the item against which the pattern specified by the grep command line option is matched. When I attempt to run the second example based on the fact that its description is the only example containing the word "still", both examples are executed instead of just the one:
$ karma start -- --grep=still
INFO [karma]: Karma v0.12.35 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Linux 0.0.0)]: Connected on socket 7Dn7Ez1Reap7ch0Uzsb0 with id 44623726
PhantomJS 1.9.8 (Linux 0.0.0): Executed 2 of 2 SUCCESS (0.002 secs / 0.001 secs)
How do I execute just this one example based on a pattern? The official documentation doesn't give a sample of the usage of the pattern matching option.
I read in the discussion of a pull request, that the grep option can be used in conjunction with "fit" and "fdescribe." This works when tested. However, in the case of using grep with "fit", what's the purpose of the pattern argument to the grep option? (It would be nice to be able to execute tests selectively without the need to augment source code!)
Here is the remainder of the files in my project for reference:
/path/to/single-test/karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: ['*.spec.js'],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['PhantomJS'],
singleRun: true
});
};
/path/to/single-test/package.json
{
"name": "single-test",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"devDependencies": {
"jasmine-core": "^2.3.4",
"karma": "^0.12.35",
"karma-jasmine": "^0.3.5",
"karma-phantomjs-launcher": "^0.2.0",
"phantomjs": "^1.9.17"
}
}
You have to start a Karma server, then specify the --grep option in a Karma runner. I.e. something along the lines of:
karma start path/to/karma.conf.js
Then in another terminal:
karma run path/to/karma.conf.js -- --grep=still
It is important that you set singleRun: false in the configuration options.
There is karma-jasmine-spec-tags plugin which helps to filter running tests by tags in their names.
Example usage:
$ karma start --tags smoke
$ karma start --skip-tags slow,bench
$ karma start --tags bench --skip-tags slow
$ karma start --tag-prefix 'scope:' --tags critical
Where a spec is following:
describe('Example test', () => {
it('should be a #smoke test', () => {
// ...
});
it('#slow test', () => {
// ...
});
})
describe('Performance test suite #bench', () => {
it('#fast #smoke test', () => {
// ...
});
it('#slow test', () => {
// ...
});
})
describe('Custom tag prefix', () => {
it('test scope:critical', () => {
// ...
});
})

Running protractor tests on multiple browsers with browser stack

I am trying to run E2E tests on multiple browsers on browser stack, I took the reference from
E2E testing on multiple/parallel browsers in Protractor?
and
Internet Explorer Selenium protractor e2e tests
but the error I get every time I try to run the tests -
ERROR - Unable to start a WebDriver session.
C:\MrWhiteMVP\whitemvp-integrationtests_develop\node_modules\gulp-protractor\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:113
var template = new Error(this.message);
^
UnknownError: Authorization required
at new bot.Error (C:\MrWhiteMVP\whitemvp-integrationtests_develop\node_modules\gulp-protractor\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:113:18)
at Object.bot.response.checkResponse (C:\MrWhiteMVP\whitemvp-integrationtests_develop\node_modules\gulp-protractor\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\response.js:103:11)
But if I run the tests on 1 browser, then it works perfectly fine. this is how my conf file looks like
'use strict';
exports.config = {
seleniumAddress: 'http://hub.browserstack.com/wd/hub',
multicapabilities: [{
'browserstack.user': 'testuser',
'browserstack.key': 'testkey',
// Needed for testing localhost
// 'browserstack.local': 'true',
// Settings for the browser you want to test
'browserName': 'chrome',
'version': '36.0',
'os': 'OS X',
'os_version': 'Mavericks',
'resolution': '1024x768'
},
{
'browserstack.user': 'testuser',
'browserstack.key': 'testkey',
// Needed for testing localhost - 'browserstack.local': 'true',
// Settings for the browser
'browserName': 'firefox',
'os': 'windows'
}],
baseUrl: 'http://origin-develop.mvp.livebranches.com',
// The require statement must be down here, since jasmine-reporters
// needs jasmine to be in the global and protractor does not guarantee
// this until inside the onPrepare function.
onPrepare: function()
{
require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter('xmloutput', true, true));
},
},
};
could anyone please tell me what am I doing wrong here, also we use gulp ti run specs but my problem is it is saying not even going beyond authentication.
I think that first of all, you have an extra comma on your config.
onPrepare: function()
{
require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter('xmloutput', true, true));
},
}, <-----
};