automation - 我如何在 Cypress 中使用软断言

标签 automation cypress assertion

`我已经从 npm (npm i soft-assert) 配置了软断言,现在我的 package.josn 有 "soft-assert": "^0.2.3"
我想使用软断言的功能
'softAssert(实际,预期,消息,忽略键)'
但是不知 Prop 体的使用步骤是什么
例子:
当我在代码中使用软断言函数时,出现以下错误。
如果我像这样使用

  • cy.softAssert(10, 12, "expected actual mismatch and will execute next line") : 不支持
    或者如果我使用不同的方式,如
  • softAssert(10, 12, "expected actual mismatch and will execute next line") : SoftAssert 未定义

  • 任何人都可以通过一些小例子告诉我如何在 cypress 代码中使用这个“softAssert”函数。`

    现在我面临的问题
    it('asserts and logs and fails', () => { 
      Cypress.softAssert(10, 12, "expected actual mismatch..."); 
      cy.log("text") 
      Cypress.softAssertAll(); 
    }) 
    
    软断言后我需要我的代码为 cy.log("text")将在同一个“it”块中执行,但当前测试在整个“it”块中失败,而不执行“cy.log("text")”语句。

    最佳答案

    软断言概念非常酷,您可以将它以最少的实现添加到 Cypress

    const jsonAssertion = require("soft-assert")
    
    it('asserts several times and only fails at the end', () => {
      jsonAssertion.softAssert(10, 12, "expected actual mismatch");
      // some more assertions, not causing a failure
    
      jsonAssertion.softAssertAll();  // Now fail the test if above fails
    })
    
    对我来说,在日志中看到每个软断言失败会更好,所以可以添加自定义命令来包装软断言功能
    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())
    
    
    //-- all above can go into /cypress/support/index.js
    //-- to save adding it to every test (runs once each test session)
    
    
    
    it('asserts and logs but does not fail', () => {
      cy.softAssert(10, 12, "expected actual mismatch...");
      cy.log('text');    // this will run
    })
    
    it('asserts and logs and fails', () => {
      cy.softAssert(10, 12, "expected actual mismatch...");
      cy.log('text');    // this will run
    
      cy.softAssertAll();
    })
    

    关于automation - 我如何在 Cypress 中使用软断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66132293/

    相关文章:

    cypress - 有什么方法可以用 cypress 测试句子的所有可能组合吗?

    cypress - 如何使用 before :run function in cypress before running all specs? 设置变量

    javascript - Cypress :只运行一项测试

    javascript - 使用 JavaScript 进行断言式编程

    ruby - 如何从命令将 Selenium Webdriver(Ruby 绑定(bind))测试结果打印到记事本文件?

    c++ - 正确的 VARIANT 初始化

    wso2 - 如何从 WSO2 身份服务器刷新 SAML 断言信息?

    javascript - JMeter BSF 断言 - 比较日期变量

    java - FlywayDB : Run same name SQL again

    python - 在不使用任何浏览器的情况下使用 Selenium