【问题标题】:Get first element that does not contain value in an array in cypress获取赛普拉斯数组中不包含值的第一个元素
【发布时间】:2019-06-28 17:53:06
【问题描述】:

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

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

还有这个:

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

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

【问题讨论】:

    标签: testing automation automated-tests cypress end-to-end


    【解决方案1】:

    我没有使用这样的命令:

    cy.get('.alert-radio-group > button')
    .not(`:contains(\'${valueSelected1}\')`)
    .not(`:contains(\'${valueSelected2}\')`)
    .first()
    .click();
    

    【讨论】:

      【解决方案2】:
      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)手动过滤掉各种孩子

      让我知道是否足够清楚?

      【讨论】:

        猜你喜欢
        • 2021-03-20
        • 2022-01-07
        • 2019-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-22
        相关资源
        最近更新 更多