Protractor-beautiful-reporter sponsored by SauseLab is a really interesting reporter. But the challenge that one face is that it won’t work with fail fast and thus the screenshots may not be taken at the correct time
The work around would be to create separate function to throw the expect message, we will create a new jasmine environment so that we can re-use jasmine expectations
Config File:
Create a global function that takes in the jasmine expectation and throws the error message.
We will add this global function to Onprepare , so that it will be available throught out our specs.
onPrepare: function () {
//create a global assertion function which take in a callback function
global.assertExpectation = function assertExpectation(callback) {
const env = new jasmine.Env();
const spec = env.it("", async function() {
//pass jasmine expect to the call back function
await callback(env.expect);
});
env.execute()
//access the spec and print out the message as error along with the stack to know where exactly this error thrown in our actual spec.
if (spec.result.failedExpectations[0] !== undefined) {
throw new Error(spec.result.failedExpectations[0].message).stack;
}
}
// your remaining onprepare and config code
Now in the spec call the function:
describe('Validate expectations', function () {
it('test', async function() {
await browser.get("https://www.npmjs.com");
// our callback function is a function that triggers the assession
// unless env.expect is passed , expect is just an undefined variable inside the callback
assertExpectation(function(expect) {
expect("This sentence ").toContain("nothing");
expect("THis to ").toBe(5);
});
await browser.get("https://www.protractortest.org/#/jasmine-upgrade");
assertExpectation(function(expect) {
expect("THis to ").toContain("p");
});
});
});
New Output:
You can see that the error show in which line of spec the expectation was thrown (13:5) and the correct screenshot (npmjs website)

Previous output:
Here we can see that expectation failed after get(npmjs) but the screenshot is of protractor website as jasmine asserts are softAsserts.
