Cypress First Test Case
Amit K Verma
Helping organizations to streamline the Agile scrum and QA processes | CSM | CSPO | QA | IIT Delhi
Writing first test case is always exciting and interesting to start with. Let’s write a hello world test case using cypress.
1. Open VS code
2. Open cypress project folder in VS Code
3. Create a file under e2e folder. helloWorld.cy.js
4. Write below code inside the file:
describe('My First test', () =>?{
it('Hello World', ()=> {
cy.log('Hello World')
})
})
?5. Now open terminal from VS code by clicking at left bottom of VS Code
6. In the terminal, run command -> npx cypress open
7. Select E2e and browser, now select your new test file from the list
8. This will run in a window and shows the log you mentioned in your code. like this
Lets understand and enhance our script and open a website:
9. Describe and it blocks comes from mocha. Describe blocks is consirered as test suite and it block can be used as test case. Context block can be used if we have different contexts within describe block. Context blocks can contain it blocks inside.
10. Cy is a cypress command, which is further chained with commands to be executed on browser
11. Cy.log is used to print in the cypress logs
12. Use cy.visit(‘webpage-url’) to visit the website
describe('My First test', () =>?{
it('Visit Amazon', ()=> {
cy.visit('https://www.amazon.com/')
})
})
?13. Save this and Re-run the tests (if cypress runner is already opened, test will re-run automatically on saving.)
This is how you can start with cypress' first test case. you should start exploring other commands in cypress from this link https://docs.cypress.io/api/table-of-contents. Do write to me if you are looking for specific command to know more about.