How can I fail Karma tests if there are unhandled rejections? - karma-runner

A couple of places propose this solution:
window.addEventListener('unhandledrejection', function(err) {
window.__karma__.error(err); // yeah private API ¯\_(ツ)_/¯
});
But it throws:
Uncaught TypeError: Cannot read property 'error' of undefined

I'm able to get reports of unhandled rejections with the following setup:
karma.conf.js:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['mocha'],
files: [
'setup.js',
'test.js',
],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
});
};
setup.js:
window.addEventListener('unhandledrejection', function(ev) {
window.__karma__.error("unhandled rejection: " + ev.reason.message);
});
test.js:
it("test 1", () => {
Promise.reject(new Error("Q"));
});
it("test 2", (done) => {
setTimeout(done, 1000);
});
Separating setup.js from test.js is not necessary. I just like to have such setup code separate from the tests proper.
When I run karma start --single-run I get:
25 01 2017 07:20:07.521:INFO [karma]: Karma v1.4.0 server started at http://0.0.0.0:9876/
25 01 2017 07:20:07.523:INFO [launcher]: Launching browser Chrome with unlimited concurrency
25 01 2017 07:20:07.528:INFO [launcher]: Starting browser Chrome
25 01 2017 07:20:08.071:INFO [Chrome 55.0.2883 (Linux 0.0.0)]: Connected on socket g-BGwMfQLsQM128IAAAA with id 22107710
Chrome 55.0.2883 (Linux 0.0.0) ERROR
unhandled rejection: Q
Chrome 55.0.2883 (Linux 0.0.0): Executed 1 of 2 ERROR (0.006 secs / 0.001 secs)
Caveat
Reports of unhandled rejections are asynchronous. This has a few consequences.
The example I gave has a 2nd test that takes 1 second to complete. This gives time to the browser to report the unhandled rejection in the 1st test. Without having this delay, Karma terminates without detecting the unhandled rejection.
Another issue is that an unhandled rejection caused by test X may be discovered while test X+1 is running. The runner's report may make it look like X+1 is the test the caused the issue.

Related

WebdriverIO can not create session to test Android emulator with Appium

I am using Visual Studio Code and WDIO with Appium to test Android on emulator by Android Studio, but I can not run Appium for some reason. However Android home and Java home has been set and Appium Doctor confirms necessary dependencies are completed, no fix needed.
I have tried with Appium stand-alone and from Command too.
Wit Appium standalone the ERROR log is the below:
ERROR webdriver: Request failed with status 500 due to session not created: Unable to create session from {
"desiredCapabilities": {
"platformName": "android",
"appium:deviceName": "Pixel",
"appium:platformVersion": "10.0",
"appium:app": "\u002fUsers\u002fzsolt\u002fappium_js\u002fApiDemos-debug.apk"
If I add ['appium'] to services in wdio.config.js the ERROR log is the below, though Appium stand-alone is shut:
ERROR #wdio/cli:utils: A service failed in the 'onPrepare' hook
Error: Appium exited before timeout (exit code: 2)
[HTTP] Could not start REST http interface listener. The requested port may already be in use. Please make sure there is no other instance of this server running already.
at ChildProcess.<anonymous> (C:\Users\zsolt\Documents\AppiumTestProject\node_modules\#wdio\appium-service\build\launcher.js:103:16)
at Object.onceWrapper (events.js:417:26)
at ChildProcess.emit (events.js:310:20)
at ChildProcess.EventEmitter.emit (domain.js:482:12)
at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
This is my wdio.config.js file with using Appium stand-alone:
exports.config = {
runner: 'local',
port: 4723,
specs: [
'./test/specs/**/*.js'
],
exclude: [
// 'path/to/excluded/files'
],
maxInstances: 10,
capabilities: [{
platformName: 'android',
'appium:deviceName': 'Pixel',
'appium:platformVersion': '10.0',
'appium:app': '/Users/zsolt/appium_js/ApiDemos-debug.apk'
}],
logLevel: 'error',
bail: 0,
baseUrl: 'http://localhost',
waitforTimeout: 10000,
connectionRetryTimeout: 120000,
connectionRetryCount: 3,
services: ['selenium-standalone',
/*['appium', {
//command : 'appium'
}]*/],
reporters: ['spec'],
mochaOpts: {
ui: 'bdd',
timeout: 60000
},
These are my:
"devDependencies": {
"#wdio/local-runner": "^6.1.11",
"#wdio/mocha-framework": "^6.1.8",
"#wdio/selenium-standalone-service": "^6.0.16",
"#wdio/spec-reporter": "^6.1.9",
"#wdio/sync": "^6.1.8",
"appium": "^1.17.1"
Has anybody an idea what am I doing wrong?
Welcome to Stack-overflow!
Below is my configuration and I believe you are missing some important details.
{
platformName: 'Android',
maxInstances: 1,
'appium:deviceName': 'Device Name',
'appium:platformVersion': '8'
'appium:orientation': 'PORTRAIT',
'appium:app': 'Path to App',
'appium:automationName': 'UiAutomator2',
'appium:noReset': true,
'appium:newCommandTimeout': 240,
}
There are boilerplates from maintainers themselves here.
I have played with the boilerplates and was able to run the tests on android emulator through Appium finally.
I have then compared the differences in conf.js and figured out, the below is needed to execute the test.
services:[
[
'appium',
{
// For options see
// https://github.com/webdriverio/webdriverio/tree/master/packages/wdio-appium-service
args: {
// For arguments see
// https://github.com/webdriverio/webdriverio/tree/master/packages/wdio-appium-service
},
command: 'appium',
},
],
],
Interesting but actually the test runs with only the below capabilites too:
capabilities: [{
platformName: 'Android',
'appium:deviceName': 'Pixel 2',
'appium:app': join(process.cwd(), './ApiDemos/ApiDemos-debug.apk'),
}],
When I execute the test it logs almost the same error messages as before though at the end of the execution it runs the test on the emulator. I did not use the Appium Desktop app.
Not perfect but works...
I guess this issue has been sorted out.
Cheers
Also you need to install the Appium service:
$ npm i #wdio/appium-service --save-dev
Finally check in your wdio.conf.js:
port: 4723, // default appium port
services: [
[
'appium',
{
args: {
},
command: 'appium'
}
]
],
Check that the file you are running is inside the correct folder when you do the 'npx wdio'.
this file

'Globals' are not working as expected in protractor 4.0.0?

we have the following setting in our onPrepare function of config file-
exports.config = {
directConnect: true,
baseUrl: env.baseUrl,
capabilities: env.capabilities,
onPrepare: function () {
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
global.expect = chai.expect; // by removing this line error is not thrown
browser.manage().window().maximize();
},
setDefaultTimeout : 60 * 1000,
framework: 'custom',
frameworkPath: require.resolve('protractor-cucumber-framework'),
specs: [
'../Features/*.feature'
],
cucumberOpts: {
monochrome: true,
strict: true,
plugin: ["pretty"],
require: ['../StepDefinitions/*.js', '../Support/*.js'],
tags:'#AddActiveTip,#AddInActiveTip,~#AddActiveManufacturer,~#AddInActiveManufacturer'
//tags:'#TestSplitText'
}
};
since the upgrade to protractor 4.0, I am getting the following error and the browser hangs indefinitely and the error does not help to debug in anyway.
error:
> protractor Config/config.js --troubleshoot
[22:38:12] I/direct - Using FirefoxDriver directly...
[22:38:12] I/launcher - Running 1 instances of WebDriver
/Users/pasalar/protractor/psms-protractor/node_modules/protractor/built/exitCodes.js:87
if (e.message.indexOf(errMsg) !== -1) {
^
TypeError: Cannot read property 'indexOf' of undefined
at Function.ErrorHandler.isError (/Users/pasalar/protractor/psms-protractor/node_modules/protractor/built/exitCodes.js:87:30)
at Function.ErrorHandler.parseError (/Users/pasalar/protractor/psms-protractor/node_modules/protractor/built/exitCodes.js:98:26)
at process.<anonymous> (/Users/pasalar/protractor/psms-protractor/node_modules/protractor/built/launcher.js:169:54)
at emitOne (events.js:82:20)
at process.emit (events.js:169:7)
at process.emit (/Users/pasalar/protractor/psms-protractor/node_modules/protractor/node_modules/source-map-support/source-map-support.js:419:21)
at process._fatalException (node.js:224:26)
npm ERR! Test failed. See above for more details.
is anyone else facing this issue? are there any major changes that I am missing?
The error message shown has nothing to do with "globals", it is working as expected! my scripts were failing randomly due to network latency issues.But the error messages need to improved as the user has no idea what caused the error!
Marking it closed as I was able debug this issue!

Error on protractor: "Timed out waiting for the WebDriver server at http://127.0.0.1:50636/hub" executing on firefox

I was executing my scripts on firefox and got one notification for Firefox upgrade which I closed. Started execution again but I'm getting error as below,
Rohits-MacBook-Pro:FFAutomation rohitgathibandhe$ /Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/bin/protractor conf.js
Report destination: target/screenshots/Report.html
Using FirefoxDriver directly...
[launcher] Running 1 instances of WebDriver
/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/selenium-webdriver/http/util.js:83
Error('Timed out waiting for the WebDriver server at ' + url));
^
Error: Timed out waiting for the WebDriver server at http://127.0.0.1:50636/hub
at Error (native)
at onError (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/selenium-webdriver/http/util.js:83:11)
at Promise.invokeCallback_ (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1329:14)
at TaskQueue.execute_ (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2790:14)
at TaskQueue.executeNext_ (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2773:21)
at /Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2652:27
at /Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
at process._tickCallback (node.js:406:9)
From: Task: WebDriver.createSession()
at acquireSession (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver.js:62:22)
at Function.createSession (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver.js:295:12)
at Driver (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/selenium-webdriver/firefox/index.js:271:38)
at [object Object].DirectDriverProvider.getNewDriver (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/built/driverProviders/direct.js:76:16)
at [object Object].Runner.createBrowser (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/built/runner.js:203:37)
at /Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/built/runner.js:293:21
at _fulfilled (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/q/q.js:834:54)
at self.promiseDispatch.done (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/q/q.js:863:30)
at Promise.promise.promiseDispatch (/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/q/q.js:796:13)
at /Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/node_modules/q/q.js:556:49
[launcher] Process exited with error code 1
My config file is as below,
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
var reporter = new HtmlScreenshotReporter({
dest: 'target/screenshots',
filename: 'Report.html',
reportTitle: 'Execution Report',
showSummary: true,
showQuickLinks: true,
pathBuilder: function(currentSpec, suites, browserCapabilities) {
// will return chrome/your-spec-name.png
return browserCapabilities.get('browserName') + '/' + currentSpec.fullName;
}
});
exports.config = {
directConnect: true,
//seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {'browserName': 'firefox'},
framework: 'jasmine',
specs: ['Login_spec2.js','Article_spec.js'],
allScriptsTimeout: 200000,
getPageTimeout: 200000,
jasmineNodeOpts: {
defaultTimeoutInterval: 200000
},
// Setup the report before any tests start
beforeLaunch: function() {
return new Promise(function(resolve){
reporter.beforeLaunch(resolve);
});
},
// Close the report after all tests finish
afterLaunch: function(exitCode) {
return new Promise(function(resolve){
reporter.afterLaunch(resolve.bind(this, exitCode));
});
},
onPrepare: function() {
var width = 1300;
var height = 1200;
browser.driver.manage().window().setSize(width,height);
jasmine.getEnv().addReporter(reporter);
afterAll(function(done) {
process.nextTick(done);
})
}
};
Other details are:
Other details are as below: protractor#3.2.2, nodeVersion: 4.2.4, npmVersion: 2.14.12, jasmine: 2.4.1, selenium-webdriver: 2.52.0, firefox: 47
Kindly help me with solution to this issue.
You have to do the following to make it work:
upgrade protractor to the latest, currently 3.3.0, version which would also bring selenium-webdriver 2.53
downgrade firefox to version 46 and don't let it update for a while (there are compatibility issues)
In my case it was the local firewall (iptables on Linux) that was very restrictive such that WebDriver could not contact the Selenium server hub at whatever TCP port.
To make it work I turned off the firewall. The long term solution was to adjust the firewall rules such that this type of connection is allowed.
If you want your scripts communicate directly with the Firefox|Chrome Driver (bypassing the Selenium server entirely) then try adding the directConnect: true in your protractor.conf.js
I solved it by change the browser (use Chrome instead of Firefox).
In C:__projectWorkspace__\src\test\javascript\protractor.conf.js
modify the entry
capabilities: {
'browserName': 'firefox',
to
capabilities: {
'browserName': 'chrome',
That wasn't enough. I had to change also the webBrowserDriver 'cause after the JHipster installation the 2.22 version was written in protractor.conf.js file but in the path:
node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.25.exe
the chromedriver was 2.25 version so i edited the entry
webbrowserDriver = prefix + 'node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.25.exe';
and it worked!
I solved this by restarting the computer.
I don't know why, but I think that "suspending" the computer can cause some bad configuration with Selenium Server.
Ah, I run commands on the command prompt as an administrator.
I fixed this issue by changing directConnect to true in base.config.js from repository folder.
or
webdriver-manager clean and then webdriver-manager start

selenium standalone terminates with unknown error

when i start my test the browser opens and an ip is displayed after few sec the ip is changed to "data;" in terminal the test is been looped and sends err ms
empresss-Mac-mini:myApp admin$ protractor test/e2e.js
Using the selenium server at http://localhost:4444/wd/hub
[launcher] Running 1 instances of WebDriver
Started
A Jasmine spec timed out. Resetting the WebDriver Control Flow.
F
Failures:
1) header Module should check title text
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Message:
Failed: header is not defined
Stack:
ReferenceError: header is not defined
at Object.<anonymous> (/Users/admin/myApp/www/head.spec.js:8:13)
From: Task: Run it("should check title text") in control flow
From asynchronous test:
Error
at Suite.<anonymous> (/Users/admin/myApp/www/head.spec.js:7:4)
at Object.<anonymous> (/Users/admin/myApp/www/head.spec.js:1:63)
1 spec, 1 failure
Finished in 30.019 seconds
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 failed 1 test(s)
[launcher] overall: 1 failed spec(s)
[launcher] Process exited with error code 1
my config file is
exports.config = {
framework: 'jasmine2',
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['../www/head.spec.js'],
multiCapabilities: [
{
browserName: 'chrome'
}],
onPrepare: function(){
browser.driver.get('http://192.168.1.2:8100');
}
}
i named it as e2e.js
my test file is
describe('header Module', function(){
beforeEach(function() {
var header = element(by.css('title'));
});
it('should check title text',function(){
expect(header.getText()).toContain('Ionic Blank Starter');
});
});
i downloaded an blank ionic app and i want to test the header contains elements "Ionic Blank Starter"
You are not handling async code well in your project. header.getText() returns a promise which you need to resolve before asserting the text. You can do something like this:
describe('header Module', function(){
it('should check title text',function(callback){
var header = element(by.css('.title'));
header.getText()
.then(function (text) {
expect(text).toContain('Ionic Blank Starter');
callback();
});
});
});

Karma isn't finding unit tests in *Spec.js

For some reason when I run this Karma.conf the test runner runs without errors but it doesn't actually run any of my tests in the *Spec.js files. Any ideas?
// Karma configuration
// Generated on Fri Jan 03 2014 12:02:32 GMT+0000 (GMT)
module.exports = function (config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// frameworks to use
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'../../root/angular/angular.js',
//'angular/angular-mocks.js',
'../../root/angular/**/*.js',
// 't/js/angularTest/process/filtersSpec.js',
'angularTest/**/*Spec.js'
],
// list of files to exclude
exclude: [
],
// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['progress',"coverage"],
preprocessors:{
'**/*.js':'coverage'
},
// 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, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: ['Chrome'],
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
this is my output from Karma if it helps:
/usr/bin/node /home/me/Desktop/WebStorm-131.515/plugins/js-karma/js_reporter/karma-intellij/lib/intellijRunner.js --karmaPackageDir=/usr/lib/node_modules/karma --serverPort=9876 --urlRoot=/
Testing started at 10:04 ...
Empty test suite.
Process finished with exit code 0
the solution is to not import an angular file:
angular-scenario.js