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('[email protected]');
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');
});
});
CodePudding user response:
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.
