Debuggability
With the help of cypress.io, we can perform debugging of the code with the help of Chrome DevTools.
Screenshots and videos
cypress.io takes a screenshot of test case failed steps and takes videos of each step when the test case is executed headlessly.
Prerequisites to working with Cypress.io
Node.js can be installed from this link https://nodejs.org/en/download/, navigate to this link and click on Windows Installer it will download a Node.js setup file like "node-v8.11.4-x64" double-click on this file. It will navigate to this below the window.
Click on Next Button.
Check the accept the terms checkbox and click on Next.
Provide the path where ever you like to save node.js by clicking on Change if required and click on Next.
Click on Next.
Click on Install Button.
It will start installing the Node.js as shown in Status Progress bar.
Click on the Finish button in order to check whether Node.js is properly installed in your machine.
You can use the below commands
Similarly, if we want to check whether npm in installed or not we can check it using the below commands,
Steps to work with Cypress.io
Create a project by adding a new folder and give the name for the project like example Cypress.ioProject and go to https://www.cypress.io/features/ and download as shown in the image below, once the download is completed, unzip the .rar file and keep the cypress folder in the above project Cypress.ioProject folder.
We can Add a few .js file in the above project folder (Cypress.ioProject) based on our requirement.
Check whether the node is installed or not by using,
For checking npm version,
In order to install Cypress through commands, we can do it like below.
Open the command prompt and navigate to the solution path and install cypress by using the following commands like
- npm i cypress --save-dev
or
- npm install cypress.
or
- npm install cypress
or
- npm install cypress --save-dev
or
- npm install --save-dev cypress
or
- npm install --save-dev [email protected]
It will install cypress to the solution folder or you can install directly from Cypress official site as shown in the above image.
Go to the project's folder path and enter npm init to install package.json to the solution folder.
After installing all the files which are mentioned above, the folder structure will look like below.
In order to open the Cypress.io Test runner navigate to the above folder path like below,
D:\>Cypress.ioProject\node_modules\.bin> cypress open
The above command will open the Cypress test runner.
From the visual studio, code navigates to the Cypress.ioProject folder and select the project and click on ok.
It will open all the files of Cypress.ioProject like below.
In the above image, we can see the list of .js files that are displayed within the examples folder.
If we want to Add a new .js file we can directly add it in Cypress.ioProject--> cypress--> fixtures--> integration --> examples.
Create .js files like below,
- CypressExample1.js
- CypressExample2.js
- CypressExample3.js
Now edit the CypressExample1.js file, add the below code and save the file.
- describe('C# Corner sign up', function() {
- it('Automate C# Corner Page', function() {
- cy.visit('http://www.google.co.in')
- cy.wait(2000)
- cy.get('input[name="q"]').type('C# Corner{Enter}')
-
- cy.get('a').contains('C# Corner - A Social Community of Developers and Programmers').click()
- cy.wait(2000)
- })
- })
Here, the wait is optional in the above code, if we did not provide and wait for a control then also it will wait for control which is an inbuilt feature of Cypress.io.
Similarly, edit the CypressExample2.js and add the below code and save the file.
- describe('First Example', () => {
- it('Automate Google', () => {
- cy.visit("https://www.google.co.in");
- cy.get('input[name="q"]').type('Wikipedia')
- cy.get('input[name="btnK"]').click()
- cy.get('a').contains("Wikipedia, the free encyclopedia").click();
- cy.wait(3000)
- cy.get('input[id="searchInput"]').type("StackOverflow{Enter}")
- cy.wait(6000)
- cy.get('a[class="mw-wiki-logo"]').click()
- cy.get('input[id="searchInput"]').type("Google{Enter}")
-
-
- })
- })
Similarly edit the third file, add the below code, and save the file.
- describe('click on signup', function() {
- it('click', function() {
- cy.visit('https://www.google.co.in')
- cy.wait(2000)
- cy.get('input[name="q"]').type('Csharp Corner')
- cy.get('input[name="btnK"]').click()
- cy.get('a').contains('C# Corner - A Social Community of Developers and Programmers').click()
- cy.wait(2000)
- cy.get('input[id="ctl00_HeaderHomeNewDesign_Login1_HyperLinkRegister"]').click()
- })
- })
Whenever we save the .js file in the Visual Studio code, it automatically starts the execution in the Cypress.io Test Runner which is one of the features available with the Cypress.io; the user doesn't need to manually click on the Run Test button on the Test Runner.
Similarly, if we want to run the above project without installing Cypress with commands we can download the Cypress zip file which is available in the Cypress official website which is shown in the above image.
After downloading Cypress, navigate to it and click on Cypress.exe. It will open the Test Runner like below.
Click on Select manually and navigate to the Cypress.ioProject, with this all the .js files will be displayed in the test runner including CypressExample1.js, CypressExample2.js, CypressExample3.js. If we click on the CypressExample1.js file it starts the execution in the Chrome browser.
Conclusion
It is one of the Javascript end to end testing tools which is similar to TestCafe, protractor, etc, which requires few setup files and is fast in execution compared to that of selenium, CodedUI, QTP, etc. It doesn't require any wait time declaration manually, as well as screenshot logic steps declaration.
It is helpful in testing applications which are developed using Angular, React, etc.
Thanks and I hope this article helps you.