赛普拉斯如何使用逻辑“ OR”断言

时间:2020-09-29 08:51:13

标签: javascript mocha cypress assert

我有这个代码

cy.get(element).should('contain.text', search_word || search_word.toLowerCase())

我收到此错误

expected <div.games__element__title.card-title.h5> to contain text Hot, but the text was Ultimate hot

如何使用OR运算符,以便可以断言element的文本包含以大写或小写字母写的搜索词?

2 个答案:

答案 0 :(得分:1)

对于文本比较,我建议使用小写字母比较,而不是使用OR方法,而要在小写版本中比较期望的文本和实际文本。这是一种更清洁的方法。

cy.get(element).invoke('text').should(text => {
  expect(text.toLowerCase()).to.contain(search_word.toLowerCase());
})

另一种选择是使用正则表达式

cy.get(element).invoke('text').should('match', /(h|H)ot/);

答案 1 :(得分:0)

获得所需内容的一种方法是使用cypress中的Conditional语句。我们将从元素中获取内部文本,然后检查文本中是否出现单词 hot Hot ,然后我们将执行操作。

cy.get(element).invoke('text').then((text) => {
   if (text.includes('Hot')) {
      //Do Something
   }
   else if (text.includes('hot')) {
      //Do Something
   }
   else {
      //Do Something
   }
})