glissario / bootcamp-schedule

bootcamp progress
0 stars 0 forks source link

Testing: Cypress Setup #86

Closed codingbootcampseu closed 3 years ago

codingbootcampseu commented 3 years ago

Step by Step Setup Guide

NPM Scripts

Serve Script

"scripts": {
  "serve": "serve ./src"
}

Run the serve script with npm run serve to start your local development server.

Remember to keep the server open while you develop or run cypress

Cypress

"scripts": {
  "e2e": "cypress open"
}
{
  "baseUrl": "http://localhost:5000"
}

The url must match to your personal setup and port

Add Own Tests

todo.spec.js

describe("todo App", () => {
  it("should allow to enter text", () => {
    cy.visit("/");
    cy.get("#input")
      .type("learn cypress")
      .should("have.value", "learn cypress");
  });

  it("should add li elements", () => {
    cy.visit("/");
    cy.get("#input").type("learn cypress");

    cy.get("#add-button").click();

    cy.get("#list").get("li").should("have.length", 1);
  });
});

Ressources

Example Source Code

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" id="input" /><button id="add-button">Add</button>
    <ul id="list"></ul>
    <script src="script.js"></script>
  </body>
</html>

script.js

const input = document.querySelector("#input");
const addButton = document.querySelector("#add-button");
const list = document.querySelector("#list");

addButton.addEventListener("click", addTodo);

function addTodo() {
  const newTodo = input.value;
  const newLi = document.createElement("li");
  newLi.innerText = newTodo;

  list.appendChild(newLi);
}