赛普拉斯是否支持软断言? [英] Does cypress support soft assertion?

查看:100
本文介绍了赛普拉斯是否支持软断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

赛普拉斯是否支持软断言?例如-我正在浏览n个元素,并想验证其值。如果任何元素的值不匹配,则测试将失败。它不会继续验证下一个元素的值。有没有办法验证所有元素的值并在最后显示失败的元素?

Does cypress support soft assertion? For example - I am navigating through 'n' number of elements and want to verify their values. If value of any element is mismatched then it fails the test. It doesn't continue to verify the value of next elements. Is there a way to verify all elements values and show the failed ones at the end?

推荐答案

编辑:我可能会误解了..如果您是指 cy 命令的软断言(不是chai的 expect / assert ),那么此答案不适合您。

I probably misunderstood.. if you meant soft assertions of cy commands (not chai's expect/assert), then this answer is not for you.

WTBS,您可以使用以下解决方案并执行以下操作:

WTBS, you could use the below solution and do something like:

describe('test', () => {

    const { softExpect } = chai;

    it('test', () => {
        cy.document().then( doc => {
            doc.body.innerHTML = `
                <div class="test">1</div>
                <div class="test">2</div>
                <div class="test">3</div>
            `;
        });
        cy.get('.test').each( $elem => {
            softExpect($elem[0].textContent).to.eq('2');
        });
    });
});






您可以执行以下操作。


You could do something like this.

support / index.js

let isSoftAssertion = false;
let errors = [];

chai.softExpect = function ( ...args ) {
    isSoftAssertion = true;
    return chai.expect(...args);
},
chai.softAssert = function ( ...args ) {
    isSoftAssertion = true;
    return chai.assert(...args);
}

const origAssert = chai.Assertion.prototype.assert;
chai.Assertion.prototype.assert = function (...args) {
    if ( isSoftAssertion ) {
        try {
            origAssert.call(this, ...args)
        } catch ( error ) {
            errors.push(error);
        }
        isSoftAssertion = false;
    } else {

        origAssert.call(this, ...args)
    }
};

// monkey-patch `Cypress.log` so that the last `cy.then()` isn't logged to command log
const origLog = Cypress.log;
Cypress.log = function ( data ) {
    if ( data && data.error && /soft assertions/i.test(data.error.message) ) {
        data.error.message = '\n\n\t' + data.error.message + '\n\n';
        throw data.error;
    }
    return origLog.call(Cypress, ...arguments);
};

// monkey-patch `it` callback so we insert `cy.then()` as a last command 
// to each test case where we'll assert if there are any soft assertion errors
function itCallback ( func ) {
    func();
    cy.then(() => {
        if ( errors.length ) {
            const _ = Cypress._;
            let msg = '';

            if ( Cypress.browser.isHeaded ) {

                msg = 'Failed soft assertions... check log above ↑';
            } else {

                _.each( errors, error => {
                    msg += '\n' + error;
                });

                msg = msg.replace(/^/gm, '\t');
            }

            throw new Error(msg);
        }
    });
}

const origIt = window.it;
window.it = (title, func) => {
    origIt(title, func && (() => itCallback(func)));
};
window.it.only = (title, func) => {
    origIt.only(title, func && (() => itCallback(func)));
};
window.it.skip = (title, func) => {
    origIt.skip(title, func);
};

beforeEach(() => {
    errors = [];
});
afterEach(() => {
    errors = [];
    isSoftAssertion = false;
});

在您的规范中:

describe('test', () => {

    const { softAssert, softExpect } = chai;

    it('test', () => {
        cy.wrap([1, 42, 3]).then( vals => {
            vals.forEach( val => {
                softAssert(val === 42, `${val} should equal 42`);
            });
        });
    });

    it('test2', () => {
        cy.wrap([2, 5, 4]).then( vals => {
            vals.forEach( val => {
                softExpect(val).to.eq(42);
            });
        });
    });
});

赛普拉斯日志:

终端(无头运行):

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

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