【发布时间】:2019-10-08 13:42:08
【问题描述】:
只是想知道是否有一种方法可以使用 cypress 测试元素是否包含 text_A 或 text_B。例如,我有一个元素填充了从 API 请求返回的错误,我想检查错误消息是包含 text_A 还是 text_B。尝试了以下代码,但它仅在存在 text_A 时才有效,并且在返回 text_B 时失败。我没有收到任何来自赛普拉斯的无效语法错误,任何帮助都是赞赏。
cy.get('body').then((body) => {
if (body.find('#request-error').length > 0) {
cy.get('#request-error').then((el)=> {
assert.include(el.text(), ('text_A' || 'text_B'));
});
} else {
// DO SOMETHING ELSE
}
});
【问题讨论】:
-
试试
cy.get('#request-error').contains(/text_A|text_B/g),来自这个问题Cypress expect element to contain one string or another string -
嗨@Ackroydd 感谢您的回答。我设法使用您提供的链接中描述的想法使其工作。
const runout = ['text_A', 'text_B'] const regex = new RegExp(`${runout.join('|')}`) const text = el.text() expect(text).to.match(regex) -
还有一件事,您在
.then((el) => ...中执行此操作。你试过.contains(regex)吗?我从你对 Cory 的评论中注意到有一个子字符串因素,我想知道这种情况是否也适用于更简单的.contains(regex)。
标签: javascript automated-tests cypress