Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I imlement soft Assertion in my Cypress test

I am struggling with implementing soft assertions in my Cypress test.
I need to convert all the assertions to soft assertions. The problem I encounter is that I cannot locate the element in the jsonAssertion. For example cy.get(‘span[class="h4"]’) is the element and I need to assert that it contains some text. How can this be done with jsonAssertion.softAssert()?

This is my test:

describe('Load Validation Test', function(){
  const jsonAssertion = require("soft-assert")

  it('Load Validation Test', function(){
      let url = Cypress.config().baseUrl
      
      cy.visit(url+'activityTaskManagement')

      cy.get('span[class="h4"]').should('contain.text','Manage Activities')
      cy.get('button[ng-click="vm.addActivityTask();"]').should('be.visible')
      cy.get('button[ng-click="vm.addActivityTaskBulk();"]').should('be.visible')
      cy.get('input[placeholder="Activity Name"]').should('be.visible')
      cy.get('div table[class="table table-striped b-t b-light table-nowrap"]').should('be.visible')
      
  })
})

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

For soft-assert, see How can i use soft assertion in Cypress

As custom commands,

const jsonAssertion = require("soft-assert")

Cypress.Commands.add('softAssert', (actual, expected, message) => {
  jsonAssertion.softAssert(actual, expected, message)
  if (jsonAssertion.jsonDiffArray.length) {
    jsonAssertion.jsonDiffArray.forEach(diff => {

      const log = Cypress.log({
        name: 'Soft assertion error',
        displayName: 'softAssert',
        message: diff.error.message
      })
    
    })
  }
});
Cypress.Commands.add('softAssertAll', () => jsonAssertion.softAssertAll())

In the test

cy.get('span[class="h4"]').then($el=> {
  const actual = $el.text()
  cy.softAssert(actual, 'Manage Activities')
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading