如何从 cypress 中的单个元素查询多个内容

Bas*_*din 1 cypress

我正在尝试测试 cypress 中弹出窗口/模式的内容。我的第一直觉是重复命令来获取模态元素,如下所示:

it('filter modal/popup', () => {
    cy.get('.some-button').click();
    cy.get('.some-modal').contains('abc').should('be.visible');
    cy.get('.some-modal').contains('def').should('be.visible');
    cy.getByCyTag('.some-modal').contains('xyz').should('be.visible');
});
Run Code Online (Sandbox Code Playgroud)

然而,“作为一名程序员”,查询“folder-tree-filter-modal”三次甚至更多次让我有点不舒服。由于您无法将 cypress 对象存储在变量中,因为它们会产生结果,因此我尝试使用 cypress 的 then() 基于 Promise 的语法,但这看起来也并没有好多少:

it('filter modal/popup', () => {
    cy.get('.some-button').click();
    cy.get('.some-modal').then((modal) => {
        cy.wrap(modal).should('be.visible');
        cy.wrap(modal).contains('abc').should('be.visible');
        cy.wrap(modal).contains('def').should('be.visible');
        cy.wrap(modal).contains('xyz').should('be.visible');
    });
});
Run Code Online (Sandbox Code Playgroud)

是我想太多还是有更好的方法?

jjh*_*ero 5

您将需要使用该.within()命令。所有后续 cy 命令都将在元素内进行查询。

it('filter modal/popup', () => {
    cy.get('.some-button').click();
    cy.get('.some-modal').within( ()=> {
      cy.contains('abc').should('be.visible');
      cy.contains('def').should('be.visible');
      cy.contains('xyz').should('be.visible');
    });
});
Run Code Online (Sandbox Code Playgroud)