top of page
  • Writer's picturekhyati sehgal

Soft assertions in protractor based typescript automation framework

How many of us has seen that while running code at once glance if the code fails at some step, then other test steps which meant to run post that step is marked as skipped in the final result?

Also, how about rephrasing the same sentence such that the code does not run and marked as skipped in the final run-results challenging the law of ‘fail-fast’. By the execution, we can not infer whether the code was wrong or the functionality dint work as it was an incomplete run for the one who ran it. I am sure many of the QA folks can relate to this. To be more precise, can we relate to this console screenshot.

If you you have landed the right place!

In our project we also faced similar issues and this is how we resolved it.We used a library called SoftAssertions. For that you just need to follow some simple steps

Installation

npm i soft-assert –save-dev

This will install the soft assertion on the system and in your project. You shall see something like this, once installed.

Now create a file name SoftAssertion in the project. I have added it in the utilities package

import { ElementArrayFinder } from "protractor";
import { Log4jConfig } from "../com.project_name.utilities/Log4jConfig";
const chai = require("chai").use(require("chai-as-promised"));
const expect = chai.expect;

export class SoftAssertion {
    public static async verify(data: string, elementArrayFinder: ElementArrayFinder, ...boolean: boolean[]) {
        const expectedResult = new Map();
        const actualResult = new Map();
        expectedResult.set("Created role is present after deletion", false);
        actualResult.set("Created role is present after deletion", elementArrayFinder);
        await Log4jConfig.Log().debug(await Promise.all(expectedResult));
        await expect(await Promise.all(expectedResult)).to.equal(actualResult);
    }

} 

This will give you a list of methods which you can use like

deepAssert(actual, expected, msg, ignoreKeys)
softAssert(actual, expected, msg, ignoreKeys)
deepContains(actual, expected, msg, ignoreKeys)
softContains(actual, expected, msg, ignoreKeys)
deepAssertKey(actual, expected, key, msg, ignoreKeys)
softAssertKey(actual, expected, key, msg, ignoreKeys)
deepContainstKey(actual, expected, key, msg, ignoreKeys)
softContainsKey(actual, expected, key, msg, ignoreKeys)
deeptTrue(value, msg)
softTrue(value, msg)
deepAssertKeyAbsence(actual, key, msg)
softAssertKeyAbsence(actual, key, msg)
softAssertAll()
 

How to use it in page test classes.

For that you need to do create a const of the jsonAssertion in your class wherein you want to use it and then use any method of your choice to apply assertions on.


Create a constant/variable
const jsonAssertion = require("../../../node_modules/soft-assert/lib/assertion");
Use of deep Assertion method
jsonAssertion.deepAssert(await this.lblDuration.isPresent(),false);
Use of contains method for string match
jsonAssertion.softContains(await this.disableTxt.getText()).contain(Automation.categoryDisabled); 

Once done we can reach to a point wherein we can see the results in the below form, hence making sure that the whole code has run once as a whole i.e no skipped line of code.

3 views0 comments

Recent Posts

See All
bottom of page