【问题标题】:Cypress - How to do negative lookaheads with RegEx?赛普拉斯 - 如何使用 RegEx 进行负前瞻?
【发布时间】:2020-02-10 15:14:00
【问题描述】:

我正在尝试在 Cypress 中测试一个过滤器,一旦过滤器被删除,搜索结果应该再次包含超出过滤值的值。

我正在尝试做这样的事情:

cy.get('.outputTableArea').within(() => {
    cy.get("td").then(($td) => {
        expect($td).to.contain(/^(?!regex)/);
    });
});

不幸的是,这里的负前瞻似乎不适用于(?!...)

【问题讨论】:

  • 你打算和^(?!regex)匹配什么?
  • 这只是一个例子。这是一个设备列表,我打算匹配除被过滤的设备之外的所有设备(以便查看过滤器是否被正确删除...)
  • 您能否提供指向contain() API 的链接?是否支持正则表达式?
  • @OliverHowald 你有没有得到这个问题的答案?

标签: javascript node.js regex filter cypress


【解决方案1】:

您在 DOM 元素上使用 chai 的 contains 匹配器(include 的别名),但 API 需要一个字符串并且不需要 RegExp 针,而是另一个字符串(与 DOM 的 @ 相同) 987654326@).

你可以使用match:

expect($td.text()).to.match(/^(?!regex)/);

或者更好的是,Cypress 的 .contains().should('match')(下面使用 chai):

describe('test', () => {
  it('test', () => {
    cy.document().then( doc => {
      doc.body.innerHTML = `
        <div class="test">Hello world!</div>
        <div class="test">Hello Steve!</div>
      `;
    });

    // using cy.contains
    // -------------------------------------------------------------------------
    cy.get('.test:first').contains(/hello(?! world)/i); // will fail
    cy.get('.test:last').contains(/hello(?! world)/i); // will succeed

    // using chai matcher on yielded text
    // -------------------------------------------------------------------------
    cy.get('.test:first').invoke('text').should('match', /hello(?! world)/i); // will fail
    cy.get('.test:last').invoke('text').should('match', /hello(?! world)/i); // will succeed

    // using cy.contains in a callback
    // -------------------------------------------------------------------------
    cy.get('.test:first').then($el => {
      cy.wrap($el).contains(/hello(?! world)/i); // will fail
    });
    cy.get('.test:last').then($el => {
      cy.wrap($el).contains(/hello(?! world)/i); // will suceed
    });

  });
});

【讨论】:

    【解决方案2】:

    如何测试列表的长度以确保在删除过滤器时返回所有结果?例如。其中“10”是完整列表中的项目数:

    cy.get('.outputTableArea td').should('have.length', 10);
    

    如果您需要检查表格内容中是否缺少特定值,您可以尝试:

    cy.get('.outputTableArea').contains(/^regex/).should(not.exist);
    

    或者也许将其缩小到带有以下内容的标签:

    cy.get('.outputTableArea td').contains(/^regex/).should(not.exist);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-23
      • 2023-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      相关资源
      最近更新 更多