In the previous posts, we have discussed various features of cypress and the installation procedure.
Here we will see how a sample test can be written and execute the test using a cypress test runner.
Test case: Search with the text cypress on Google.
Steps:
Go to Google.com
Type the text ‘cypress’
Click on the Search button
Verify the text “JavaScript End to End Testing Framework | cypress.io”
Now start automating the script.
Create a project structure:
Open the folder using the visual studio code that I have created in my last post in order
Go to Terminal->New Terminal to open a new terminal.
Type the following command to create a new project structure.
npx cypress open
It will create a cypress project structure.
Click the OK button and the project structure has been created
Cypress Test runner window is now opened and click the close button to close the window.
The project structure looks like the following.
Now expand the cypress folder and see the structure.
There are four folders.
fixtures: All the test data files are there,
Integration: All the test scripts are there.
Plugins: cypress plugins are there.
Support: all the customized commands are there.
I will discuss the project structure in a separate post.
Writing Test:
Now come to write the cypress test.
Expand the Integration folder and note that a folder named “examples” are displayed.
Now delete that folder as it has some sample scripts and it is not needed now.
create a new test file name cypress_test.spec.js
Write the following script
Note that we use describe..it blocks. As cypress is using mocha by default, it uses this block.
cy.visit() : to visit a page
cy.get(): for getting element location or searching an element
cy.get().click() : to click an element
cy.contains(): to check the text available on the page.
We will discuss more details on cypress API in later posts.
Save the file.
Run the test using TestRunner:
Open a terminal window
Type the following command in the terminal.
npx cypress open
It will open the Cypress test runner.
Now click on the cypress_test.spec.js file and it will start running.
Run the test in the terminal:
If you want to run the cypress test in terminal, then just open the terminal and type the following
npx cypress run
The test will start running in headless mode and display the result.
That’s all for now. We will discuss more on cypress in my future post.
Thank you.
Happy automation testing with Cypress!
No comments:
Post a Comment