柏树是否支持软断言?

问题描述 投票:1回答:1

柏树是否支持软断言?例如 - 我正在浏览'n'个元素并想要验证它们的值。如果任何元素的值不匹配,那么它将无法通过测试。它不会继续验证下一个元素的值。有没有办法验证所有元素值并在最后显示失败的值?

cypress
1个回答
1
投票

编辑:我可能误解了..如果你的意思是cy命令的软断言(不是chai的expect / assert),那么这个答案不适合你。

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

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

你可以这样做。

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

赛普拉斯日志:

enter image description here

终端(无头跑):

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.