what is onprepare and onComplete in protector or jasmine? - jasmine

While trying to execute conf.js file in protector everything working file while generating report from xml to html screenshot not attached to html report.
Also why onprepare and onComplete excuted by default . Wants to know about implementaion of these.
Any clarification will be helpfull with related github source code.
exports.config = {
framework: 'jasmine2',
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['spec.js'],
capabilities: {
browserName: 'chrome'
},
onPrepare: function () {
console.log('on prepared called');
var jasmineReporters = require('jasmine-reporters');
jasmine.getEnv().addReporter(new . jasmineReporters.JUnitXmlReporter({
consolidateAll: true,
//savePath: '.',
filePrefix: 'xmlresults.xml'
}));
},
onComplete: function() {
console.log('on complete called');
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: './',
outputFilename: 'ProtractorTestReport',
screenshotPath: './screenshots',
testBrowser: browserName,
browserVersion: browserVersion,
modifiedSuiteName: false,
screenshotsOnlyOnFailure: true,
testPlatform: platform
};
new HTMLReport().from('xmlresults.xml', testConfig);
});
}
}

onPrepare and onComplete are referred to as lifecycle hooks and are executed at particular stages of your execution. There are many lifecycle hooks available to protractor through various means including from the jasmine reporters you declare, as you've mentioned.
Overview of lifecycle hooks and the order they are triggered
--- beforeLaunch
--- onPrepare (set in conf) ***reporters initialized here
--- jasmineStarted (set in reporter)
--- beforeAll
--- suiteStarted (set in reporter)
--- specStarted (set in reporter)
--- beforeEach (set in testFile)
+++ afterEach (set in testFile)
+++ specDone (set in reporter)
+++ suiteDone (set in reporter)
+++ afterAll
+++ jasmineDone (set in reporter)
+++ onComplete (set in conf)
+++ afterLaunch
It's not clear what your issue is from your question but I am assuming you are having issues with your html reporter because you are declaring it in the onComplete.
Jasmine reporters have a few important lifecycle hooks themselves:
(jasmineStarted, jasmineDone, suiteStarted, suiteDone, specStarted, specDone)
but, if you look at the above overview I pasted, you can see that those reporter lifecycle hooks all take place before the onComplete is called. If you declare your reporter in the onComplete instead of the onPrepare these lifecycle stages will already has passed and no actions will be taken on them.
You can read more about the purpose of the Protractor lifecycle hooks in the attached link.
https://github.com/angular/protractor/blob/master/exampleTypescript/conf.ts
and the Jasmine Reporter lifecycle hooks here.
https://jasmine.github.io/api/3.3/Reporter.html
Hope that answers you questions but let me know if I have misunderstood.

Related

Jasmine get current test result

I am using Jasmine - 3.3.1, with combination of ProtractorJS.
My requirement is to store the result for each spec (or describe / test) and update results in Testrail system using the afterEach() method. I want to store the results into the variable "testResult".
Tried with various methods - custom_reports.js etc, but could not get what I needed.
Code snippet:
var testResult;
describe('1st scenario', function () {
it('1st Test', function () {
expect(true).toBe(true);
testResult=5;
});
});
describe('2nd scenario', function () {
it('2nd Test', function () {
expect(true).toBe(true);
testResult=1;
});
});
afterEach(function () {
helper.updateResults("Section Name", testcaseID, testResult);
});
I achieved something similar by creating my own custom reporter. My reporter uploads spec results (it blocks) to a dynamoDB table after each spec finishes and uploads suite results (describe blocks) after all tests have finished. All uploads occur asynchronously but in the onComplete all of the async upload actions are awaited.
Obviously I am using the async / await approach as opposed to the SELENIUM_PROMISE_MANAGER you appear to. I would recommend making that change over.
DBReporter.js
function dbReporter() {
this.jasmineStarted = function (options) {};
this.specStarted = function (result) {};
this.specDone = async function (result) {
if (result.status == 'pending') {
}
else if (result.status == 'passed') {
}
else if (result.status == 'failed') {
//Put your testrail interaction code here
}
testResultsUploadQueue.push(result);
};
this.suiteStarted = function (result) {};
this.suiteDone = function (result) {}
this.jasmineDone = async function (result) {}
}
module.exports = dbReporter;
conf.js
onPrepare: async () => {
//require the dpReporter file
let dbReporter = require('../src/functions/db-reporter');
//Declare a global variable that will contain all the asynchronous upload actions (promises)
global.testResultsUploadQueue = [];
//initialize the dbreporer
await jasmine.getEnv().addReporter(new dbReporter());
}),
onComplete: async() => {
//Wait for all uploads to resolve before completing
let testRulesUploadValue = await Promise.all(testResultsUploadQueue);
console.log(` ${testRulesUploadValue.length} result files uploaded to dynamoDB`);
}
No changes required in your spec files
The Constraints
I had a lot of issues with handling asynchronous actions with the reporter, this was why I chose to use the queue approach. I could not figure out how to get around this but this approach does work.
Your TestRail actions must return a promise
It is important to understand the execution order of the hooks in order to understand the solution.
--- beforeLaunch
--- onPrepare
--- jasmineStarted (set in jasmine reporter)
--- beforeAll
--- suiteStarted (set in jasmine reporter)
--- specStarted (set in jasmine reporter)
--- beforeEach
+++ afterEach
+++ specDone (set in jasmine reporter)
+++ suiteDone (set in jasmine reporter)
+++ afterAll
+++ jasmineDone (set in jasmine reporter)
+++ onComplete
+++ afterLaunch

Show specs texts with Jasmine 2, Protractor and Gulp

I have a Gulp configuration file that runs Protractor/Jasmine like this:
.pipe($.protractor.protractor({ configFile: 'protractor.conf.js', args: args || ['--baseUrl', 'http://localhost:' + basePort] }))
But the report shown in the console are just dots, instead of the actual specs tests. How can I make Jasmine be verbose?
This was possible with the isVerbose option in Jasmine 1, but I can't find the equivalent for Jasmine 2.
What we are doing is setting these jasmine settings:
jasmineNodeOpts: {
showColors: true,
isVerbose: true,
includeStackTrace: true,
defaultTimeoutInterval: 100000,
print: function() {}
}
And using jasmine-spec-reporter package that provides a nice and detailed test output:
onPrepare: function () {
var SpecReporter = require('jasmine-spec-reporter');
// jasmine spec reporter
jasmine.getEnv().addReporter(new SpecReporter({
displayStacktrace: 'all',
displayPendingSpec: true,
displaySpecDuration: true
}));
}

how does phantomjs work with protractor?

I am getting my head around protractor and phantomjs. The test looks like this, it works fine with
just chrome:
describe('angularjs homepage', function () {
beforeEach(function () {
browser.driver.manage().window().setSize(1124, 850);
});
it('should greet the named user', function () {
browser.driver.get('https://angularjs.org/');
element(by.model('yourName')).sendKeys('Julie');
var greeting = element(by.binding('yourName'));
expect(greeting.getText()).toEqual('Hello Julie!');
});
});
the protractor.config looks like this :
// An example configuration file.
exports.config = {
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'phantomjs',
'phantomjs.binary.path': 'C:/ptor_testing/node_modules/phantomjs/lib/phantom/phantomjs.exe'
},
// For speed, let's just use the Chrome browser.
//chromeOnly: true,
// Spec patterns are relative to the current working directly when
// protractor is called.
specs: ['example.spec.js'],
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
// onComplete will be called just before the driver quits.
onComplete: null,
// If true, display spec names.
isVerbose: true,
// If true, print colors to the terminal.
showColors: true,
// If true, include stack traces in failures.
includeStackTrace: true,
// Default time to wait in ms before a test fails.
defaultTimeoutInterval: 30000
},
seleniumAddress: 'http://localhost:4444/wd/hub'
};
When I run this i get:
Error: Error while waiting for Protractor to sync with the page: {"message":"Can't find variable: angular","name":"ReferenceError","line":4,"stack":"ReferenceError: Can't find variable: angular\n at :4\n at anonymous (:13)\n at Na (phantomjs://webpage.evaluate():14)\n at phantomjs://webpage.evaluate():15\n at phantomjs://webpage.evaluate():15\n at phantomjs://webpage.evaluate():16\n at phantomjs://webpage.evaluate():16\n at phantomjs://webpage.evaluate():16","stackArray":[{"sourceURL":"","line":4},{"sourceURL":"","line":13,"function":"anonymous"},{"sourceURL":"phantomjs://webpage.evaluate()","line":14,"function":"Na"},{"sourceURL":"phantomjs://webpage.evaluate()","line":15},{"sourceURL":"phantomjs://webpage.evaluate()","line":15},{"sourceURL":"phantomjs://webpage.evaluate()","line":16},{"sourceURL":"phantomjs://webpage.evaluate()","line":16},{"sourceURL":"phantomjs://webpage.evaluate()","line":16}],"sourceId":81819056}
How can I fix this?

Skipping a test in Qunit

I just found qHint, a method to integrate jsHint testing into Qunit... but it doesn't work locally (I don't mean localhost) except in Firefox.
So I wanted to add a "warning" or "notice", NOT a test failure, showing that the test was skipped:
// do unit test if not local or local and running Firefox
t = QUnit.isLocal;
if (!t || (t && /Firefox/.test(navigator.userAgent))) {
jsHintTest('JSHint core check', 'js/myplugin.js');
} else {
test('JSHint core check (skipped)', function(){
ok( true, 'check not done locally' );
});
}
I would just like to make it more obvious that a test was skipped, is this possible?
Update: Thanks to Odi for the answer!, but I had to make a slight modification to make the code work in QUnit v1.11.0pre:
QUnit.testSkip = function( testName, callback ) {
QUnit.test(testName + ' (SKIPPED)', function() {
if (typeof callback === "function") {
callback();
}
var li = document.getElementById(QUnit.config.current.id);
QUnit.done(function() {
li.style.background = '#FFFF99';
});
});
};
testSkip = QUnit.testSkip;
I had the same requirement and I simply defined a new kind of test() that I called testSkip().
This test method simply replaces your test function and changes the name to <test name> (SKIPPED). After that the test is considered passed by QUnit.
To further indicate that this is a skipped test, I added a callback function to QUnit.done for each skipped test to change the color of the test in the HTML output to yellow. These callbacks are executed when the test suite is done. Setting the value directly does not work, because QUnit applies the styles for passed/failed tests at the end of the run.
QUnit.testSkip = function() {
QUnit.test(arguments[0] + ' (SKIPPED)', function() {
QUnit.expect(0);//dont expect any tests
var li = document.getElementById(QUnit.config.current.id);
QUnit.done(function() {
li.style.background = '#FFFF99';
});
});
};
testSkip = QUnit.testSkip;
Then you can use testSkip() instead of test() for skipped tests.
For my test suite the result looks like that:
For anyone who may have glazed over the comments, Mottie's comment on the question points out that Qunit now has a skip() function. Just replace any call to test() with skip() to skip that test.

How do I focus on one spec in jasmine.js?

I have a bunch of failing specs from a rather large architectural change. I'd like to work on fixing them one by one by tagging each one with 'focus'.
Does jasmine.js have a feature like this? I swore I read at one point that it does but I don't see it in the docs.
When using Karma, you can enable only one test with fit or fdescribe (iit and ddescribe in Jasmine before 2.1).
This only runs Spec1:
// or "ddescribe" in Jasmine prior 2.1
fdescribe('Spec1', function () {
it('should do something', function () {
// ...
});
});
describe('Spec2', function () {
it('should do something', function () {
// ...
});
});
This only runs testA:
describe('Spec1', function () {
// or "iit" in Jasmine prior 2.1
fit('testA', function () {
// ...
});
it('testB', function () {
// ...
});
});
In core since 2.1 with fit and fdescribe.
You can run a single spec by using the url for the spec
describe("MySpec", function() {
it('function 1', function() {
//...
})
it('function 2', function() {
//...
}
})
Now you can run just the whole spec by this url http://localhost:8888?spec=MySpec and a the first test with http://localhost:8888?spec=MySpec+function+1
There are a few ways you can do it.
There is: Jasmine's feature Focused Specs (2.2): http://jasmine.github.io/2.2/focused_specs.html
Focusing specs will make it so that they are the only specs that run. Any spec declared with fit is focused.
describe("Focused specs", function() {
fit("is focused and will run", function() {
expect(true).toBeTruthy();
});
it('is not focused and will not run', function(){
expect(true).toBeFalsy();
});
});
However, I don't really like the idea of editing my tests (fit and fdescribe) to run them selectively. I prefer to use a test runner like karma which can filter out tests using a regular expression.
Here's an example using grunt.
$ grunt karma:dev watch --grep=mypattern
If you're using gulp (which is my favourite task runner), you can pass args into gulp-karma with yargs and match patterns by setting karma's config.
Kinda like this:
var Args = function(yargs) {
var _match = yargs.m || yargs.match;
var _file = yargs.f || yargs.file;
return {
match: function() { if (_match) { return {args: ['--grep', _match]} } }
};
}(args.argv);
var Tasks = function() {
var test = function() {
return gulp.src(Files.testFiles)
.pipe(karma({ configFile: 'karma.conf.js', client: Args.match()}))
.on('error', function(err) { throw err; });
};
return {
test: function() { return test() }
}
}(Args);
gulp.task('default', ['build'], Tasks.test);
See my gist: https://gist.github.com/rimian/0f9b88266a0f63696f21
So now, I can run a single spec using the description:
My local test run: (Executed 1 of 14 (skipped 13))
gulp -m 'triggers the event when the API returns success'
[20:59:14] Using gulpfile ~/gulpfile.js
[20:59:14] Starting 'clean'...
[20:59:14] Finished 'clean' after 2.25 ms
[20:59:14] Starting 'build'...
[20:59:14] Finished 'build' after 17 ms
[20:59:14] Starting 'default'...
[20:59:14] Starting Karma server...
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [watcher]: All files matched by "/spec/karma.conf.js" were excluded.
INFO [Chrome 42.0.2311 (Mac OS X 10.10.3)]: Connected on socket hivjQFvQbPdNT5Hje2x2 with id 44705181
Chrome 42.0.2311 (Mac OS X 10.10.3): Executed 1 of 14 (skipped 13) SUCCESS (0.012 secs / 0.009 secs)
[20:59:16] Finished 'default' after 2.08 s
Also see: https://github.com/karma-runner/karma-jasmine
For anyone stumbling upon this, a better approach, which you can set up from the code itself, is to use this plugin: https://github.com/davemo/jasmine-only
It allows you set the spec exclusivity right on the code like this:
describe.only("MySpec", function() {
it('function 1', function() {
//...
})
it.only('function 2', function() {
//...
}
})
// This won't be run if there are specs using describe.only/ddescribe or it.only/iit
describe("Spec 2", function(){})
There has been a long discussion to get this added to Jasmine core, see: https://github.com/pivotal/jasmine/pull/309
If you happen to be using Jasmine via Karma/Testacular you should already have access to ddescribe() and iit()
You can create your all your specs up front but disable them with xdescribe and xit until you're ready to test them.
describe('BuckRogers', function () {
it('shoots aliens', function () {
// this will be tested
});
xit('rescues women', function () {
// this won't
});
});
// this whole function will be ignored
xdescribe('Alien', function () {
it('dies when shot', function () {
});
});
This is the most simplified answer with a practical example .Even in fdescribe you can run few it blocks using it. f means focus.
Also in a none fdescribe block which is just describe, you can select only specific it blocks by marking them as fit.
Please run the below code and observe the console log, also read the comments in the code.
Read this author's article it helps too . https://davidtang.io/2016/01/03/controlling-which-tests-run-in-jasmine.html
//If you want to run few describe only add f so using focus those describe blocks and it's it block get run
fdescribe("focus description i get run with all my it blocks ", function() {
it("1 it in fdescribe get executed", function() {
console.log("1 it in fdescribe get executed unless no fit within describe");
});
it("2 it in fdescribe get executed", function() {
console.log("2 it in fdescribe get executed unless no fit within describe");
});
//but if you and fit in fdescribe block only the fit blocks get executed
fit("3 only fit blocks in fdescribe get executed", function() {
console.log("If there is a fit in fdescribe only fit blocks get executed");
});
});
describe("none description i get skipped with all my it blocks ", function() {
it("1 it in none describe get skipped", function() {
console.log("1 it in none describe get skipped");
});
it("2 it in none describe get skipped", function() {
console.log("2 it in none describe get skipped");
});
//What happen if we had fit in a none fdescribe block will it get run ? yes
fit("3 fit in none describe get executed too eventhough it;s just describe ", function() {
console.log("3 fit in none describe get executed too");
});
});
With stand-alone Jasmine(2.0.0), on the spec_runner.htlm, I could click a specific spec and focus on that one spec. I should have noticed this feature earlier.
Not exactly what you've asked for but adding iit will test only that particular spec and ignore all others in the file, ddescribe works in the same way. So you can focus on a particular spec using iit or ddescribe

Resources