• This repository has been archived on 28/Feb/2023
  • Stars
    star
    136
  • Rank 267,670 (Top 6 %)
  • Language
    JavaScript
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Quickly generates Cypress tests from HTML and JS code

cypress-fiddle renovate-app badge CircleCI

Generate Cypress tests live from HTML and JS

Instantly experiment with Cypress tests by creating a tiny live HTML fiddle and running E2E tests against it.

runExample test

Install

Cypress is a peer dependency of this module. Install the current version of Cypress by running npm i -D cypress.

After installing Cypress, install this module via npm:

npm i -D @cypress/fiddle

Then load the custom command by adding the following line to cypress/support/index.js

// adds "cy.runExample()" command
import '@cypress/fiddle'

Use

Create a single test

You can take an object with an html property containing HTML and a test property containing Cypress commands and run the tests.

For example in the cypress/integration/spec.js file:

// loads TypeScript definition for Cypress
// and "cy.runExample" custom command
/// <reference types="@cypress/fiddle" />

const helloTest = {
  html: `
    <div>Hello</div>
  `,
  test: `
    cy.get('div').should('have.text', 'Hello')
  `
}

it('tests hello', () => {
  cy.runExample(helloTest)
})

Which produces

runExample test

Parameters

The test object can have multiple properties, see src/index.d.ts for all.

  • test JavaScript with Cypress commands, required

The rest of the properties are optional

  • html to mount as DOM nodes before the test begins
  • name the name to display at the top of the page, otherwise the test title will be used
  • description extra test description under the name, supports Markdown via safe-marked
  • commonHtml is extra HTML markup to attach to the live HTML (if any) element. Useful for loading external stylesheets or styles without cluttering every HTML block

The next properties are NOT used by cy.runExample but are used by the testExamples function from this package.

  • skip creates a skipped test with it.skip
  • only creates an exclusive test with it.only

Included scripts

You can include your own additional scripts by using environment variable block in cypress.json file

{
  "env": {
    "cypress-fiddle": {
      "scripts": [
        "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
      ]
    }
  }
}

Styles

Sometimes you want to inject external stylesheets and maybe custom style CSS into the frame (we already include Highlight.js). Pass additional CSS link urls and custom styles through environment variables in cypress.json config file.

{
  "env": {
    "cypress-fiddle": {
      "stylesheets": [
        "https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
      ],
      "style": "body { padding: 1rem; }"
    }
  }
}

Tip: it is more convenient to set multiline environment variables or even load CSS files from plugins file.

Create multiple tests

Instead of writing the cy.runExample() command one by one, you can collect all test definitions into a list or a nested object of suites and create tests automatically.

For example, here is a list of tests created from an array:

import { testExamples } from '@cypress/fiddle'

const tests = [
  {
    name: 'first test',
    description: 'cy.wrap() example',
    test: `
      cy.wrap('hello').should('have.length', 5)
    `
  },
  {
    name: 'second test',
    description: 'cy.wrap() + .then() example',
    test: `
        cy.wrap()
          .then(() => {
            cy.log('In .then')
          })
      `
  }
]
testExamples(tests)

List of tests

While working with tests, you can skip a test or make it exclusive. For example to skip the first test add a skip: true property.

{
  name: 'first test',
  description: 'cy.wrap example',
  skip: true
  ...
}

Or run just a single test by using the only: true property.

{
  name: 'first test',
  description: 'cy.wrap example',
  only: true
  ...
}

You can create suites by having nested objects. Each object becomes either a suite or a test.

import { testExamples } from '@cypress/fiddle'
const suite = {
  'parent suite': {
    'inner suite': [
      {
        name: 'a test',
        html: `
          <div id="name">Joe</div>
        `,
        test: `
          cy.contains('#name', 'Joe')
        `
      }
    ],
    'list test': {
      html: `
        <ul>
          <li>Alice</li>
          <li>Bob</li>
          <li>Cory</li>
        </ul>
      `,
      test: `
        cy.get('li').should('have.length', 3)
          .first().should('contain', 'Alice')
      `
    }
  }
}

testExamples(suite)

Tree of tests

Find more examples in cypress/integration folder.

Markdown

This package includes a JS/CoffeeScript/Markdown preprocessor that can find and run tests in .md files. Just surround the tests with HTML comments like this:

<!-- fiddle Test name here -->
Add additional text if you want. HTML code block is optional.

```html
<div>Example</div>
```

Test code block that should be run as a test
```js
cy.contains('Bye').should('be.visible')
```
<!-- fiddle-end -->

See example bahmutov/vuepress-cypress-test-example and live site. Read blog posts Run End-to-end Tests from Markdown Files and Self-testing JAM pages.

You can have common HTML block and split the test across multiple JavaScript code blocks. This is useful to explain the test step by step

This test has multiple parts. First, it confirms the string value
```js
cy.wrap('first').should('equal', 'first')
```
Then it checks if 42 is 42
```js
cy.wrap(42).should('equal', 42)
```

The actual test to be executed will be

cy.wrap('first').should('equal', 'first')
cy.wrap(42).should('equal', 42)

Skip and only

You can skip a fiddle, or run only a particular fiddle similar to it.skip and it.only

<!-- fiddle.skip this is a skipped test -->
<!-- fiddle.only this is an exclusive test -->

Note: there is also fiddle.export modifier. These fiddles are skipped during normal testing from Markdown, but exported and enabled in the output JavaScript specs.

Page title

If the Markdown file has page title line like # <some text>, it will be used to create the top level suite of tests

describe('<some text>', () => {
  // tests
})

Nested suites

You can put a fiddle into nested suites using / as a separator

<!-- fiddle Top / nested / test -->

Will create

describe('Top', () => {
  describe('nested', () => {
    it('test', () => {})
  })
})

Live HTML

You can include "live" html blocks in the fiddle - in that case they will become the test fragment.

<!-- fiddle includes live html -->
<div id="live-block">Live Block</div>
```js
cy.contains('#live-block', 'Live Block').should('be.visible')
```
<!-- fiddle-end -->

When including HTML source fragment and live HTML block, live HTML block wins and will be used as the test fragment.

<!-- fiddle includes both live and html block -->
```html
<div id="my-block">Block</div>
```

<div id="live-block">Live Block</div>

```js
// when including both live HTML block and
// html code block, the live HTML block wins
cy.contains('#live-block', 'Live Block').should('be.visible')
cy.contains('#my-block', 'Block').should('not.exist')
```
<!-- fiddle-end -->

Common Live HTML

If you have common HTML to load before the live HTML block, but do not want to show it in the HTML snippet, put it into a comment like this

<!-- fiddle-markup
<link rel="stylesheet" href="some CSS URL">
<style>
body {
  padding: 2rem;
}
</style>
-->

You can load styles and external CDN scripts using this approach.

Hiding fiddle in Markdown

You can "hide" fiddle inside Markdown so the page can test itself. See cypress/integration/hidden-fiddle.md example.

Markdown

Hidden fiddle Markdown

Rendered page

Hidden fiddle Markdown preview

Notice how only the summary is displayed

Test runner

Hidden fiddle test

Note: by default the summary element is displayed in the HTML. You can the fiddle completely using

<details style="display:none">
 ...
</details>

Installation

In your plugins file use

const mdPreprocessor = require('@cypress/fiddle/src/markdown-preprocessor')
module.exports = (on, config) => {
  on('file:preprocessor', mdPreprocessor)
}

And in cypress.json file allow Markdown files

{
  "testFiles": "*.md"
}

Warning: issue #5401

Exporting JS specs from Markdown fiddles

# saves path/to/md.js file
npx export-fiddle <path/to/md>
# adds optional "beforeEach" hook with cy.visit(url)
npx export-fiddle <path/to/md> --before-each <url>
# adds optional "before" hook with cy.visit(url)
npx export-fiddle <path/to/md> --before <url>

Debug

To see debug logs, use DEBUG=@cypress/fiddle when running Cypress.

Publishing

Automatic semantic release on CircleCI using Cypress Circle Orb, see circle.yml file.

More Repositories

1

cypress

Fast, easy and reliable testing for anything that runs in a browser.
JavaScript
46,739
star
2

cypress-realworld-app

A payment application to demonstrate real-world usage of Cypress testing methods, patterns, and workflows.
TypeScript
5,426
star
3

cypress-example-recipes

Various recipes for testing common scenarios with Cypress
JavaScript
3,113
star
4

github-action

GitHub Action for running Cypress end-to-end & component tests
JavaScript
1,346
star
5

cypress-example-kitchensink

This is an example app used to showcase Cypress.io testing.
HTML
1,130
star
6

cypress-docker-images

Docker images with Cypress dependencies and browsers
Dockerfile
906
star
7

cypress-documentation

Cypress Documentation including Guides, API, Plugins, Examples, & FAQ.
TypeScript
827
star
8

eslint-plugin-cypress

An ESLint plugin for projects that use Cypress
JavaScript
701
star
9

cypress-react-unit-test

Unit test React components using Cypress
678
star
10

testing-workshop-cypress

End-to-end testing workshop with Cypress
JavaScript
479
star
11

code-coverage

Saves the code coverage collected during Cypress tests
JavaScript
431
star
12

cypress-vue-unit-test

A little helper to unit test Vue components in the Cypress.io E2E test runner
294
star
13

cypress-example-todomvc

The official TodoMVC tests written in Cypress.
JavaScript
237
star
14

cypress-chrome-recorder

Export Cypress Tests from Google Chrome DevTools' Recorder
TypeScript
231
star
15

cypress-and-jest-typescript-example

Example using Jest and Cypress with TypeScript in a single repo
TypeScript
231
star
16

cypress-skip-test

Simple commands to skip a test based on platform, browser or a url
JavaScript
180
star
17

circleci-orb

Install, cache and run Cypress.io tests on CircleCI with minimal configuration.
159
star
18

cypress-recorder-extension

JavaScript
157
star
19

cypress-xpath

Adds XPath command to Cypress test runner
JavaScript
147
star
20

cypress-example-docker-compose

Example running Cypress tests against Apache server via docker-compose
JavaScript
142
star
21

cypress-grep

Filter tests using substring
JavaScript
138
star
22

cypress-example-docker-circle

Cypress + Docker + CircleCI = ❀️
JavaScript
124
star
23

cypress-example-api-testing

[ARCHIVED] Cypress E2E runner can also test Rest and other APIs
JavaScript
120
star
24

snapshot

Adds value / object / DOM element snapshot testing support to Cypress test runner
JavaScript
116
star
25

cypress-example-conduit-app

[ARCHIVED] Conduit example blogging app.
JavaScript
108
star
26

cypress-webpack-preprocessor

Cypress preprocessor for bundling JavaScript via webpack
93
star
27

netlify-plugin-cypress

Runs Cypress end-to-end tests after Netlify builds the site but before it is deployed
JavaScript
89
star
28

add-cypress-custom-command-in-typescript

Testing how new Cypress commands are added in TypeScript
TypeScript
88
star
29

chromium-downloads

A website that helps users to find and download archived Chromium versions.
JavaScript
87
star
30

cypress-realworld-testing

Next.js project for learn.cypress.io
MDX
77
star
31

cypress-realworld-testing-course-app

TypeScript
71
star
32

schema-tools

Validate, sanitize and document JSON schemas
TypeScript
71
star
33

instrument-cra

Little module for CRA applications to instrument code without ejecting react-scripts
JavaScript
64
star
34

cypress-component-testing-apps

TypeScript
63
star
35

cypress-test-tiny

Tiny Cypress E2E test case
JavaScript
52
star
36

cypress-example-todomvc-redux

Example TodoMVC application with full code coverage
JavaScript
50
star
37

cypress-test-nested-projects

[ARCHIVED] Tests Cypress running tests in subfolders
JavaScript
48
star
38

xvfb

Easily start and stop an X Virtual Frame Buffer from your node apps
JavaScript
40
star
39

cypress-tutorial-build-todo

Step by step code for the Cypress tutorial in which we build and test a todo app
JavaScript
38
star
40

cypress-tutorial-build-todo-starter

Starter project for the Cypress tutorial in which we build and test a todo app
CSS
38
star
41

cypress-cli

CLI for Cypress.io Desktop App
CoffeeScript
38
star
42

cypress-component-testing-examples

Cypress component examples
JavaScript
33
star
43

birdboard

Example Twitter client web app shown in Cypress in a Nutshell webcast.
JavaScript
33
star
44

cypress-example-docker-circle-workflows

Cypress + Docker + CircleCI Workflows = ❀️
JavaScript
29
star
45

cypress-example-circleci-orb

Demo of using the Cypress CircleCI Orb
JavaScript
28
star
46

cypress-test-example-repos

[ARCHIVED] Tests new version of Cypress against multiple projects
JavaScript
27
star
47

cypress-workshop-ci

A workshop that teaches you how to run Cypress on major CI providers
CSS
27
star
48

cypress-realworld-testing-blog

A Next.js Blog for the Real World Testing with Cypress Curriculum
JavaScript
26
star
49

angular-pizza-creator

Example Angular Pizza ordering app
TypeScript
26
star
50

cypress-browserify-preprocessor

Cypress preprocessor for bundling JavaScript via browserify
JavaScript
23
star
51

cypress-example-docker-compose-included

Cypress example with docker-compose and cypress/included image
JavaScript
20
star
52

cypress-electron-plugin

Cypress plugin for testing Electron applications
JavaScript
19
star
53

cypress-heroes

Cypress Heroes Demo App
TypeScript
18
star
54

cypress-component-examples

Cypress configured with various frameworks and dev servers
JavaScript
17
star
55

commit-info

Collects Git commit info using git
JavaScript
17
star
56

react-tooltip

A tooltip component for React apps
JavaScript
17
star
57

cypress-example-reporters

[ARCHIVED] Example showing multiple test reports merged into a single Mochawesome report
JavaScript
17
star
58

cra-template-cypress

The base Cypress template for Create React App
JavaScript
17
star
59

cypress-mock-ssr

Node.js Mock SSR Middleware and Cypress Commands
JavaScript
16
star
60

cra-template-cypress-typescript

The base Cypress + TypeScript template for Create React App
TypeScript
15
star
61

cypress-realworld-testing-todomvc

A TodoMVC Application for the Testing Your First Application Course in the Real World Testing with Cypress Curriculum
JavaScript
12
star
62

cypress-design

Find here all the components to build UI with the Cypress Brand
TypeScript
12
star
63

cypress-example-docker-codeship

Cypress + Docker + Codeship Pro = ❀️
Dockerfile
12
star
64

cypress-example-electron

Electron.js application tested with Cypress - WIP
JavaScript
12
star
65

gh-action-and-gh-integration

Example project that uses both Cypress GH Action and Cypress GH Integration
JavaScript
12
star
66

todomvc

Vanilla JS TodoMVC with Cypress Tests
JavaScript
11
star
67

cypress-gh-action-vue-example

Testing an app scaffolded with Vue CLI using Cypress GH Action
Vue
10
star
68

netlify-plugin-cypress-example

An example site built and tested on Netlify using netlify-plugin-cypress
CSS
9
star
69

cypress-watch-preprocessor

Simple preprocessor that only watches files
JavaScript
8
star
70

cypress-workshop-ci-example

A simple example app to be used during cypress-workshop-ci session
HTML
7
star
71

cypress-example-netlify-store

πŸ› A Tested E-Commerce Site with Stripe payment
Vue
7
star
72

feature-maybe

Functional feature toggles on top of any object
JavaScript
6
star
73

cypress-icons

Cypress logos, icons, favicons, tray, iconset
JavaScript
6
star
74

cypress-parcel-preprocessor

Cypress preprocessor for bundling JavaScript via Parcel
JavaScript
6
star
75

cypress-test-module-api

[ARCHIVED] Example running specs using Cypress via its module API
JavaScript
6
star
76

full-network-proxy

Demo repo for Cypress with full network stubbing support
JavaScript
5
star
77

circleci-orb-parallel-example

Using Cypress CircleCI Orb to quickly run tests in parallel
JavaScript
5
star
78

v8-snapshot

Tool to create a snapshot for Electron applications.
TypeScript
5
star
79

cypress-heroes-app

Demo app for Cypress
TypeScript
5
star
80

cypress-repeat-retry

Stress-testing Cypress test retries
JavaScript
5
star
81

get-windows-proxy

Node.js module that finds a user's system proxy settings depending on their platform.
JavaScript
5
star
82

component-testing-quickstart-apps

Apps from the Cypress Component Testing quickstart guides
HTML
5
star
83

bumpercar

[ARCHIVED] Easily update settings and trigger builds across projects and CI providers
CoffeeScript
4
star
84

generator-node-cypress

Yeoman generator for PUBLIC Node packages from Cypress.io team
JavaScript
4
star
85

error-message

User-friendly error text with additional information
JavaScript
4
star
86

cypress-load-test

JavaScript
3
star
87

mksnapshot

A rewrite of electron/mksnapshot to support multiple electron versions.
TypeScript
3
star
88

cypress-migrator

Apps and libraries related to the Cypress Migrator tool
TypeScript
3
star
89

cypress-adapter-ruby

Cypress Ruby Driver
Ruby
3
star
90

debugging-proxy

A simple, pass-through HTTP proxy that works with HTTP/HTTPS. For debugging applications to make sure they still work behind a proxy.
JavaScript
3
star
91

testing-workshop-cph

End-to-end testing workshop with Cypress at CopenhagenJS
JavaScript
3
star
92

jsnation-example

TodoMVC example tests for JSNation conference
JavaScript
3
star
93

eslint-plugin-dev

Common ESLint rules and configuration shared by Cypress packages
JavaScript
3
star
94

cypress-chrome-recorder-extension

JavaScript
2
star
95

cypress-test-ci-environments

[ARCHIVED] Confirms that missing Xvfb or dependencies can be detected by Cypress
JavaScript
2
star
96

3rd-party-error

Example showing 3rd party JavaScript error
JavaScript
2
star
97

env-or-json-file

Loads JSON object from environment string or local file
JavaScript
2
star
98

cypress-ct-definition-template

Template for authoring Component Framework Definitions
TypeScript
2
star
99

cypress-onboarding-demo

JavaScript
1
star
100

knex-timings

Prints each Knex operation and its timings
JavaScript
1
star