Refactor A Test Part 4: A Better Delete Method @gleb
Refactor A Test Part 4: A Better Delete Method  @gleb
Uploaded March 2024 | Updated September 2026, 1 week ago
In this video, I refactor the page object method that deletes a customer.
- add a log message
- use cy.contains(selector, text) rather than the longer cy.get(selector).contains(text)
- explicit "confirm" stub and check that the stub was called with expected argument
- confirm the application routes to the correct URL after deleting
delete() {
cy.log('**deleting the customer**');
cy.on(
'window:confirm',
cy
.stub()
.returns(true)
// @ts-expect-error
.as('confirm')
);
cy.contains('button', 'Delete').click();
cy.get('@confirm').should('have.been.calledOnceWith', 'Really delete?');
cy.location('pathname').should('eq', '/customer');
}
Find the original code at github.com/bahmutov/basta-spring-2024-cypress-and-playwright
Refactor A Test Part 4: A Better Delete MethodEscape Regular Expression Text When Using cy.contains CommandShort Demo Of cypress-thinkDisable ServiceWorker In Cypress TestCypress Flaky Test Exercise: Level 1Cypress Flaky Test Exercise: Level 4Validate Email Format Using Built-in Browser Input ElementAdd Cypress AUTWindow PropertiesHandle Telephone And Other Custom Protocol Links In Cypress TestsCheckboxes With ConfirmationCypress Flaky Test Exercise: Level 8Testing Data Preload In TanStack Router
gleb bahmutov |

Refactor A Test Part 4: A Better Delete Method

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER