我如何在赛普拉斯中使用软断言 [英] How can i use soft assertion in Cypress

查看:101
本文介绍了我如何在赛普拉斯中使用软断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

`我已经从npm配置了软断言(npm i soft-assert),现在我的package.josn具有"soft-assert":"^ 0.2.3"

我想使用软件断言的功能'softAssert(实际,预期,味精,ignoreKeys)'

但是不知道,使用它的确切步骤是什么

示例:当我在代码中使用软断言功能时,出现以下错误.

如果我这样使用

  1. cy.softAssert(10,12,预期的实际不匹配,将执行下一行"):不支持或者如果我使用类似的方式
  2. softAssert(10,12,预期的实际不匹配,将在下一行执行"):未定义SoftAssert

任何人都可以通过一个小例子告诉我如何在赛普拉斯代码中使用此"softAssert"功能.


现在我要面对的问题

  it('断言并记录并失败',()=> {Cypress.softAssert(10、12,预期的实际不匹配...");cy.log(文字")Cypress.softAssertAll();}) 

在软断言后,我需要将我的代码作为 cy.log("text")在同一'it'块中执行,但是当前测试无法通过整个'it'块,而没有执行'cy.log("text")'语句.

解决方案

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

  const jsonAssertion = require("soft-assert")它(断言多次,仅到最后失败",()=> {jsonAssertion.softAssert(10,12,预期的实际不匹配");//其他一些断言,不会导致失败jsonAssertion.softAssertAll();//现在,如果以上失败,则无法通过测试}) 

对我来说,最好在日志中查看每个软断言失败,因此可以添加自定义命令来包装软断言功能

  const jsonAssertion = require("soft-assert")Cypress.Commands.add('softAssert',(实际,预期,消息)=> {jsonAssertion.softAssert(实际,预期,消息)如果(jsonAssertion.jsonDiffArray.length){jsonAssertion.jsonDiffArray.forEach(diff => {const log = Cypress.log({名称:软断言错误",displayName:'softAssert',讯息:diff.error.message})})}});Cypress.Commands.add('softAssertAll',()=> jsonAssertion.softAssertAll())//-以上所有内容都可以进入/cypress/support/index.js//-保存将其添加到每个测试中(每个测试会话运行一次)它(声明并记录但不会失败",()=> {cy.softAssert(10、12,预期的实际不匹配...");cy.log('text');//这将运行})它(声明并记录并失败",()=> {cy.softAssert(10、12,预期的实际不匹配...");cy.log('text');//这将运行cy.softAssertAll();}) 

`I have configured soft assert from npm (npm i soft-assert) and now my package.josn has "soft-assert": "^0.2.3"

i want to use function of Soft assert 'softAssert(actual, expected, msg, ignoreKeys)'

But don't know, what is the exact steps to use it

Example: When i use soft assertion function in my code, getting following error.

If i use like this

  1. cy.softAssert(10, 12, "expected actual mismatch and will execute next line") : not supported or if i use different way like
  2. softAssert(10, 12, "expected actual mismatch and will execute next line") : SoftAssert not defined

Can any one tell me how to use this 'softAssert' function in cypress code with some small example.`


Now the problem I am facing

it('asserts and logs and fails', () => { 
  Cypress.softAssert(10, 12, "expected actual mismatch..."); 
  cy.log("text") 
  Cypress.softAssertAll(); 
}) 

I need my code after soft assertion as cy.log("text") to be executed in the same 'it' block but the current test failing the whole 'it' block, without executing 'cy.log("text")' statement.

解决方案

The soft assertion concept is pretty cool, and you can add it with minimal implementation to 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
})

To me, it would be nicer to see each soft assertion failure in the log, so it's possible to add custom commands to wrap the soft-assert functions

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();
})

这篇关于我如何在赛普拉斯中使用软断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆