我有一张卡的列表,我需要以编程方式选择列表中的第一张,该列表不包含数组中五个值之一。我已经试过了:

cy.get('.parent-element').not().contains('string1', 'string2', 'etc')


还有这个:

cy.get('.parent-element').not('string1', 'string2', 'etc')


嗯。任何想法。除了以下几个例外,.parent-element的任何子元素都是公平的游戏。

最佳答案

const selector = ".parent-element li";
const array = ["string1", "string2", "string3"];
cy.get(selector)
  .then($els => {
    return $els.filter((i, el) => {
      // Cypress.$ is jQuery
      const textContent = Cypress.$(el).text();
      const result = array.filter(str => textContent.includes(str));
      // if noone string is been found... we have found the desired element
      return !result.length;
    });
  })
  .then($el => {
    // $el is your searched element

    // some assertions about it
    expect($el.text()).not.includes("string1");
    expect($el.text()).not.includes("string2");
    expect($el.text()).not.includes("string3");
  });



指定选择器以查找子代(我在选择器后附加了li
您使用的.not().contains是断言,而不是过滤器
cy.get函数返回一些jquery元素
使用Cypress.$(即jQuery)手动过滤掉各种子项


让我知道是否足够清楚😊

10-06 16:13
查看更多