Learning Visual Testing as a Junior Developer with Applitools and Cypress
Published: 2019-05-23 16:20:24

There is a first time for everything, and I was lucky to get a chance to learn about front end testing through a new program called the [This Dot Labs Open Source Apprentice Program](http://thisdot.co/hirethefempire) recently.
 
Apprentice programs are a great help for a person like me who was a mom returning to work after a year off to kick start things smoothly. For me, this program helped me learn tons of new things.

I was introduced to an amazing tool called [Applitools](http://applitools.com) which can ease our task as a developer to perform visual tests across an application. 

Applitools uses AI-powered image comparison technology which only reports differences that are noticeable to users and reliably ignores invisible rendering, size and position differences. It can instantly validate entire application pages, detect layout issues. Using the same baseline image it can validate any change in layout, missing elements, overlapping elements or size issues across multiple browsers and devices.

**How I made my first PR with Applitools and Cypress in Preact Material**

My first PR was introducing Applitools and Cypress to Preact-Material-Component. 

Preact-Material-Component is a wrapper over Google’s official material-components-web library. With over 50k downloads it has been an opinionless wrapper for Preact developers interested in using material design for their applications.

For their next major release, the maintainers are planning on adding custom code on top of Google’s library like gestures, experimental components and more. This is why they needed something more than just a puppeteer based pixel to pixel matching which already existed in their current version.

I was a little nervous to do add Applitools as both testing and front end were pretty new to me but thanks to my This Dot mentor things were a cake walk. 

There was some basic installation needed so that we could write our test.
Installing Cypress for Applitools: Applitools has an SDK for Cypress so it’s easy to use.


To install Cypress navigate to the folder path on the terminal.

Run the below command for installation.
``` shell
yarn add cypress --dev
```

This installs cypress as a dev dependency. 
For opening cypress on the system you can use:
``` shell
npx cypress open
```

When we run the cypress for the first time, a default structure is added to our code.
  ![](https://lh4.googleusercontent.com/qm2THSCcRwW-Y8A5ZVrZY_-kTTr97_8r5V6ceL685VN9wTVeoPbY0t15buWjdeO9GfcT5Bg1vwQi9DJUK6okHGnWaKNKPetZMBC_JHI6F495qT9vRPUG23PtaIvOaVtDNfb6EtxF)


To begin our test we add the eyes-cypress plugin to our cypress project. 
It basically adds three main methods, cy.eyesOpen to start the test, cy.eyesCheckWindow to take screenshots (for each test step) and cy.eyesClose to close the test.
``` shell
npm install @applitools/eyes-cypress --dev
```

We import the Applitools and Cypress dependencies in `plugins/index.js` and `commands/index.js`.
``` shell
require('@applitools/eyes-cypress')(module)
```
``` shell
import @applitools/eyes-cypress/commands
```

Next, add cy.eyesOpen to start the test inside it block.
**![](https://lh3.googleusercontent.com/oGGJoukQVX5JUsQyrEGTu9pvJfebNl9te_vHtvuMZGRt9CEpKOwf_2BUQYmPN6WE-K4ii_9ZUujWrQ3SfwpN8L3vfP-JHLedGYnNvA8LTStkILLgzeqvwaH80Ls5POxmJBuc3y11)**

Add one or more eyesCheckWindow checkpoints for various test steps.

**![](https://lh5.googleusercontent.com/8PF70QgqmkxQlRMxo_rO2gPHqP0Jz_lslJU4X5hz5_DHWoHCVZXTOIQmx_vjilIaoqwoHrNXQbCg_MjNLcEHvXDlIc--6krN4pAirHaUMtR9oZCBwKz4AlqnjcC2a8zxgYxwGxaH)**

Towards the end of the test, you add cy.eyesClose.
``` shell
cy.eyesClose()
```
To run Applitools:

1. Create Applitools account (open source libraries can get this for free [here](http://applitools.com/open-source)).
2. Go to the menu in the top right corner and click the profile icon on the far right.
**![](https://lh3.googleusercontent.com/5sS06CIMfO40C2pUgEbFoOArPJUkVYtmmcF2Q9gPqrMbCFBZM2x0udpYW61xNLuGJb2ehHH31MaMsm6I6Qir2cDNv5Eyzcw3ZkNntnf0gEo82FoYSP0nn9JC6FUXiVWRQjVrtzk7)**
3. Click on “My API Key” and copy your key.
4. Go to your terminal and paste the following:
export APPLITOOLS_API_KEY=Your_API_Key_Here
5. Go to your directory and run:
``` shell
yarn test:travis
```
6. In your Applitools dashboard, you should see the results of your tests.
**![](https://lh6.googleusercontent.com/jhq30QIa_1ItGKvgiwysvBIvf7TBsi4NP5G7Mt1XoouuC0oUtXwtQhi3ny5Yqpa8KBELSQt-jcpa9ouKLQJxgBTQ4XY77TPfUPDcL-MKju4qd1kNlBo0kMbfXZpydFFJ1i-LtotH)**

This tool amazed me as it helped me capture even the smallest details which may otherwise be overlooked during manual testing. 

So thanks, Applitools for teaching me testing, the importance of visual testing, and bringing out such an awesome tool which is in many ways a boon to the developers.

If you want to talk to me about [Applitools](http://twitter.com/applitools) or  The [This Dot Labs Apprentice Program](http://thisdot.co/hirethefempire), you can find me on Twitter [@_megsachdev](http://twitter.com/_megsachdev).