How to write my first test case in Cypress?
Hi Dipen,
To write your first test case in Cypress, start by installing Cypress using npm or yarn. Once installed, create a new folder for your Cypress tests and open it in your preferred code editor. Inside the folder, create a new JavaScript file, such as “first_test.js”. In this file, import Cypress using the require or import statement.
Next, use the Cypress commands to interact with your application. For example, you can use the cy.visit()
command to navigate to a specific URL, cy.get()
to select and interact with elements on the page, and cy.contains()
to search for specific text. Write your test case using these commands to describe the behavior you want to test.
For instance, a simple test case could be to verify that the login functionality works correctly. You can use cy.visit()
to navigate to the login page, cy.get()
to select the username and password input fields, and cy.contains()
to locate the login button. Then, use cy.type()
to input valid credentials into the fields and cy.click()
to click the login button. Finally, use cy.url()
to assert that the user is redirected to the expected page after logging in.
Once you have written your test case, save the file and return to your terminal. Run Cypress using the command npx cypress open
to launch the Cypress Test Runner. Select your test file from the Cypress Test Runner window and click on it to run your test case. Cypress will open a browser window and execute your test case, providing real-time feedback on the results.
Please refer to this blog to learn more: