Executing angularJS unit test in karma tool - angularjs

It was executing all test cases without any issues. i don't understand what happened suddenly.
when i execute the command karma start karma.conf.js it is not executing all test cases.
Result am getting is,
Chrome 40.0.2214 (Windows 7): Executed 10 of 1517 (skipped 1507) SUCCESS (0.53 s
ecs / 0.134 secs)
what could be wrong?

One of my test files has,
ddescribe('my controller tests, ', function () {
this should be,
describe('my controller tests, ', function () {

Related

Karma JSPM Horrible Trace Log

I have been setting up karma and jasmine on my angularjs 1.5 and jspm setup. Firstly all errors from karma the trace log is just coming from systemjs which makes it harder to debug. I am also getting a lot of Potentially unhandled rejection messages even though all of my promises are handling rejections.
ERROR LOG: 'Potentially unhandled rejection [5]
tryCatchReject#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:1252:34
runContinuation1#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:1211:18
when#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:999:20
run#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:1109:28
_drain#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:166:22
drain#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:131:15'
ERROR LOG: 'Potentially unhandled rejection [6]
tryCatchReject#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:1252:34
runContinuation1#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:1211:18
when#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:999:20
run#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:1109:28
_drain#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:166:22
drain#http://localhost:9020/base/jspm_packages/system-polyfills.src.js?3aa57969dce4ecea4c51aab540f372d15cc886b6:131:15'
MyService
✗ should do the thing
Expected 3 to equal 2.
tryCatchReject#/var/www/html/loyaltyone/src/jspm_packages/system-polyfills.src.js:1252:34
runContinuation1#/var/www/html/loyaltyone/src/jspm_packages/system-polyfills.src.js:1211:18
when#/var/www/html/loyaltyone/src/jspm_packages/system-polyfills.src.js:999:20
run#/var/www/html/loyaltyone/src/jspm_packages/system-polyfills.src.js:890:17
PhantomJS 2.1.1 (Linux 0.0.0): Executed 46 of 46 (1 FAILED) (0.205 secs / 0.028 secs)
I there a way to have better trace logs with the errors?
I was seeing the same Potentially unhandled rejection... errors too. They're just awful and completely unhelpful. What I did to debug my issue was to put phantomjs into debug mode, and place a debugger; statement just before the line of code referenced in the error and then I was able to step through and find the exact issue I was having.
In your case, the error is being thrown on line 1252 of jspm_packages/system-polyfills.src.js, at which is a method tryCatchReject. I would place a debugger; statement like so and then view the value of e.message while debugging:
/**
* Return f.call(thisArg, x), or if it throws return a rejected promise for
* the thrown exception
*/
function tryCatchReject(f, x, thisArg, next) {
try {
next.become(getHandler(f.call(thisArg, x)));
} catch(e) {
debugger;
next.become(new Rejected(e));
}
}
The karma-phantomjs-launcher readme gives a good example of how to configure Karma to pass the right flags to phantomjs for debugging as well as some good instructions:
// karma.conf.js
module.exports = function(config) {
config.set({
browsers: ['PhantomJS', 'PhantomJS_custom'],
// you can define custom flags
customLaunchers: {
'PhantomJS_custom': {
base: 'PhantomJS',
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
},
},
flags: ['--load-images=true'],
debug: true
}
},
phantomjsLauncher: {
// Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
exitOnResourceError: true
}
})
}
If you set the debug option to true, you will be instructed to launch
a web browser to bring up the debugger. Note that you will want to put
debugger; statements in your JavaScript to hit breakpoints. You should
be able to put breakpoints in both your test code and your client
code. Note that the debug option automatically adds the
--remote-debugger-port=9000 and --remote-debugger-autorun=yes switches to PhantomJS.
When you start running your tests you should see a prompt to navigate to http://localhost:9000/webkit/inspector/inspector.html?page=2. There you can enabled debugging and step through the code.

Closing a window.confirm in protractor with phantomJs

I'm writing E2E tests with Protractor for my AngularJS app.
At some point the browser will encounter a window.confirm.
When using Chrome as the test browser, the following code works fine :
var ptor = protractor.getInstance();
ptor.switchTo().alert().accept();
But on PhantomJS it raises the following error :
UnknownError: Invalid Command Method
==== async task ====
WebDriver.switchTo().alert()
at tests/E2E/spec/search.spec.js:73:33
==== async task ====
Asynchronous test function: it()
Error
at null.<anonymous> (tests/E2E/spec/search.spec.js:63:5)
at Object.<anonymous> (tests/E2E/spec/search.spec.js:6:1)
Any clue on how to handle it with PhantomJS ?
Since there is no support yet for switchTo().alert() for PhantomJS/GhostDriver
I went for the following solution : mocking window.confirm as following :
beforeEach(function() {
// bypassing PhantomJS 1.9.7/GhostDriver window.confirm (or alert) bug.
// as WebDriver's switchTo().alert() is not implemented yet.
browser.executeScript('window.confirm = function() {return true;}')
});
NB: I used jasmine for my Protractor tests, therefore I needed to put it in the beforeEach, else it would have no effect.

Protractor: timed out after 10000 msec waiting for spec to complete

I have several protractor tests and sometimes I get a error saying:
Message:
timeout: timed out after 10000 msec waiting for spec to complete
Stacktrace:
undefined
It can be randomly happening on some tests.
I usually test on BrowserStack and it shows the error once in 3-5 builds. But recently I tried SauceLabs and almost every (every!) but not all the tests fail with that error. Probably, SauceLabs are just significantly slower so I get the error more often...
So here are the questions:
Is there a way in Protractor/Selenium to change test running timeout? It needs to be changed on BrowserStack/Saucelabs as well.
Why am I getting the error so often? Is there anything wrong with my tests? Most of the doesn't seem complicated or long running. Again, on local machine it's almost always fine.
Here is the example:
it('should check that login gives error on empty or incorrect email', function () {
p.get('/#/login');
p.findElement(protractor.By.css('button[type="submit"]')).click();
expect(p.findElement(protractor.By.css('.alert-danger')).getText()).toEqual('E-mailadres is niet geldig');
p.findElement(protractor.By.model('user.email')).sendKeys('test-1xtc.vc');
p.findElement(protractor.By.css('button[type="submit"]')).click();
expect(p.findElement(protractor.By.css('.alert-danger')).getText()).toEqual('E-mailadres is niet geldig');
p.findElement(protractor.By.model('user.email')).clear();
});
The app is using
AngularJS, selenium 2.20, protractor 0.20.1
Is there a way in Protractor/Selenium to change test running timeout?
Yes :) You can do it via the allScriptsTimeout in your Protractor config (from Protractor FAQ)
You can also set the defaultTimeoutInterval in jasmineNodeOpts option (from Protractor referenceConf.js)
Why am I getting the error so often? Is there anything wrong with my tests? Most of the doesn't seem complicated or long running. Again, on local machine it's almost always fine.
Hard to say without seeing your tests. The example you provided looks good to me.
Is your project an angularjs project? Are you using any $interval or $timeout service somewhere? If so, try to use $interval instead of $timeout and try to adjust the optional 'count' parameter to be 1, for example (https://docs.angularjs.org/api/ng/service/$interval#usage).
I had recently a similar problem and I solved it this way.
I solved it using a different property of config file, which I didn't see mentioned in answers above.
getPageTimeout : 100000 //in millis, i.e., 100 secs
the default time for saucelabs is 90 seconds I believe. it can be changed via your conf.js file using the idleTimeout variable which takes in a value of seconds. example
idleTimeout = 90; // equals 90 seconds
exports.config = {
//Includes test sub-sub-foldersbefore any tests in sub folders
specs: ['tests/*/*/*.js', 'tests/*/*.js', ],
// use jasmine 2
framework: 'jasmine2',
capabilities : {
browserName: "chrome",
// this takes seconds so 120 would be 120 seconds.
idleTimeout = 120;
},
},
Now if you need to change the value of a specific spec file you can use this inside your spec file, of course if your saucelabs idleTimeout is lower than this value than it will time out on saucelabs first. or if your saucelabs value is higher but the DEFAULT_TIMEOUT_INTERVAL is lower then it will time out.
// this takes in miliseconds so 1000 = 1 second
jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000;
Jasmine has timeout for each spec i.e. each it in jasmine framework. so if any spec (it) exceeds jasmine spec's default timeout then it fails that spec.
Solution:
To override jasmine spec timeout for one individual spec, pass a third parameter to it:
it(description, testFn, timeout_in_millis)
it('description of test case', function() {
/*your test
steps
here*/
},120000);//120 seconds timeout for this spec
more information on timeouts is here

Karma Jasmine PhantomeJS test failing ramdomly

I have a really strange behavior in my test for a simple angularJS app
Setup:
Karma v0.10.9
PhantomJS 1.9.7
All test passing the first time ( sometimes the second or third too :-) ) But after a while test failing randomly
There are two kinds of errors
TypeError: 'null' is not an object (evaluating 'errorForStack.stack')
at workFn (<my_path>/angular-mocks.js:1811)
and
Error: SECURITY_ERR: DOM Exception 18
//Line $window.openDatabase('myDB', "", 'Offline DB', 5 * 1024 * 1024);
Problem occurs on Windows and Linux
Current fix -> restart Karma --> But i hate this solution since continuous testing is just great
Any suggestions ?
Fixed the first problem ( Bug in angular-mocks.js ).
Second problem fixed by mocking openDatabase.
Thx for the help

when running protractor with phantomjs browser, only able to run tests once

test code:
describe('mysite', function(){
var init_url = 'http://localhost/mySite/#/home';
beforeEach(function(){
// driver = new webdriver.Builder().
// withCapabilities(webdriver.Capabilities.phantomjs()).build();
})
it('should click on toolbox and do stuff', function(){
browser.get(init_url);
browser.waitForAngular();
browser.getCurrentUrl().then(function(url){
console.log('current_url', url);
expect(init_url).toEqual(init_url);
})
expect(true).toBe(true);
browser.sleep(2000);
})
result 1st time run,
Using the selenium server at http://localhost:9515
data Zoom Pad
class active
mysite
should click on toolbox and do stuff
Finished in 3.94 seconds
1 test, 4 assertions, 0 failures
2nd time run, without any interruption, just up arrow and enter:
Stacktrace:
Error: Error while running testForAngular: Error Message => 'Detected a pag
e unload event; asynchronous script execution does not work across page loads.'
caused by Request => {"headers":{"Accept":"application/json; charset=utf-8","Co
nnection":"keep-alive","Content-Length":"689","Content-Type":"application/json;c
harset=UTF-8","Host":"localhost:9515"},"httpVersion":"1.1","method":"POST","post
":"{\"script\":\"return (function () {\\n var attempts = arguments[0];\\n var
callback = arguments[arguments.length - 1];\\n var check = function(n) {\\n
try {\\n if (window.angular && window.angular.resumeBootstrap) {\\n
callback([true, null]);\\n } else if (n < 1) {\\n if (window.angular
) {\\n callback([false, 'angular never provided resumeBootstrap']);\\n
} else {\\n callback([false, 'retries looking for angular exceed
third time
1) mysite should click on toolbox and do stuff
Message:
Error: ECONNREFUSED connect ECONNREFUSED
Stacktrace:
Error: ECONNREFUSED connect ECONNREFUSED
at ClientRequest.<anonymous> (K:\Users\Congwen\AppData\Roaming\npm\node_modu
les\protractor\node_modules\selenium-webdriver\http\index.js:127:16)
at ClientRequest.EventEmitter.emit (events.js:95:17)
at Socket.socketErrorListener (http.js:1547:9)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:441:14
and on third time the phantomjs webserver is down, and needs to be reconnected, and afterwards it goes back to result 1:
any clues?
config file used:
exports.config = {
seleniumAddress: 'http://localhost:9515',
specs: [
'./ptor-tests/mysite-test.js'
],
capabilities: {
browserName: 'phantomjs',
version: '',
platform: 'ANY'
},
//baseUrl: 'http://testapp.example.com/index.html',
rootElement: 'body',
allScriptsTimeout: 11000,
onPrepare: function () {},
jasmineNodeOpts: {
onComplete: function () {},
isVerbose: true,
showColors: true,
includeStackTrace: true,
defaultTimeoutInterval: 30000
}
};
also I noticed that sometimes there's no step 2 needed and it will go directly to ECONNECT error, and sometimes it gets stuck in step 2 for a number of tests and eventually will terminate phantomjs server.
This is an issue with Protractor that was resolved in version 0.17 and made better in 0.18.
It's a bug with a long tail, but the TL;DR is that Protractor's .get(url) function actually uses client-side JavaScript to make the location change; this is to ensure it properly bootstraps. An unfortunate side effect of that design is that for some reason, PhantomJS takes a few seconds to navigate over properly.
The bug was resolved by adding a longer timeout to the .get function.
Github Issue: https://github.com/angular/protractor/issues/85
Relevant changelog entries:
v0.18
(10aec0f) fix(pageload): increase wait timeout
The 300 ms wait caused problems when testing IE on Sauce Labs. It seems way too short. "browser.get()" invariably timed out. Increasing it solved our problem.
v0.17
(a0bd84b) fix(pageload): add a wait during protractor.get() to solve unload issues
Some systems would not wait for the browser unload event to finish before beginning the asynchronous script execution.
Closes #406. Closes #85.
I've run your test locally (with a different page, but otherwise the same code):
Happy testing!

Resources