12. Babel Coder
COMMANDS, ASSERTIONS AND RETRY ABILITY
it('adds words correctly', () => {
cy.visit('/cypress/async-word-list'); // command
cy.getByTestID('my-word-input') // command
.type('lorem'); // command
cy.getByTestID('add-word-button') // command
.click(); // command
cy.get('ul > li') // command
.
fi
nd('span') // command
.should('have.text', 'lorem'); // assertion
cy.getByTestID('my-word-input') // command
.type('ipsum'); // command
cy.getByTestID('add-word-button') // command
.click(); // command
cy.get('ul > li') // command
.
fi
nd('span') // command
.should('have.text', 'ipsum'); // assertion
});
it('adds words correctly', () => {
cy.visit('/cypress/async-word-list');
cy.getByTestID('my-word-input').type('lorem');
cy.getByTestID('add-word-button').click();
cy.get('ul > li:
fi
rst-child span').should('have.text', 'lorem');
cy.getByTestID('my-word-input').type('ipsum');
cy.getByTestID('add-word-button').click();
cy.get('ul > li:
fi
rst-child span').should('have.text', 'ipsum');
});
Cypress only retries commands that query the DOM:
cy.get(), .
fi
nd(), .contains(), etc.
Commands are not retried when they could potentially
change the state of the application under test.
For example, Cypress will not retry the .click() command,
because it could change something in the application.
14. Babel Coder
SHOULD AND THEN
cy.getByTestID('word-list').then(($list) => {
const $words = $list.
fi
nd('li');
expect($words).to.have.length(2);
expect($words.
fi
rst()).to.have.text('ipsum');
expect($words.last()).to.have.text('lorem');
});
cy.getByTestID('word-list').should(($list) => {
const $words = $list.
fi
nd('li');
expect($words).to.have.length(2);
expect($words.
fi
rst()).to.have.text('ipsum');
expect($words.last()).to.have.text('lorem');
});
If you have to use commands that cannot be
retried, but need to retry the entire chain, consider
rewriting the commands into a single
.should(callbackFn) chained o
ff
the very
fi
rst
retry-able command.
The .then() command is not retried.
15. Babel Coder
CYPRESS.CONFIG.TS
import { de
fi
neCon
fi
g } from 'cypress';
export default de
fi
neCon
fi
g({
env: {
apiUrl: 'http://127.0.0.1:5000/api/v1',
},
retries: 3,
e2e: {
baseUrl: 'http://127.0.0.1:3000',
setupNodeEvents(_on, _con
fi
g) {
// implement node event listeners here
},
},
});
33. Babel Coder
SPECIAL CHARACTERS
{del} Deletes character to the right of the cursor
{enter} Types the Enter key
{movetostart} Moves cursor to the start of typeable element
{movetoend} Moves cursor to end of typeable element
{selectall} Selects all text by creating a selection range
{alt} Activates the altKey modi
fi
er.
{ctrl} Activates the ctrlKey modi
fi
er.
{shift} Activates the shiftKey modi
fi
er.