
Cypress Has a Free API You Should Know About
Cypress is famous for E2E testing, but its API goes far beyond simple test commands. Custom commands, plugins, network stubbing, and component testing make it a complete testing platform. Custom Commands API Extend Cypress with reusable commands: // cypress/support/commands.js Cypress . Commands . add ( " login " , ( email , password ) => { cy . session ([ email , password ], () => { cy . visit ( " /login " ) cy . get ( " [data-cy=email] " ). type ( email ) cy . get ( " [data-cy=password] " ). type ( password ) cy . get ( " [data-cy=submit] " ). click () cy . url (). should ( " include " , " /dashboard " ) }) }) Cypress . Commands . add ( " createUser " , ( user ) => { return cy . request ({ method : " POST " , url : " /api/users " , body : user , headers : { Authorization : `Bearer ${ Cypress . env ( " API_TOKEN " )} ` } }) }) // In tests cy . login ( " admin@test.com " , " password123 " ) cy . createUser ({ name : " John " , role : " editor " }) Network Stubbing with cy.intercept() C
Continue reading on Dev.to Webdev
Opens in a new tab

