如何使用 jsonAssertion.softAssert 断言元素可见

Geo*_*iev 0 automation cypress

我在 Cypress 测试中使用了 softAssertions。我能够使用softAssert()方法来验证元素中的文本,但我试图弄清楚如何使用softAssert()方法来断言元素可见。在 cypress 中,我使用.should('be.visible')很简单,但我似乎无法使其与softAssert()方法一起使用。我试图断言的元素是输入字段、表格和按钮。我在下面举了一个简单的例子。

我就是这样做的:

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('softContains', (actual, expected, message) => {
  jsonAssertion.softContains(actual, expected, message)
  if (jsonAssertion.jsonDiffArray.length) {
    jsonAssertion.jsonDiffArray.forEach(diff => {

      const log = Cypress.log({
        name: 'Soft assertion error',
        displayName: 'softContains',
        message: diff.error.message
      })
    
    })
  }
});

  Cypress.Commands.add('softAssertAll', () => jsonAssertion.softAssertAll())
Run Code Online (Sandbox Code Playgroud)

这是我的自定义命令,这是实际测试

describe('Load Validation Test', function(){
    const jsonAssertion = require("soft-assert")
  
    it('Load Validation Test', function(){ 
        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)

Suc*_*UKR 5

对于任何不支持开箱即用的条件,请使用常规.then()回调来 apply jsonAssertion.softAssert()

const jsonAssertion = require("soft-assert")

it('Load Validation Test', function() { 
  cy.get('input[placeholder="Activity Name"]')
    .then($input => {
      const isVisible = $input.is(':visible')
      jsonAssertion.softAssert(isVisible, true, 'should be visible')
    })
})
Run Code Online (Sandbox Code Playgroud)