Git Product home page Git Product logo

cypress-followalong's Introduction

Cypress Follow Along

Cypress is an end to end testing framework like Selenium, and works on a real browser instead of headless browser. Read more about Cypress.io.

Setup

Depends on NodeJS.

npm install --save-dev cypress
npx cypress open 

Creates a bunch of example tests to get us familiar with the UI. The test files are at cypress/integration/examples/*.spec.js. My tests are in the folder cypress/integration/mytests

target-app contains a simple react app to test.

Selecting Elements

Element tags, names, CSS, id are bad (increasing better, but still bad) as any refactoring or style change can have an impact. Recommended way is to use data-* attribute. Some testing frameworks use data-test-id, while cypress recommends data-cy. I recommend data-test-id or something generic so that we wont have framework dependent nomenclature (relevant in the event we have to switch it in future).

Use aliasing for better readability of tests.

cy.get('[data-cy="last-name-chars-left-count"]')
    .as('charsLeftSpan');

You can also use results and expect to write tests, but they tend to be more verbose. Also, the .then does not imply its a standard promise.

cy.get('@charsLeftSpan')
    .then(span => {
        expect(span.text()).to.equal('15');
    });

Move common element aliasing to beforeEach. Also, we can move the base URL to cypress.json and use the path in tests.

Common Interactions

  • Double click
cy.get('[data-cy="box-1-items-list"] > :nth-child(2)')
    .dblclick();
  • Checkbox
cy.get('[data-cy="box-2-checkboxes"] > :nth-child(1) input')
    .check();
  • Dropdown selection
cy.get('[data-cy="box-3-dropdown"]')
    .select('Option Three');
  • Use trigger for more complicated interactions
cy.get('[data-cy="box-4-items-list"] > :nth-child(2)')
    .trigger('mouseover');

For more details check out the official docs.

Assertions

Check out the list of assertions from official docs

Automatic Retries

Cypress keeps retrying failing command (not test) till they pass or a timeout occurs (default at 4s). This resolves problems around most flaky tests.

Debugging

Since it is asynchronous, putting debugger between cypress commands will not have desired effect. Long hand method:

cy.get('[data-cy="box-4-items-list"] > :nth-child(2)')
    .trigger('mouseover')
    .then(() => {
        debugger;
    });

Shorthand:

cy.get('[data-cy="box-4-items-list"] > :nth-child(2)')
    .trigger('mouseover')
    .debug();

This also sets some variables in the console for easy access, like the subject.

Test Doubles

Generally, use it sparingly as we most often want to test actual behaviour. Cypress wraps Sinon.js, and we can use stub and spy when needed.

Environment Variables

Check out official docs.

Cypress Studio

Checkout the Studio feature to interactively add tests.

Acknowledgements

This repository is my follow along of the LinkedIn Learning course End-to-End JavaScript Testing with Cypress.io by Shaun Wassell

cypress-followalong's People

Contributors

royniladri avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.