我有将文本转换为大写的功能,我想要做的是在 cypress 中为该函数编写测试并在 htmml 中打印结果
这里的功能:
module.exports = () => ({
upperCaseName: (name) => {
return name.toUpperCase()
}
});
我在这里打印:
<h1 cy-data='uppercase'> the result </h1>
那么我应该如何编写测试:
我知道我可以这样做:
cy.get('[cy-data=uppercase]').contains('the result')
但我想要这样的东西:例如:
cy.get('[cy-data=uppercase]').to.be.upperCase
是否可以?
怎么样
cy.get('[cy-data=uppercase]').contains('THE RESULT', { matchCase: true })
但是{ matchCase: true }
是默认设置,所以可以只是
cy.get('[cy-data=uppercase]').contains('THE RESULT')
大写的自定义 Chai 断言
window.chai.Assertion.addProperty('uppercase', function () {
var obj = this._obj;
new chai.Assertion(obj).to.be.a('string');
this.assert(
obj === obj.toUpperCase()
, 'expected #{this} to be all uppercase' // error message when fail for normal
, 'expected #{this} to not be all uppercase' // error message when fail for negated
);
});
it('test for upper case (and not uppercase)', () => {
cy.get('[cy-data=uppercase]').invoke('text').should('be.uppercase')
cy.get('[cy-data=lowercase]').invoke('text').should('not.be.uppercase')
})
使用新的断言扩展了 Chai 的内部 Cypress 版本,也适用于.should()
重试和超时。
或者没有自定义柴断言
it('test for upper case (and not uppercase)', () => {
cy.get('[cy-data=uppercase]').invoke('text')
.should(text => expect(text).to.eq(text.toUpperCase())
cy.get('[cy-data=lowercase]').invoke('text')
.should(text => expect(text).not.to.eq(text.toUpperCase())
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句