Common tests are excluded in suite configuration of protractor - jasmine

I could see the common tests are excluded in a suite configuration of protractor. Below is my config.js and there are two scenarios configured in suites.
I'm expecting the test to complete the scenario1 successfully and then Login again as part of scenario2. But, I could see the test ignores 'Login.js', 'CustomerSelection.js', 'Create.js' of Scenario2 and directly proceeds with 'ProductSelection.js'.
Any idea why is that so ? Am I missing anything in conf.js to work the way the scenarios configured ?
Config.js:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {
'browserName': 'chrome'
},
framework: 'jasmine' ,
showColors: true,
suites : {
scenario1: [
'Login.js',
'CustomerSelection.js',
'Create.js',
'View.js',
],
scenario2: [
'Login.js',
'CustomerSelection.js',
'Create.js',
'ProductSelection.js',
]
},
jasmineNodeOpts: {
isVerbose: true,
showColors: true,
print: function () {
},
includeStackTrace: true,
defaultTimeoutInterval: 700000
},
onPrepare: function() {
browser.manage().window().maximize();
browser.manage().timeouts().implicitlyWait(5000);
}
};
Below are the versions I'm using:
protractor: Version 5.4.0
Jasmine: Version 3.2.0
Node: v8.11.1
NPM: Version 5.6.0

If this tests are something that you run always before all. You can place them like this into View.js and ProductSelection.js as part of beforeAll, I'm placing login beforeAll (loginPage is page where my functions are placed, Login() is function in loginPage that logins in application if you send correct username and password to it) like this:
beforeAll(function() {
loginPage.Login(username, password);
});

Related

using ES2015 with mocha, karma and headless chrome for testing

I have a problem with setting up a test environment for a single page application. I am able to run my tests with headless chrome via karma and mocha but I can´t write tests with ES6 Syntax.
My current start command is
karma start --browsers ChromeHeadless karma.config.js --single-run
my karma.config.js
module.exports = function(config) {
config.set({
frameworks: ['mocha', 'chai'],
files: ['test/**/*spec.js'],
reporters: ['nyan'],
port: 9876, // karma web server port
colors: true,
logLevel: config.LOG_INFO,
browsers: ['ChromeHeadless'],
autoWatch: true,
singleRun: false, // Karma captures browsers, runs the tests and exits
concurrency: Infinity,
})
}
I am able to write normal tests but cant use ES6 Syntax here. When I try to import some react components I get this error:
HeadlessChrome 0.0.0 (Linux 0.0.0)
Uncaught SyntaxError: Unexpected token import
at http://localhost:9876/base/test/components.spec.js?b89d2ba6de494310860a60ad2e9e25aea5eb3657:2
So I have to setup babel somehow to compile my test files first. When I try to use compilers: ['js:babel-core/register'] in my karma config its not gonna work.
I also have seen that compilers seems to be deprecated soon so I also tried require: ['babel-core/register'] but it still won´t compile to use ES6 for my test files.
Any idea how to configurate my karma file to write my tests with ES6 ?
Just in case its important. This is my webpack.config.js
const path = require('path');
const ServiceWorkerWebpackPlugin = require('serviceworker-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
inject: 'body'
});
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve('dist'),
filename: 'index_bundle.js'
},
module: {
loaders: [
{test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/},
{test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/}
]
},
plugins: [
new ServiceWorkerWebpackPlugin({
entry: path.join(__dirname, 'src/sw.js'),
}),
HtmlWebpackPluginConfig
],
devServer: {
hot: false,
inline: false,
historyApiFallback: true
}
};
To make things more clear here is a sample project (it's fully runnable, you can fill out files and play around). Just two things to mention: I used jamsine instead of mocha and real 'Chrome' browser instead of headless. Runnable via npm run test command.
files structure
/
karma.conf.js
package.json
sample.js
sampleTest.js
webpack.test.config.js
karma.conf.js:
// Karma configuration
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: ['*Test.js'],
// list of files to exclude
exclude: [],
// preprocess matching files before serving them to the browser
preprocessors: {
'*Test.js': [ 'webpack'] //preprocess with webpack
},
// test results reporter to use
reporters: ['progress'],
// setting up webpack configuration
webpack: require('./webpack.test.config'),
// 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: true,
// start these browsers
browsers: ['Chrome'],
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
// Concurrency level how many browser should be started simultaneous
concurrency: Infinity
})
}
package.json (only relevant stuff):
{
"scripts": {
"test": "node_modules/karma/bin/karma start karma.conf.js"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"jasmine-core": "^2.8.0",
"karma": "^2.0.0",
"karma-chrome-launcher": "^2.2.0",
"karma-jasmine": "^1.1.1",
"karma-webpack": "^2.0.9",
"webpack": "^3.10.0"
}
}
sample.js:
export default function(data){
return data;
}
sampleTest.js:
import sample from 'sample';
describe('Sample', function(){
it('is defined', function(){
expect(sample).toBeDefined();
});
it('returns argument', function(){
expect(sample(0)).toBe(0);
})
});
webpack.test.config.js:
module.exports = {
module: {
rules: [
{
test: /tests\/.*\.js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['babel-preset-env']
}
}
}
]
},
resolve: {
modules: ["node_modules", './'],
extensions: [".js"]
}
};
Karma's webpack plugin is used to inform karma that it should prepare files using webpack and specific webpack configuration before sending them to the browser.
Please note key points:
test files pattern in karma.conf.js
pattern to preprocess files (should match the pattern above)
webpack entry in karma.conf.js file
module entry in webpack.test.config.js
p.s. personally I don't use separate patterns for files, I use a separate file (named, say, tests.webpack.js) to have a single place where the way to find test files is defined:
//make sure you have your directory and regex test set correctly
var context = require.context('.', true, /.*Test\.js$/i);
context.keys().forEach(context);
and have in karma.conf.js (paths are irrelevant to sample project above):
files: [
'tests/tests.webpack.js',
],
preprocessors: {
'./tests/tests.webpack.js': [ 'webpack'] //preprocess with webpack
}
You need to convert ESModule in commonjs module with the babel-plugin-transform-es2015-modules-commonjs plugin
In your .babelrc file :
{
"plugins": [
"transform-es2015-modules-commonjs"
]
}
Update :
You can set the plugin in your webpack configuration :
{
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env'],
plugins: [require('#babel/plugin-transform-es2015-modules-commonjs')]
}
}

Pass karma programmatically arguments

I am using karma with jasmine and the jasmine-spec-tags framework. When starting karma from CLI I use "karma start --tags=MY_TAG" to run only some tests with the tag MY_TAG. This works fine.
Now, I need to start karma programmatically. I tried the following code (note the client.args value at the end):
const server = new Server(
{
autoWatch: true,
browsers: [
'Chrome',
],
files: [
'./node_modules/babel-polyfill/dist/polyfill.js',
'./node_modules/es6-shim/es6-shim.min.js',
'./karma/karma.entry.js'
],
frameworks: ['jasmine', 'jasmine-spec-tags'],
phantomJsLauncher: {
exitOnResourceError: true
},
port: 9876,
preprocessors: {
'./karma/karma.entry.js': ['webpack', 'sourcemap']
},
reporters: ['dots'],
singleRun: false,
webpack: webpackConf,
webpackServer: {
noInfo: true
},
client:
{
args: ['--tags=SchedulingApiService']
}
});
server.start();
This does not work. Am I misunderstanding the client.args value? Would be glad about any help.
To solve this, the client part has to look like this:
client:
{
tags: 'SchedulingApiService'
}

Running multiple specs using Protractor results in a percentage of them timing out

Running multiple test specs using Protractor results in some of them timing out, giving the following error:
Jasmine spec timed out. Resetting the WebDriver Control Flow.
The failure is not consistent; not the same specs fail with each run but a percentage of the specs do and they vary from time to time. below is the config file for Protractor:
'use strict';
exports.config = {
baseUrl: 'http://www.example.com/',
capabilities: {
browserName: 'chrome',
chromeOptions: {
args: [ "--headless", "--disable-gpu"]
},
specs: 'specs/**/*Spec.js'
shardTestFiles: true,
maxInstances: 4
},
"scripts": {
"test": "protractor conf.js",
"test-in-parallel": "node -r parallel-protractor node_modules/.bin/protractor conf.js"
},
useAllAngular2AppRoots: true,
allScriptsTimeout: 30000,
getPageTimeout: 30000,
restartBrowserBetweenTests: true,
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
},
onPrepare: function() {
browser.ignoreSynchronization = false;
}
};
Note: I have tried increasing the timeout interval for Jasmine but a percentage of the specs fail anyway (they just take a longer time to do so)

Custom Jasmine reporter in Protractor tests

I can not find how to change reporter style in protractors runner using jasmine framework.
What I have right now is:
But I would like something more like:
Is there a way to add custom reporter for jasmine that would show current test running instead of DOTS and Fs?
I am building a jasmine reporter that does exactly what you want, jasmine-spec-reporter.
To configure in protractor.conf.js:
onPrepare: function(){
var SpecReporter = require('jasmine-spec-reporter').SpecReporter;
jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: 'all'}));
}
Add the isVerbose flag to the protractor config, it's false by default:
exports.config = {
. . .
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
isVerbose: true
}
};
Add this dependency to your project:
npm install jasmine-spec-reporter --save-dev
And add this to your config file:
onPrepare: function(){
var SpecReporter = require('jasmine-spec-reporter').SpecReporter;
jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: 'all'}));
}
To extend #fer's answer:
You can add these settings to jasmineNodeOpts to both see the current test and get stack trace right when test fails:
jasmineNodeOpts: {
showColors: true,
isVerbose: true,
realtimeFailure: true,
includeStackTrace: true,
defaultTimeoutInterval: 30000
},

How to run Karma using TeamCity reporter using QUnit

I'm trying to run Karma with the TeamCity reporter. But when I run the test suite, it fails with:
Error: No provider for "framework:qunit"! (Resolving: framework:qunit)
This works fine when the output is set to 'progress', but not when I add 'teamcity'.
My karma config looks as follows:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['qunit'],
files: [
'scripts/nml/marco/tests/tempTest.js'
],
exclude: [
],
reporters: ['progress', 'teamcity'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['PhantomJS'],
captureTimeout: 60000,
singleRun: true
});
};
My test is still very simple:
(function () {
test('Test one equals one', function () {
equal(1, 1);
});
})();
Any ideas?
I figured out my problem. When I installed the TeamCity reporter, I did it to my current folder instead of the global karma folder. So I think the runner got confused with only a small set of files being in the current folder (and that overrode the global settings).
I was wrong, the tests did not pass any more when just running with the 'progress' reporter.

Resources