简体   繁体   English

我们可以测试元素文本是否包含带有 cypress 的 text_A 或 text_B 吗?

[英]Can we test if element text includes text_ A or text_B with cypress?

Just wondering if there is a way to test that an element contains text_A or text_B with cypress.只是想知道是否有一种方法可以用柏树测试一个元素是否包含text_Atext_B For example, I have an element that is filled with the error returned from an API request and I want to check whether the error message includes text_A or text_B .例如,我有一个元素填充了从 API 请求返回的错误,我想检查错误消息是否包含text_Atext_B Tried the following code but it works only if text_A is present and fails when text_B is returned.I don't get any error for invalid syntax from cypress, any help would be appreciated.尝试了以下代码,但它仅在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
     }
 });

Essentially you have an array of possible error messages, so you can test if the element's text exists within that array.本质上,您有一组可能的错误消息,因此您可以测试元素的文本是否存在于该数组中。

expect(['text_A', 'text_B']).to.include(el.text())

Another option that reads better would be to use .oneOf另一个更好读的选择是使用.oneOf

expect(el.text()).to.be.oneOf(['text_A', 'text_B']);

https://docs.cypress.io/guides/references/assertions.html#BDD-Assertions https://docs.cypress.io/guides/references/assertions.html#BDD-Assertions

I am late but you can do it with satisfy:我迟到了,但你可以满足:

 cy.get('.text-element').invoke('text').then(text => {
      expect(text).to.satisfy((mText: string) => possibleMatchesArray.includes(mText));
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM