GithubHelp home page GithubHelp logo

ptzagk / cypress-example-recipes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cypress-io/cypress-example-recipes

0.0 0.0 0.0 6.15 MB

Various recipes for testing common scenarios with Cypress

Home Page: https://on.cypress.io/examples

JavaScript 100.00%

cypress-example-recipes's Introduction

Recipes CircleCI Travis CI Build status renovate-app badge

This repo contains various recipes for testing common scenarios using Cypress: Fundamentals, Testing the DOM, Logging in, Preprocessors, Blogs, Stubbing and spying, Unit Testing, Server Communication

Fundamentals

Recipe Description
Node Modules Import your own node modules
Environment variables Passing environment variables to tests
Dynamic tests Create tests dynamically from data
Fixtures Loading single or multiple fixtures
Adding Chai Assertions Add new or custom chai assertions
Cypress module API Run Cypress via its module API

Testing the DOM

Recipe Description
Tab Handling and Links Links that open in a new tab
Hover and Hidden Elements Test hidden elements requiring hover
Form Interactions Test form elements like input type range
Drag and Drop Use .trigger() to test drag and drop

Logging in recipes

Recipe Description
Single Sign On Log in across multiple servers or providers
HTML Web Forms Log in with a basic HTML form
XHR Web Forms Log in using an XHR
CSRF Tokens Log in with a required CSRF token

Also see Authentication plugins and watch video "Organizing Tests, Logging In, Controlling State"

Preprocessors

Recipe Description
grep Filter tests by name using Mocha-like grep syntax
Typescript with Browserify Add typescript support with browserify
Typescript with Webpack Add typescript support with webpack

Blogs

Demo recipes from the blog posts at www.cypress.io/blog

Recipe Description
Application Actions Application actions are a replacement for Page Objects
Direct Control of AngularJS Bypass the DOM and control AngularJS
E2E API Testing Run your API Tests with a GUI
E2E Snapshots End-to-End Snapshot Testing
Element Coverage Track elements covered by tests
Codepen.io Testing Test a HyperApp Codepen demo
Testing Redux Store Test an application that uses Redux data store
Vue + Vuex + REST Testing Test an application that uses central data store
A11y Testing Accessability testing with cypress-axe

Stubbing and spying

Recipe Description
Stubbing Functions Use cy.stub() to test function calls
Stubbing window.fetch Use cy.stub() to control fetch requests
Stubbing methods called on window Use cy.stub() for methods called on window
Stubbing Google Analytics Use cy.stub() to test Google Analytics calls

Unit Testing

Recipe Description
Application Code Import and test your own application code
React Test your React components in isolation
File Upload in React Test file upload in React app

Server Communication

Recipe Description
Bootstrapping your App Seed your application with test data
Seeding your Database in Node Seed your database with test data

Overview

  • This repo is structured similar to how other "Monorepos" work.
  • Each example project has it's own Cypress configuration, tests, backend and frontend assets.
  • Each of these example projects share a single "root" Cypress that is installed in the root node_modules folder.
  • This structure looks different from normal projects, but its the easiest way to manage multiple projects without installing Cypress independently for each one.

Installation

## install all dependencies
npm install

Opening Cypress GUI

cd ./examples/testing-dom__drag-drop
# start local server
npm start &
# and open Cypress GUI
npm run cypress:open

Running from the CLI

Same as running Cypress GUI but with cypress run command (and any CLI arguments)

cd ./examples/testing-dom__drag-drop
# start local server
npm start &
# run Cypress tests headlessly
npm run cypress:run

### runs all example projects in specific browser
### similar to cypress run --browser <name>
npm run cypress:run -- --browser chrome

### sends test results, videos, screenshots
### to Cypress dashboard
npm run cypress:run -- --record

Recipes

  • Import ES2015 modules.
  • Require CommonJS modules.
  • Organize reusable utility functions.
  • Import 3rd party node_modules.
  • Login when authentication is done on a 3rd party server.
  • Parse tokens using cy.request().
  • Manually set tokens on local storage.
  • Map external hosts and point to local servers.
  • Test a standard username/password HTML form.
  • Test errors submitting invalid data.
  • Test unauthenticated redirects.
  • Authenticate users with cookies.
  • Create a custom cy.login() test command.
  • Bypass needing to use your actual UI.
  • Increase speed of testing with cy.request().
  • Test an AJAX backed username/password form.
  • Test errors submitting invalid data.
  • Stub JSON based XHR requests.
  • Stub application functions.
  • Create a custom cy.login() test command.
  • Bypass needing to use your actual UI.
  • Increase speed of testing with cy.request().
  • Use cy.request() to get around CSRF protections.
  • Parse CSRF tokens out of HTML.
  • Parse CSRF tokens out of response headers.
  • Expose CSRF via a route.
  • Disable CSRF when not in production.
  • Test anchor links opening in new tabs: <a target="_blank">.
  • Test anchor links that link to external domains: <a href="...">.
  • Prevent content from opening in a new tab.
  • Request external content that would open in a new tab using cy.request().
  • Speed up tests by reducing loading times.
  • Interact with elements that are hidden by CSS.
  • Use .invoke() and .trigger() to simulate hovering.
  • Trigger mouseover, mouseout, mouseenter, mouseleave events. Get around the lack of a .hover() command.

This demo shows the cypress-axe plugin which can run the Axe-core library against the webpage to check if the page follows accessibility practices.

  • Invoke methods on the application's model object
  • Avoid code duplication and need to create page object hierarchy
  • Run e2e very quickly by skipping UI unless testing that specific UI feature
  • Blog article written here
  • Programmatically control AngularJS
  • Bypass the DOM, update scopes directly
  • Create custom command for controlling services
  • Blog post End-to-End Snapshot Testing
  • Adding .snapshot() command by requiring 3rd party module
  • Capturing and saving snapshots of primitive values
  • Testing central data Vuex store using snapshots
  • Making assertions against a DOM element with cy.get('...').snapshot()
  • Blog post
  • Load Codepen and get around iframe security restrictions.
  • Use cy.request() to load a document into test iframe.
  • Test HyperApp.js application through the DOM and through actions.
  • Blog post Element coverage
  • Overwrite several built-in Cypress commands like cy.type and cy.click
  • Draw elements after the tests finish
  • control application via DOM and check that Redux store has been properly updated
  • drive application by dispatching Redux actions
  • use Redux actions directly from tests
  • load initial Redux state from a fixture file
  • Blog post
  • Test a Vue.js web application that uses central data store
  • Mock REST calls to the server
  • Dispatch actions to the Vuex store
  • Test text file upload
  • Use cy.stub() to stub dependencies in a unit test.
  • Handle promises returned by stubbed functions.
  • Handle callbacks in stubbed functions.
  • Use cy.spy() to verify the behavior of a function.
  • Use cy.stub() to verify and control the behavior of a function.
  • Use cy.clock() and cy.tick() to control time.
  • Stub window.fetch to control server responses.
  • Replace window.fetch with a polyfill that uses XHR and is loaded only for tests.
  • Delete window.fetch during specific visit or every window load.
  • Use cy.spy() to test window.open behavior.
  • Use blacklistHosts to block Google Analytics from receiving requests.
  • Use cy.stub() to verify that window.ga(...) was called with the correct arguments
  • Unit test your own application code libraries.
  • Import modules using ES2015.
  • Test simple math functions.
  • Test the canonical fizzbuzz test.
  • Automatically retry assertion until a given property inside an object:
    • is added or deleted
    • has expected value
  • Extend chai with the chai-date-string assertion plugin.
  • Extend chai with the chai-colors assertion plugin.
  • Globally extend chai for all specs.
  • Set up Intelligent Code completion for custom assertions.
  • Run Cypress tests using its Node module API
require('cypress').run({
  // options
}).then(testResults => {
  // rerun failing specs
  // or email test report
  // or post it on Slack
  // ...
})
  • Use cy.visit() onBeforeLoad callback.
  • Start your application with test data.
  • Stub an XHR to seed with test data.
  • Wait on an XHR to finish.
  • Use cy.task() to communicate with node via the pluginsFile.
  • Seed your database with test data.
  • Wrap your pluginsFile so you can require files that use ES modules (import/export).
  • Pass values via env object in cypress.json.
  • Pass any variable that starts with CYPRESS_.
  • Extract any other variable from process.env using cypress/plugins/index.js callback.
  • Run the same test against different viewport resolutions
  • Run the same test against multiple subdomains
  • Generate tests based on the fetched data
  • loading a single fixture file or multiple fixtures in multiple ways

Development

See Development.md

cypress-example-recipes's People

Contributors

abramenal avatar bahmutov avatar brian-mann avatar chrisbreiding avatar danielswain avatar greenkeeper[bot] avatar jennifer-shehane avatar kuceb avatar ramosjoel avatar renovate[bot] 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.