cypress - 赛普拉斯测试验证
问题描述
我有将文本转换为大写的功能,我想做的是在 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()
重试和超时。
或者没有自定义 chai 断言
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())
})
推荐阅读
- c# - 响应对象没有实现在 ASP.NET MVC 框架中下载的文件
- python - 使用python将speedtest csv数据写入csv文件
- mysql - HikariCP 多个池中的连接数
- javascript - 遍历 HTML 元素的属性,并能够判断哪些是在 HTML 中显式设置的
- windows - 如何配置用户的 git hook 文件夹以匹配项目的
- swift - 运行和开发相同的 Swift macOS 应用程序
- python - Django - 在另一个应用程序中访问应用程序设置
- scala - 通过 spark sql 和 scala 删除文件中的逻辑和直接重复
- wordpress - Wordpress,根据登录凭据重定向到不同的页面
- security - iOS NSLog 语句是否代表安全/隐私问题,还是在 2018 年被系统忽略?