JScript: Protractor and Jasmine reporters - jasmine

I am really new with programming and developing (I know exactly 1 syntax) but I want to test a part of a website and also generate an output report. Protractor is working quite alright, but I cant seem to get the reporter to work.
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
allScriptsTimeout: 15000,
//baseUrl: 'http://localhost:4444/wd/hub',
specs: ['x.js'],
framework: 'jasmine2',
rootElement: '.ocf-widget-savings',
multiCapabilities: [
{
'browserName': 'chrome',
},
],
onPrepare: function(){
var capsPromise = browser.getCapabilities();
capsPromise.then(function(caps){
var browserName = caps.caps_.browserName.toUpperCase();
var browserVersion = caps.caps_.version;
var prePendStr = browserName + "-" + browserVersion + "-";
var jasmineReporters = require('jasmine-reporters');
var htmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
consolidateAll: true,
filePrefix: prePendStr+'xmloutput',
savePath: 'Testing Protractor/testresults'
}));
jasmine.getEnv().addReporter(
new htmlScreenshotReporter({
dest: './screenshots',
captureOnlyFailedSpecs: true,
filename: prePendStr+'my-report.html'
})
);
});
},
Its now throwing the following error
var result = fn();
does anyone have any clue about what I am doing wrong here? Help would be greatly appreciated.

Sounds like an installation problem.
Do:
npm install --save-dev jasmine-reporters#^2.0.0
and try again.

Related

Onprepare with jasmine-reporters causes failures while executing with IE

After some time trying to figure out why the beginning of my tests fails (only for IE, with chrome works just fine), I found out that it is caused by the on-prepare function when comes to this part of the code:
jasmine.getEnv().addReporter({
specDone: function (result) {
browser.getCapabilities().then(function (caps)
{
var browserName = caps.get('browserName');
browser.takeScreenshot().then(function (png) {
var stream = fs.createWriteStream('./execution_results/reports/results/screenshots/' + browserName + '-' + result.fullName+ '.png');
stream.write(new Buffer.from(png, 'base64'));
stream.end();
});
});
}
});
If i comment this part, the tests goes smoothly.
My login page is not Angular, so I turn off the sync for the login and turn on again, not sure if this could be related.
How can I force protractor to wait for this part to finish before continuing with the run?
I already tried to add this code in a promise (in conf file) to make protractor wait but even with this i get the jasmine timeout 'TimeoutError: Wait timed out after 20000ms', so I believe I did it wrong.
The error I get is:
Failed: Unable to determine type from: E. Last 1 characters read: E
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'xxxxx', ip: 'xx.xx.xx.xx', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '10.0.2'
Driver info: driver.version: unknown
Full conf file:
var jasmineReporters = require('./lib/node_modules/jasmine-reporters');
var HTMLReport = require('./lib/node_modules/protractor-html-reporter-2');
var mkdirp = require('./lib/node_modules/mkdirp');
var fs = require('./lib/node_modules/fs-extra');
let date = require('./lib/node_modules/date-and-time');
var environmentToExecute = 'https://myportal'
exports.config = {
seleniumAddress: 'http://'+process.env.AUTOTEST_ADDRESS+'/wd/hub',
framework: 'jasmine2',
specs: ['all my specs'],
suites: {
//All my suites
},
allScriptsTimeout: 20000,
onPrepare: function () {
{
//Here I create the folders (removed to make it shorter)
}
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
consolidateAll: true,
savePath: './execution_results/reports/xml/',
filePrefix: 'xmlresults'
}));
jasmine.getEnv().addReporter({
specDone: function (result) {
browser.getCapabilities().then(function (caps)
{
var browserName = caps.get('browserName');
browser.takeScreenshot().then(function (png) {
var stream = fs.createWriteStream('./execution_results/reports/results/screenshots/' + browserName + '-' + result.fullName+ '.png');
stream.write(new Buffer.from(png, 'base64'));
stream.end();
});
});
}
});
},
//HTMLReport called once tests are finished
onComplete: function()
{
//I removed this to make it shorter, but basically it is the function
// that comverts the xml in html and build the report
},
jasmineNodeOpts: {
showColors: true, // Use colors in the command line report.
// If true, display spec names.
isVerbose: true,
defaultTimeoutInterval: 100000
},
params: {
//Other files like functions and so on...
},
login:{
//parameters to login
}
},
multiCapabilities:
[
{
'browserName': 'internet explorer',
'version': 11,
},
/*
//chrome, firefox...
*/
],
};//end of Conf.js
Thanks!
I also had issues with asynchronous actions in a Jasmine reporter recently and unfortunately could not figure out how to get them to await promise results properly before moving on. If anyone else has information on this I would greatly appreciate it also.
I did implement a work around using global variables and the AfterAll hook which is able to correctly await promises which may work for you.
I'm assuming that you only need the 'fullname' property of your result so you can try this.
Declare a global properties in your onPrepare and you can assigned this global variable values in your reporter. Assign it the spec fullname value inside of specStarted instead of specDone. Then you can create you screenshot inside you tests afterAll statements which are correctly able to await promise results.
onPrepare: function () {
global.currentlyExecutingSpec = 'tbd';
jasmine.getEnv().addReporter({
specStarted: function (result) {
currentlyExecutingSpec = result.fullName
}
})
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
consolidateAll: true,
savePath: './execution_results/reports/xml/',
filePrefix: 'xmlresults'
}));
}
Inside your testFiles
afterEach(function(){
browser.getCapabilities().then(function (caps)
{
var browserName = caps.get('browserName');
browser.takeScreenshot().then(function (png) {
var stream =
fs.createWriteStream('./execution_results/reports/results/screenshots/' + browserName + '-' + currentlyExecutingSpec + '.png');
stream.write(new Buffer.from(png, 'base64'));
stream.end();
});
};
});

Protractor - Suite logs are seperated into multiple suites in Jasmine reporters

I tried to run a test suite with 2 .js test case files.
The report config is in onPrepare part of conf.js below:
onPrepare: function () {
var jasmineReporters = require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
consolidate: true,
savePath: path = './Logs/' + dateFormat(new Date(), "dddd_mmmm_dS_yyyy_h_MM_ss_TT"),
filePrefix: result = 'xmlresults',
}));
var fs = require('fs-extra');
fs.emptyDir(path + '/screenshots/', function (err) {
console.log(err);
});
jasmine.getEnv().addReporter({
specDone: function () {
if (specs.status == 'failed') {
browser.getCapabilities().then(function (caps) {
var browserName = caps.get('browserName');
browser.takeScreenshot().then(function (png) {
var stream = fs.createWriteStream('screenshots/' + browserName + '-' + specs.fullName + '.png');
stream.write(new Buffer(png, 'base64'));
stream.end();
});
});
}
}
});
},
onComplete: function () {
var browserName, browserVersion;
var capsPromise = browser.getCapabilities();
capsPromise.then(function (caps) {
browserName = caps.get('browserName');
browserVersion = caps.get('version');
platform = caps.get('platform');
var HTMLReport = require('protractor-html-reporter-2');
testConfig = {
reportTitle: 'Protractor Test Execution Report',
outputPath: path,
outputFilename: 'htmlresult',
screenshotPath: './screenshots',
testBrowser: browserName,
browserVersion: browserVersion,
modifiedSuiteName: false,
screenshotsOnlyOnFailure: true,
testPlatform: platform
};
new HTMLReport().from(path + '/' + result + '.xml', testConfig);
});
},
After execution, the jasmine html reporter as picture:jasmine html reporter
Can anyone help me to make the reports gather into 1 test suite report?
Thank you very much

Protractor jasmine empty test spec timeout issue

I'm receiving very strange test spec behavior when trying to run my jasmine specs with protractor.
I have two empty specs that both should pass, however my first spec passes then all proceeding specs fail. I believe this may have something to do with the version levels, as when I did an update it caused my jasmine test cases to break.
Protractor 3.3.0
Jasmine 2.4.1
Test specs
it('test spec 1', function () {
});
it('test spec 2', function () {
});
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
'use strict';
exports.config = {
seleniumAddress: 'http://127.0.0.1:4723/wd/hub',
baseUrl: 'http://10.0.2.2:' + (process.env.HTTP_PORT || '8000'),
specs: [
'./e2e-test.js'
],
framework: 'jasmine',
jasmineNodeOpts: {
showColors: true,
isVerbose: true,
defaultTimeoutInterval: 30000,
print: function() {}
},
capabilities: {
deviceName:"Samsung S7",
platformName: 'Android',
'appium-version': '1.4.16',
platformVersion:'23',
app: 'C:/Users/egreen/Desktop/Android/foo/platforms/android/build/outputs/apk/android-debug.apk',
browserName:'',
udid:'988627534e4c383848',
autoWebview: true
},
// A callback function called when tests are started
onPrepare: function () {
var wd = require('wd'),
protractor = require('protractor'),
wdBridge = require('wd-bridge')(protractor, wd);
wdBridge.initFromProtractor(exports.config);
require('jasmine-reporters');
var fs = require('fs'),
d = new Date(),
date = [
d.getFullYear(),
('0' + (d.getMonth() + 1)).slice(-2),
('0' + d.getDate()).slice(-2)
].join('-'),
time = [
('0'+d.getHours()).slice(-2),
(('0'+d.getMinutes()).slice(-2)),
('0'+d.getSeconds()).slice(-2)
].join('');
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter');
jasmine.getEnv().addReporter(
new Jasmine2HtmlReporter({
savePath: 'target/reports/mobile-app/'+date+'/'+time+'/',
screenshotsFolder: 'images'
})
);
var SpecReporter = require('jasmine-spec-reporter');
jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: 'all'}));
},
};
Error gist
Updated :
Try to eliminate Jasmine2HtmlReporter.
Try to add :
describe("long asynchronous specs", function() {
beforeEach(function(done) {
done();
}, 10000);
// Your code here
afterEach(function(done) {
done();
}, 10000);
}
You can also have a look into : Jasmine Asynchronous Support
Or try to add time out here :
it('test spec 1', function () {
},1000);
it('test spec 2', function () {
},1000);

error when running protractor with jasmine-reporters

I've installed jasmine reporters using this command:
npm install --save-dev jasmine-reporters#^2.0.0
this is the important part of the config file:
jasmineNodeOpts: {
isVerbose: true,
showColors: true,
defaultTimeoutInterval: 360000,
includeStackTrace: true
},
framework: 'jasmine2',
onPrepare: function() {
var jasmineReporters = require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
consolidateAll: true,
savePath: 'testresults',
filePrefix: 'xmloutput'
}));
browser.ignoreSynchronization = true;
}
when running the test, after s while I get this:
Failed: Cannot call method 'results' of undefined
Stack:
Error: Failed: Cannot call method 'results' of undefined
at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:102:16
at [object Object].promise.ControlFlow.runInFrame_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:1877:20)
at [object Object].promise.Callback_.goog.defineClass.notify (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:2464:25)
at [object Object].promise.Promise.notify_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:563:12)
any ideas?
UPDATE:
after updating protractor to 2.1.0, I'm getting a little different error:
Failed: Cannot call method 'results' of undefined
Stack:
TypeError: Cannot call method 'results' of undefined
at Object.exports.takeScreenshotOnFailure (/Users/myuser/Workspace/Spark/FrontEnd/test/spec/e2e/global/screenshots.js:23:13)
at Object.<anonymous> (/Users/myuser/Workspace/Spark/FrontEnd/test/spec/e2e/CreateApp/createAppTest.js:23:16)
From: Task: Run afterEach in control flow
at Array.forEach (native)
screenshots.js:
var fs = require('fs');
var spec=jasmine.getEnv().currentSpec;
function capture(name) {
browser.takeScreenshot().then(function (png) {
var stream = fs.createWriteStream('screenshots/' + name + '.png');
stream.write(new Buffer(png, 'base64'));
stream.end();
})
}
exports.takeScreenshot = function (spec) {
capture(spec.description.split(' ').join('_'));
};
exports.takeScreenshotOnFailure = function (spec) {
if (spec.results().passed()) return;
capture(spec.description.split(' ').join('_'));
};
conf.js (relevant part):
jasmineNodeOpts: {
isVerbose: true,
showColors: true,
defaultTimeoutInterval: 360000,
includeStackTrace: true
},
framework: 'jasmine2',
onPrepare: function() {
var jasmineReporters = require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
consolidateAll: true,
savePath: '/TestResults',
filePrefix: 'xmloutput'
}));
browser.ignoreSynchronization = true;
}
This actually sounds closely related to What line is causing this Protractor error?, upgrade protractor to 2.1.0 or a newer version:
npm install protractor#^2.1.0
Also, since you are on jasmine2, you need to handle spec failures differently, see:
How can I get screenshots of failures?

CasperJS with JSON.parse

When I run the following code, I get the following:
TypeError: 'undefined' is not a function (evaluating 'this.emit('error', error)')
I asked this earlier, but im rephrasing. It appears that the JSON object is undefined in the casper function.
If I do JSON.parse() outside, then its not undefined.
Thoughts on how to get this working?
var casper = require("casper").create({
verbose: true,
logLevel: 'debug',
});
var site = 'http://my.internalsite.com';
casper.start(site);
casper.run(function() {
var currentURL = this.getCurrentUrl();
this.echo('URL: ' + currentURL);
var json_string = JSON.parse(this.getPageContent());
this.echo(json_string);
this.exit();
});
This could possibly be due to this.exit() getting called before JSON.parse(this.getPageContent())
You could try the following:
var casper = require("casper").create({
verbose: true,
logLevel: 'debug',
});
var site = 'http://xkcd.com/info.0.json';
casper.start(site);
casper.then(function() {
var currentURL = this.getCurrentUrl();
this.echo('URL: ' + currentURL);
var json_string = JSON.parse(this.getPageContent());
require('utils').dump(json_string);
});
casper.run();

Resources