我想用柏树测试检查单行文本字段和多行文本区域中的值是否正确。
我这里有一个单行框,我使用下面的代码添加输入:
cy.followLabel("When did the incident happen?").type("single \nline");
多行文本区域也是如此
cy.followLabel("Deenter code herescribe what happened").type("multiple \nlines");
我尝试用 .should() 验证它,但没有找到任何适合我需要的链接器。
有没有类似
.contains("\n")
的东西?
Chai 断言库在
chai-string模块中有一个
singleLine
断言。
单线
assert.singleLine('abcdef');
期望('abcdef').to.be.singleLine();
要使用其他库的断言,请参阅赛普拉斯食谱添加 Chai 断言
简单的例子
安装
yarn add -D chai-string
//or
npm install -D chai-string
测试
chai.use(require('chai-string'));
it('tests for singleLine', () => {
cy.get('input')
.type("single \nline")
.invoke('val')
.should('be.singleLine') // passes
cy.get('textarea')
.type("multiple \nlines")
.invoke('val')
.should('not.be.singleLine') // passes
})
可以直接查看innerText是否有换行符
\n
.
cy.get('selector').then(($ele) => {
expect($ele).to.contain('\n')
})
如果你的内部文本没有换行符,你断言样式标签包含
overflow-wrap: break-word
cy.get('selector').should('have.attr', 'style').and('include', 'overflow-wrap: break-word')
我知道正则表达式有一个断言“匹配”。
match(RegExp)
Aliases: matches
expect('testing').to.match(/^test/)
(来自 https://docs.cypress.io/guides/references/assertions)
所以在你的情况下,正则表达式可能是
match(/.*\n.*/gm)
我找到了一个更简单的解决方案。只需将带有链接器“have.value”的 should 添加到您的 DOM 元素,然后您就可以检查新行。
.should('have.value','multiple\nlines');