automated-tests - 如何在我的 Cypress 测试中实现软断言

标签 automated-tests cypress

我在 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')
      
  })
})

最佳答案

有关软断言,请参阅 How can i use soft assertion in 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())

在测试中

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

关于automated-tests - 如何在我的 Cypress 测试中实现软断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71262780/

相关文章:

html - 如何使用 Selenium IDE 单击指定的 li 以自动完成 ul?

javascript - 如何使用cypress手动上传文件?

selenium - 如何使用 Selenium Server 运行 Selenium IDE 测试

javascript - 使用 Cypress 登录 WordPress,无需使用 UI

javascript - 将变量的值从测试传递到 Cypress 中的下一个

azure - 在 CI 中 headless 运行时输出 cypress 浏览器日志消息

testing - 运行 TestCafe 测试作为 CI/CD 的一部分

testing - 猴子补丁Testcafe

variables - cypress:then block 之外的变量范围。我面临问题

比较两个值并在 Cypress 中做出决定