18 Sep 2020
5 min

Write Better Automated Tests With Cypress in Angular

Hello devs and testers, in this article, I'll show you how we can write automated end-to-end tests on an angular project which is Todo App on cypress. We will have some scenarios for doing our e2e tests and we will be covering it one by one. This article will not probably be covering angular parts but cypress parts.

So if you just want to write some tests right now, do clone this repo which I have already created with the angular Todo App project and cypress configured in it. Here's the repo link https://gitlab.com/mquanit/angular-items. Just clone this, if you want everything ready for playing with cypress.

So, before moving to our examples, first, we have to know what is Cypress. According to its official Website

Cypress is an automated end-to-end testing framework for writing automated tests

But why should we use cypress when we have many other testing tools like Protactor, Karma, Mocha, etc.

Cypress is much like Protractor for Angular applications, but Cypress is much faster to run and easier to debug. Cypress not only is good at the automated end-to-end testing that is independent of our applications, but also is capable of unit testing against methods in model classes, service classes, and so on. Cypress provides a complete end-to-end testing experience in your browser where you can see your tests in an automated way.

Cypress provides it's own test runner where you can do the tests locally. Cypress provides some other cool features like Time Travel, Debuggability, Real-time reloads, Automatic waiting. These brilliant features make apart this tool and we will see these in action.

Enough talk. If you've already cloned this repo then you're good to go but if not then you probably have an Angular project and you just have to add Cypress as a dependency. We only need to install the cypress NPM package using the following command:

npm i -D cypress

The cypress package includes a desktop app and the Cypress binary. It may take a few minutes to run this command for the first time because the package installation needs to download the Cypress binary. The Cypress binary is saved in a global cache directory, so installing this package in the future will be much faster for the same version.

After Installing Cypress, a folder named cypress and a file named cypress.json has been added into your project's root folder. The generated cypress folder is the place where we write tests and the place where Cypress generates artifacts. The generated configuration file cypress.json contains an empty JSON object. This file is the place where we configure the default behavior of Cypress. So I will be doing some configuration before writing our tests.

In your cypress.json file, add the below code for the default config,

{
  "baseUrl": "http://localhost:4200",
  "ignoreTestFiles": "**/examples/*",
  "viewportHeight": 760,
  "viewportWidth": 1080
}

As their name suggests, you will write your tests on baseUrl which is localhost:4200 for angular projects.
When you add Cypress into your project, it provides an examples folder in which some demo test files are included, but we don't need default example tests in our project so we exclude this from our Test Suite.

So, that's all in our setup. Let's write some code for our first test.

In your folder structure under the cypress folder, you'll see an integration folder, that is where you'll write all your tests so that you'll see on your test suite.

Create your first test file, DemoTest.spec.js and add the below code in that file.

describe("Our Todo App Test Suite", () => {
  it("Visiting our app", () => {
    cy.visit("/");
    cy.get(".nav-wrapper").contains("Items Manager");
  });
});

In the above code, we used the describe function which is responsible for all our cases. It's often called a Suite or Test Suite. Under the describe function, we used the it function where we write our test code. In the above case, we are visiting our angular app by using cy.visit("/") and we are verifying that it'll contain a nav-wrapper class which contains text Items Manager.

There are many API's provided by cypress from getting an element to make assertions and many more. Here cy.get() is used to get the element's reference from DOM. This is the most used API from cypress.

Now open second cmd terminal and type,

npx cypress open       //to open cypress test runner

It will take some time at first, but you'll see something like this.

Alt Text

This is called Test Runner. Click on your file name DemoTest.spec.js and there is another instance of the browser that will open for your test. Now you'll see how easy and fast is it to work on cypress tests.

Let's write another test for typing into input fields and then add new todo by clicking the Submit button.

describe("Our Todo App Test Suite", () => {
  it.only("Type title and description", () => {
    cy.visit("/");
    cy.get("input[name='title']").type("Lunch")
    cy.get("input[name='description']").type("Eating lunch at 1")
    cy.get('input[type="submit"]').click()
    cy.get("ul.collection").find("li > strong").should("contain", "Lunch")
  });
  });
});

When you run this case, it will type into both input fields and click on the Submit button which in this case will add a new Todo Item in our Todo list and in last we are asserting that our added todo contains text Lunch

Now let's see an example of deleting a todo item and verify that it is deleted.

describe("Our Todo App Test Suite", () => {
  it.only("Type title and description", () => {
    cy.visit("/");
    cy.get("ul.collection > li").eq(1).find("a").click()
    cy.get("ul.collection > li").eq(1).find("form").find('button[class="btn red"]').click()
    cy.get("ul.collection > li").eq(1).should("not.exist")
  });
  });
});

So that's how you do e2e testing by cypress. Now try to update some value by cypress by yourself. If you are able to do it successfully then congrats, you can call yourself an Automation Test Engineer because that's what automation test engineers do on their roles.

So I hope you guys like this article and learn something new and don't forget to follow me for more of these kinda articles.

Happy Testing ✌️✌️

Frequently asked questions

How do I add Cypress to an existing Angular project?

Install it as a dev dependency with npm i -D cypress, which adds a cypress folder and a configuration file to your project root. The npm package includes the Cypress binary, which is downloaded on first install and saved in a global cache, so later installs of the same version are faster. Your tests go in the generated cypress folder.

How do I set baseUrl for an Angular app in Cypress?

Set baseUrl in the cypress.json file in the project root to 'http://localhost:4200', the address Angular projects are typically served on. With this value, calls like cy.visit('/') resolve against that address, so tests don't need the full URL. Keep the Angular app running on that port while the tests execute.

How do I run Cypress tests with the interactive test runner?

Run npx cypress open in a terminal to launch the Cypress Test Runner, then click a spec file such as DemoTest.spec.js to run it in a browser instance. The runner offers Time Travel, debuggability, real-time reloads, and automatic waiting. The first launch can take a while because the Cypress binary is set up.

How do I type into an input and click a submit button in Cypress?

Use cy.get() with a selector to find each element, then chain .type() to enter text and .click() to trigger the action. In a todo form, selectors such as input[name='title'] and input[type='submit'] locate the fields and button. Finish with .should('contain', 'Lunch') on the resulting list item to assert that the new item was added.

How do I assert that an element was removed in Cypress?

Chain .should('not.exist') onto a cy.get() query to assert that the element is no longer in the DOM. Cypress retries this assertion until it passes or times out, so it also works after an asynchronous delete. Query the element again after the action, for example cy.get('ul.collection > li').eq(1).should('not.exist').

How do I exclude Cypress example test files from my test run?

Add ignoreTestFiles set to '**/examples/*' in cypress.json so Cypress skips the demo specs generated during installation. Those examples aren't needed in your own test suite, so excluding them keeps the runner focused on your specs. Your own files, such as DemoTest.spec.js in the integration folder, are still picked up.

Share this post

Sign up for our newsletter

Stay up-to-date with the trends and be a part of a thriving community.