简体   繁体   中英

Cypress/VueJS Selecting Element that is dynamically added after another action

I'm testing a feature where a button appears for the user to click AFTER they have taken another action.

Before taking the action I'm checking if the button exists:

cy.get('span')
    .contains('Select')
    .parent('button')
    .should('not.exist');

And after the action I'm trying to find that same button like this but it fails. Would anyone know how to do this in Cypress?

Here is my entire code:

cy.get('span')
    .contains('Select')
    .parent('button')
    .should('exist');
/* eslint-disable */

describe('Workgroup Switch', () => {
    beforeEach(() => {
        cy.visit('/login');
        cy.get('input[id="username"]').type('boss@example.com');
        cy.get('input[id="password"]').type('password');
        cy.get('button[id="login-button"]').click();
        cy.url().should('include', '/offers/workgroup');
    });

    it('switch workgroup', () => {
        cy.get('span')
            .contains('Select')
            .parent('button')
            .should('not.exist');

        cy.get('.v-overlay__scrim').click();

        cy.get('.text-h6')
            .contains('Workgroup #1')
            .parent()
            .parent()
            .click();

        cy.contains('Make this my default workgroup');

        cy.get('span')
            .contains('Select')
            .parent('button')
            .should('exist');
    });
});

You should use the combined form of contains() to get the correct element.

cy.contains('span', 'Select')
  .parent('button')
  .should('exist');

Doing it in two parts like this

cy.get('span')
  .contains('Select')

Cypress finds the first span on the page and then repeatedly checks it's text for "Select", but it does not repeat the cy.get('span') part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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