Cypress Basics - Understanding Test Automation & setting up your Cypress Test project
Tags: beginners, javascript, testing, cypress
Published: 2022-07-26 16:44:01

In the Quality Assurance world, our aim is to ensure quality, and we ensure quality by testing effectively. Test automation is important because in software testing there are a number of features that are required to be tested repetitively and or concurrently. If testing these features is automated (writing and executing of test scripts) it is less time-consuming, and cost-effective and we get accelerated results.

In Continuous Integration, automated tests serve as collections of regression test suites and are crucial for detecting errors as soon as they occur.


### What is Test Automation?

Test automation is the practice of writing tests for features that require repetitive testing. It is a form of software testing that uses an automation tool to execute test cases automatically to improve software quality.

NB:- Some testcases can’t be automated and features / testcases are manually tested first before automating.


### What Should We Automate?

- Test cases that test the essential features of the program.
- Test cases that must be run repeatedly on a big dataset.
- Test cases that take a lot of time.
- Tests cases requiring parallel execution (There are scenarios that require checking the concurrent access to the application e.g. in the case of performance testing with multiple users)

### What are the things we should not automate?
- UI test cases
- New Functionalities
- Subjective testing
- Usability test cases
- Functionalities that are rarely used and take time for scripting
- Exploratory testing


### When should we Automate?
We can start creating the automation framework in parallel with the development team after outlining all the features of the automation suite during test planning. But it's important to script the test cases at the appropriate moment.

The scripting of test cases should begin when the product (application) is stable and frequent changes in the application are not anticipated.





# Test automation with Cypress
**Now you will set up your project for writing test scripts in Cypress.**



### What is Cypress?
Cypress is an all-in-one javascript-only framework(built on Mocha) making asynchronous testing simple and convenient. Cypress is used for testing the functionality of the UI features of the frontend applications. It uses a BDD/TDD assertion library and a browser to pair with any Javascript testing framework. The speed run is very fast, It was built by front-end developers for front-end developers.

### Setting up your Project

To work with Cypress, you need to work with the modern javascript toolset, and that toolset includes things like node.js and npm.
You have to install node.js. If you have node.js installed, you have to ensure that the version you have is either equal to or greater than version 8. (check that by running the “node –version” command)

Next, You need an editor, pick any IDE of your choice(Pycharm, Vscode, etc)
When you have the IDE(code editor) of your choice, you can now install Cypress. Cypress is an NPM package. Usually, we install NPM packages in the same folder where the test code will be. 

The first thing will be to create the folder where our test code will reside. Once you create that folder, open the folder with the IDE of your choice. Once the folder has been opened in your IDE, then you create a package.json file. To do this, open a terminal in your IDE and run this command inside the project folder: 

```javascript
npm init -y
```

When you run this command above, the package.json file will be created for you inside the project folder. You need this package.json folder so that when you install Cypress in this test folder, it will be registered in the package.json file that you have Cypress installed in your test project folder. Also, the same terminal is activated to run NPM commands. We are going to use NPM commands to install Cypress. 

Cypress is just another package in the NPM repository. Cypress is free to install, though it has some other interesting features you might want to pay for. Cypress is Open Source as well. 

When you install Cypress inside this folder, you are installing it locally, which means Cypress will only run in that folder. This is good because it enables us to have different versions of Cypress running on our system when we need to.

In order to install Cypress, run this command: 

```javascript
npm install cypress --save-dev cypress
```

Note that this installation might take a while if you are installing Cypress for the first time (This creates the node modules and package-lock.Json File) 

Once your installation is complete, you should see a folder named cypress inside your test project folder which contains some other folders- Integration, Fixtures, Plugins, and support. Now you are good to go!


### Running Cypress
To run Cypress, you use NPX commands.
In order to run Cypress, run this command: 

```javascript
npx cypress open
```
Once you run this command, a window opens that contains some sample tests you can run. 
Explore the examples to see how Cypress works.


### Next Steps
The next step is to Write your first Test. Have fun!