DEV Community

Marko Cen
Marko Cen

Posted on • Originally published at

Automate Node.js Website E2E Test with Cypress and GitHub Actions

In this article, we are going to explain how to setup a continue integration job on GitHub Actions to automatically trigger end-to-end testing by using Cypress e2e framework.

TLDR, the working code can be found here.

Not Familiar with Cypress?

Cypress is an open sourced modern e2e testing framework. It provides out of box solution to test website functionalities with cross browsers support. Cypress leverages the most popular testing libraries, like Sinon, Chai, etc, if you are familiar with these libraries, you can start to write Cypress e2e tests with zero learning curve. Cypress also super easy to setup, you can literally setup an Cypress testing server in 5 minutes by following its official documents. In this article, we are going to use Cypress to test our production website.

Why GitHub Actions?

GitHub Actions is the latest CI/DI platform introduced by GitHub. It deeply integrated with GitHub source control, you can easily build, test, deploy your code right from the your GitHub code repository. It supports most major platforms, like popular Linux distros, Windows and MacOS. GitHub also offers 2,000 Actions minutes per month for free account, which it is enough for most side/small projects. You can simply define GitHub Action jobs by using a yaml files and commit it into source control.

Build Website

First we are going to build the testing website using nodejs and express, our website has one page and one API handler. When users visit the website, they will see a login form, and login request will be handled by /submit api.

import express from "express";
import * as http from "http";
import * as path from "path";

const app = express();

app.get("/", (req, res) => {
});"/submit", (req, res) => {
  res.end("submit success!");

const server = http.createServer(app);

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Cypress E2E Test Demo Page</title>
    <!-- simple login form -->
        <input type="text" id="username" />
        <input type="password" id="password" />
      <button type="submit">Submit</button>

    <!-- we use axios as restful request client -->
    <script src=""></script>
      const form = document.querySelector('form');

      form.onsubmit = (e) => {
        const username = form.querySelector('#username').value;
        const password = form.querySelector('#password').value;"/submit", { username, password });

Setup E2E Test

First install Cypress through npm or yarn

npm install cypress -D

Then create cypress.json to config cypress server

  "baseUrl": "http://localhost:4040",
  "fixturesFolder": "e2e/fixtures",
  "integrationFolder": "e2e/tests",
  "screenshotsFolder": "e2e/screenshots",
  "videosFolder": "e2e/videos",
  "supportFile": false,
  "pluginsFile": false

We config the baseUrl to localhost because we are going to start the website in local CI environment. It also support remote url like using a deployed website url, in that case, e2e tests will be run against the production website. integrationFolder defined the path to test files, and fixturesFolder and screenshotsFolder will be used to locate fixture data and screenshot images.

The last thing is to write the test, for demonstrate purpose, we only have one test file which contains two tests to cover the login flow. You can check more usage details on cypress website.

// e2e/tests/index.test.js

describe("Index Page", () => {
  before(() => {

  it("should contain a form element", () => {
    const form = cy.get("form");

  it("should send http request with login payload when submit button clicked", () => {
    cy.route("POST", "/submit").as("submit");

    cy.wait("@submit").should("", "status", 200);

    cy.get("@submit").should((xhr) => {
      expect(xhr.request.body).to.deep.equal({ username: "test", password: "123abc" });
      expect(xhr.response.body).to.equal("submit success!");

Test Run

That's it, our Cypress server is ready to run, to start the server locally, execute npx cypress open, and the Cypress dashboard will show up


In dashboard, it lists the test we just wrote, we can select the testing browser and check previous running results. Now click 'Run all specs', Cypress will start to execute our tests (make sure our website is started before running tests!). We will see the target browser pop up and all tests executed successfully!


Define CI Pipeline

The last step is to setup GitHub Action workflow, workflow is a custom automated processes that we can set up in repository to build, test, package, release, or deploy any project on GitHub. We want the workflow can run e2e test automatically when there are new commits pushed to master branch, the workflow has several jobs, first it will checkout source code to local, then install nodejs and dependencies, finally start e2e testing.

To define workflow, we should create a yaml file and put it in .github/workflows folder, and commit it into source control. More workflow sytax detail can be found on here.

name: E2E Test

      - master

    runs-on: ubuntu-16.04

      - name: Checkout Code
        uses: actions/checkout@v2

      - name: Install Node
        uses: actions/setup-node@v1
          node-version: "14"

      - name: Install Dependencies
        run: npm i

      - name: Run Cypress
        uses: cypress-io/github-action@v2
          start: npm run start

Here we use the official cypress-io/github-action plugin to help us start cypress testing, but it's same as easy as setting up by ourself. Once this file committed and pushed, we will see workflow can be triggered by following commits.


Top comments (0)