Karma JSPM Horrible Trace Log - angularjs

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.

Related

test case execution is getting failed and it is throwing any error stack trace in command line

I created a testcase with Protractor-cucumber framework and I have used Grunt for execution of that test case. However, at the time of execution it failed and it's not providing any error stacktrace to know why it is failing.
I've searched Google and also reviewed other Stack Overflow solutions but I didn't find a solution for this.
Config File
exports.config = {
//seleniumAddress: 'http://localhost:4444/wd/hub',
getPageTimeout: 60000,
allScriptsTimeout: 500000,
directConnect:true,
framework: 'custom',
// path relative to the current config file
frameworkPath: require.resolve('C:\\...\\node_modules\\protractor-cucumber-framework'),
capabilities: {
'browserName': 'chrome'
},
ignoreUncaughtExceptions:true,
// Spec patterns are relative to this directory.
specs: [
'./learnFramework/utility/test.feature'
],
cucumberOpts: {
require: './learnFramework/TestCases/spec.js',
tags: false,
profile: false,
'no-source': true
},
onPrepare: function () {
browser.ignoreSynchronization=true;
browser.manage().window().maximize();
}
};
Spec File
module.exports=function(){
this.Given(/^Open the browser and Load the URL$/,async function(){
browser.get("https://google.com");
});
this.When(/^User entered the text in the search box$/,async function(){
browser.sleep(5000);
console.log(await browser.getTitle());
await element(By.name("q")).sendKeys("facebook");
browser.sleep(3000);
});
this.Then(/^click on search$/,async function(){
browser.action().sendKeys(protractor.Key.ENTER).perform();
browser.sleep(5000);
});
}
Error Log
Running "jshint:files" (jshint) task
>> 1 file lint free.
>
Running "protractor:singlerun" (protractor) task
[00:08:29] I/launcher - Running 1 instances of WebDriver
[00:08:29] I/direct - Using ChromeDriver directly...
>
DevTools listening on ws://127.0.0.1:50146/devtools/browser/5c09d68c-f3ff-43b2-b645-0b5c098c41d9
Feature: Title of your feature
>
I want to use this template for my feature file
>
Scenario: Title of your scenario
✓ Given Open the browser and Load the URL
✖ When User entered the text in the search box
- And click on search
>
Failures:
>
[00:09:03] I/launcher - 0 instance(s) of WebDriver still running
[00:09:03] I/launcher - chrome #01 failed 1 test(s)
[00:09:03] I/launcher - overall: 1 failed spec(s)
[00:09:03] E/launcher - Process exited with error code 1
>>>
>>> Test failed but keep the grunt process alive.
>
Done.
If you observe the error log "When" step has failed but there is no error stacktrace in the command line to find why it failed.
My expectation is that it should show the error stacktrace for why it has failed.
You need to use await on your browser actions otherwise it performs the actions synchronously.
e.g
await browser.get("https://google.com");

Protractor and Cucumber.js: expectations doesn't works as expected using promises with mocha

I have been trying to testing my angular app using protractor and cucumber, but when i use mocha for create a expectation when the expectation it's false the error spec doesn't show in the console.
The relevant HTML for the route http://localhost:9000/#/form29/form29'
...
<h4 class="title">
The title
</h4>
...
And my step file it is:
//form29_steps.js
var chai = require('chai'),
chaiAsPromised = require('chai-as-promised'),
assert;
chai.use(chaiAsPromised);
expect = chai.expect;
module.exports = function () {
this.Given(/^I am in the form 29 page$/, function (done) {
browser.get('http://localhost:9000/#/form29/form29');
done();
});
this.Then(/^should be the title "(.*)"/,function(title, done){
var el = element(by.css('.title'));
el.getText().then(function(text){
//a false expect
expect(title).to.eq('Aaaaa');
done();
});
});
};
when the expect its valid its ok, but when the expect failed there aren't a expect failed error and show the following:
[16:14:08] E/launcher - "process.on('uncaughtException'" error, see launcher
[16:14:08] E/launcher - Process exited with error code 199
When i try the same but not using promised this works well
this.Then(/^should be the title "(.*)"/,function(title, done){
var el = element(by.css('.title'));
expect(title).to.eq('A');
done();
});
I get the error that i wish:
Message:
AssertionError: expected 'Formulario 29' to equal 'A'
at World.<anonymous> (/protractor/test/e2e/features/step_definitions/form29_steps.js:18:20)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
Why happen this?
[16:14:08] E/launcher - "process.on('uncaughtException'" error, see launcher
[16:14:08] E/launcher - Process exited with error code 199
The above error can be caused due to various reasons mostly related to promises. But it should throw the correct message. There is already a work around provided here https://github.com/angular/protractor/issues/3384 to catch the exact error message.
You could change the launcher.ts file in your protractor dependency as mentioned in above forum to catch the error in order to debug your issue.

Executing angularJS unit test in karma tool

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 () {

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.

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