如何在 Cypress 测试中实现软断言

Geo*_*iev 5 automated-tests cypress

我正在努力在我的赛普拉斯测试中实现软断言。我需要将所有断言转换为软断言。我遇到的问题是我无法在 jsonAssertion 中找到该元素。例如cy.get('span[class="h4"]')是元素,我需要断言它包含一些文本。如何使用jsonAssertion.softAssert()来完成此操作?

这是我的测试:

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')
      
  })
})
Run Code Online (Sandbox Code Playgroud)

Fod*_*ody 6

对于软断言,请参阅如何在 Cypress 中使用软断言

作为自定义命令,

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())
Run Code Online (Sandbox Code Playgroud)

在测试中

cy.get('span[class="h4"]').then($el=> {
  const actual = $el.text()
  cy.softAssert(actual, 'Manage Activities')
})
Run Code Online (Sandbox Code Playgroud)


keg*_*gne 5

还有代理包expect

const { proxy, flush } = require("@alfonso-presa/soft-assert");
const { expect } = require("chai");
const softExpect = proxy(expect);

cy.get('span[class="h4"]')
  .invoke('text')
  .should(actual => {
    softExpect(actual).to.eq('Manage Activities')
  })

flush()   // Now fail the test if above fails
})
Run Code Online (Sandbox Code Playgroud)