All Projects → maximilianschmitt → cypress-routines

maximilianschmitt / cypress-routines

Licence: MIT license
Easily write scalable Node.js setup code for Cypress

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to cypress-routines

Madara
✍️ A way for people to manage their tasks.
Stars: ✭ 17 (-60.47%)
Mutual labels:  cypress
svelte-starter-kit
Svelte starter kit — router, state management and testing included.
Stars: ✭ 16 (-62.79%)
Mutual labels:  cypress
vite-primevue-starter
VUE 3 Starter project for using primevue 3 with Vite 2 - Pages, Layouts, Validation
Stars: ✭ 37 (-13.95%)
Mutual labels:  cypress
app
Source code of intencje.pl website and mobile/desktop apps based on Angular, Firebase, and Capacitor.
Stars: ✭ 12 (-72.09%)
Mutual labels:  cypress
molecule-demo
Molecule for Ansible Role Testing Demonstration for AnsibleFest 2017
Stars: ✭ 18 (-58.14%)
Mutual labels:  integration-testing
jmockit2
The evolution of JMockit APIs: smaller, simpler, safer, and still powerful
Stars: ✭ 33 (-23.26%)
Mutual labels:  integration-testing
quasar-starter-ssr-pwa-jest-cypress
Accelerated starter kit for building a quasar 17 app.
Stars: ✭ 49 (+13.95%)
Mutual labels:  cypress
ng-nest-cnode
Angular 10 Front-End and Nestjs 7 framework Back-End build Fullstack CNode
Stars: ✭ 17 (-60.47%)
Mutual labels:  cypress
Recorder
A browser extension that generates Cypress, Playwright and Puppeteer test scripts from your interactions 🖱 ⌨
Stars: ✭ 277 (+544.19%)
Mutual labels:  cypress
dusker
Stand alone Laravel Dusk test suit, which do not require Laravel framework itself
Stars: ✭ 28 (-34.88%)
Mutual labels:  integration-testing
api-test
🌿 A simple bash script to test JSON API from terminal in a structured and organized way.
Stars: ✭ 53 (+23.26%)
Mutual labels:  integration-testing
cypress-get-it
Get elements by data attribute by creating a Cy command on the fly
Stars: ✭ 23 (-46.51%)
Mutual labels:  cypress
wargabantuwarga.com
Inisiatif warga untuk berbagi informasi seputar fasilitas kesehatan dan alat kesehatan untuk COVID-19.
Stars: ✭ 533 (+1139.53%)
Mutual labels:  cypress
duct
docker-compose alike functionality directly from golang, for integration testing.
Stars: ✭ 61 (+41.86%)
Mutual labels:  integration-testing
eyes-cypress
Applitools Eyes SDK for cypress.io - This repository is deprecated. It has moved to https://github.com/applitools/eyes.sdk.javascript1/tree/master/packages/eyes-cypress
Stars: ✭ 23 (-46.51%)
Mutual labels:  cypress
webring
“วงแหวนเว็บ” แห่งนี้สร้างขึ้นเพื่อส่งเสริมให้ศิลปิน นักออกแบบ และนักพัฒนาชาวไทย สร้างเว็บไซต์ของตัวเองและแบ่งปันการเข้าชมซึ่งกันและกัน
Stars: ✭ 125 (+190.7%)
Mutual labels:  cypress
orchestrator
The orchestrator executes all cypress specs across parallel docker containers based on a configuration file.
Stars: ✭ 61 (+41.86%)
Mutual labels:  cypress
nodejs-vuejs-mysql-boilerplate
Node.js (REST API) + Vue.js/Nuxt.js (Frontend/Backend) + MySQL Boilerplate
Stars: ✭ 134 (+211.63%)
Mutual labels:  cypress
instrument-cra
Little module for CRA applications to instrument code without ejecting react-scripts
Stars: ✭ 61 (+41.86%)
Mutual labels:  cypress
playwright-testing-library
🔍 Find elements in Playwright with queries from Testing Library
Stars: ✭ 160 (+272.09%)
Mutual labels:  integration-testing


Easily write scalable Node.js setup code for Cypress


CI Badge npm weekly downloads Badge

cypress-routines

Motivation

cy.task() allows Cypress users to run code in a Node.js process. However, all Cypress tasks run in a global namespace and as your app and number of different test setups grow, relying on cy.task() for test setups can become hard to maintain.

cypress-routines enables you to organize your test setups neatly per spec-file. Routines run in Node.js, so you can easily access things like databases and file systems in your test setups.

Screencast

cypress-routines screencast
Using cypress-routines to write and organize test setups that run in Node.js

Installation

1. Install cypress-routines

# With yarn:
yarn add cypress-routines --dev

# With npm:
npm install cypress-routines --save-dev

2. Require plugin-file

In cypress/plugins/index.js:

module.exports = async (on, config) => {
	const db = await connectDb() // 👈 Example

	// After `on, config`, you can pass e.g. db    👇
	require('cypress-routines/plugin')(on, config, db)
}

3. Require support-file

In cypress/support/index.js:

require('cypress-routines/support')

4. Ignore *.routines.js

In cypress.json:

{
	"ignoreTestFiles": ["*.routines.js"]
}

Usage guide

Where do I put my routines?

Routines live next to their respective spec-file:

cypress/
	integration/
		login.spec.js
		login.routines.js
		signup.spec.js
		signup.routines.js

You can also define global routines.

Writing routines

A routines-file is a simple node.js module that exports a factory-function that returns an object with functions ("routines") attached to it:

// cypress/integration/login.routines.js

function loginRoutines(db) {
	return {
		createUser(user) {
			await db.collection('users').insertOne(user)

			return user
		}
	}
}

module.exports = loginRoutines

The return-value of the routine will be accessible from the spec-file in the browser context, so it must be JSON-serializable.

The createUser routine from login.routines.js can be used from login.spec.js like so:

cy.routine('createUser', { email: '...' }).then(() => {
	// ...
})

Giving routines access to the database

In your Cypress plugin-file, pass the db (or any other parameters you like) after on, config to the function that's required as cypress-routines/plugin.

// cypress/plugin/index.js

module.exports = async (on, config) => {
	const db = await connectDb()

	// All arguments after `on, config` are passed along
	// to the routine-factories. In this case, we're passing
	// `db` so that every routines-file can access the db
	// if it needs to.
	require('cypress-routines/plugin')(on, config, db, param2, param3 /* etc. */)
}

The factory-functions in your routines files now have access to those params.

// cypress/integration/login.routines.js

function loginRoutines(db, param2, param3 /* etc. */) {
	return {
		// ...
	}
}

module.exports = loginRoutines

Calling routines

Routines are called with cy.routine(routineName: string, routineArg?: any). A routine can optionally take a single argument (must be JSON-serializable).

// cypress/integration/login.spec.js

it('logs in the user', function () {
	const routineArg = {
		email: '[email protected]',
		hashedPassword: hashPassword('123456'),
	}

	cy.routine('createUser', routineArg).then(() => {
		cy.visit('login')
		// ...
	})
})

a.spec.js, can only call routines defined in a.routines.js (not b.routines.js).

cy.routine(), like other Cypress commands, is asynchronous but cannot be used with async/await. Read here for more info on async commands.

Sharing routines across spec-files

Routines are scoped to their spec-files. For 95% of cases, this is what you want because it introduces clean separations between test-setups and makes it easy to find a routine that is used in a certain spec-file.

In some cases, you might want to reuse certain routines. There are two options for this:

  • Global routines
  • Sharing routine functions

Global routines

Global routines can be defined in cypress/integration/global-routines.js. The global routines-file looks like any other routines-file:

// cypress/global-routines.js

function globalRoutines(db) {
	return {
		async createDefaultUser() {
			const defaultUser = {
				email: '[email protected]',
				hashedPassword: hashPassword('123456'),
			}

			await db.collection('users').insertOne(defaultUser)

			return defaultUser
		},
	}
}

module.exports = globalRoutines

Global routines are called like regular routines, but with a leading '/':

// cypress/integration/login.spec.js

it('logs in the user', function () {
	//         👇 Leading '/'
	cy.routine('/createDefaultUser').then((testUser) => {
		cy.visit('login')
		// ...
	})
})

Sharing routine functions

You can always require other routines-files from any routines-file. You can then re-use and re-export functions with normal JavaScript:

// cypress/integration/login.routines.js

// Either export an entire other routines-file:
module.exports = require('./homepage.routines.js')

// Or export single functions:
module.exports = (db) => {
	const homepageRoutines = require('./homepage.routines.js')(db)

	return {
		createUser: homepageRoutines.createUser,
	}
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].