Plugins add new features to Cypress that are not built-in. They help you do more with your tests easily.
Why plugins extend Cypress capabilities
Introduction
Syntax
Cypress
module.exports = (on, config) => { // register event listeners or tasks here on('task', { log(message) { console.log(message) return null } }) return config }
This code goes in the cypress/plugins/index.js file.
The on function listens for events or tasks Cypress triggers.
Examples
Cypress
on('task', { readFile(filename) { const fs = require('fs') return fs.readFileSync(filename, 'utf8') } })
Cypress
on('before:browser:launch', (browser = {}, launchOptions) => { if (browser.name === 'chrome') { launchOptions.args.push('--disable-dev-shm-usage') } return launchOptions })
Sample Program
This example shows a plugin that logs messages to the terminal. The test calls the plugin task to print a message.
Cypress
// cypress/plugins/index.js module.exports = (on, config) => { on('task', { log(message) { console.log('Plugin log:', message) return null } }) return config } // cypress/e2e/sample.cy.js describe('Plugin task test', () => { it('logs a message using plugin task', () => { cy.task('log', 'Hello from test!') }) })
Important Notes
Plugins run in Node.js, outside the browser, so they can do things tests cannot.
Always keep plugin code simple and secure to avoid slowing tests or causing errors.
Summary
Plugins add extra power to Cypress by letting you do things outside the browser.
Use plugins to customize, integrate, or extend Cypress features.
Plugins are written in the cypress/plugins/index.js file and use event listeners.
Practice
1. What is the main reason to use plugins in Cypress?
easy
Solution
Step 1: Understand Cypress plugin purpose
Plugins let Cypress do things that normal browser tests cannot, like file system access or custom tasks.Step 2: Compare options with plugin use
Only To add extra features that work outside the browser correctly describes extending Cypress outside the browser environment.Final Answer:
To add extra features that work outside the browser -> Option AQuick Check:
Plugins extend Cypress beyond browser limits = C [OK]
Hint: Plugins add power outside browser, not inside tests [OK]
Common Mistakes:
- Thinking plugins only speed up test writing
- Confusing plugins with UI changes
- Believing plugins fix test failures automatically
2. Which file is used to write Cypress plugins?
easy
Solution
Step 1: Identify plugin file location
Cypress plugins are written in thecypress/plugins/index.jsfile as per Cypress structure.Step 2: Eliminate other files
Other files are for commands, tests, or config, not plugins.Final Answer:
cypress/plugins/index.js -> Option BQuick Check:
Plugins live in plugins/index.js file = A [OK]
Hint: Plugins always go in plugins/index.js file [OK]
Common Mistakes:
- Choosing support or integration files for plugins
- Confusing config file with plugin file
- Not knowing Cypress folder structure
3. What will this plugin code do?
module.exports = (on, config) => {
on('task', {
log(message) {
console.log(message)
return null
}
})
}medium
Solution
Step 1: Analyze the plugin code
The code useson('task', {...})to define a custom task named 'log' that prints a message to the Node console.Step 2: Understand task purpose
This task can be called from tests to log messages outside the browser.Final Answer:
Defines a task named 'log' that prints messages to the console -> Option CQuick Check:
Task 'log' prints messages = D [OK]
Hint: Tasks run in Node, not browser console [OK]
Common Mistakes:
- Thinking task runs inside browser
- Confusing task with event listener
- Assuming it overrides default logs
4. Identify the error in this plugin code:
module.exports = (on, config) => {
on('task', {
readFile(path) {
return fs.readFileSync(path, 'utf8')
}
})
}medium
Solution
Step 1: Check code dependencies
The code usesfs.readFileSyncbut does not import the 'fs' module, causing a runtime error.Step 2: Verify other parts
The event name 'task' is correct, return type is valid, and plugin files are JavaScript files.Final Answer:
Missing import of 'fs' module -> Option AQuick Check:
Using fs without import causes error = B [OK]
Hint: Always import Node modules before use in plugins [OK]
Common Mistakes:
- Assuming event name is wrong
- Thinking return type must be a promise
- Believing plugin files must be JSON
5. You want to create a plugin that reads a JSON file and returns its content as an object to your test. Which plugin code correctly does this?
const fs = require('fs')
module.exports = (on, config) => {
on('task', {
readJson(path) {
const content = fs.readFileSync(path, 'utf8')
return JSON.parse(content)
}
})
}hard
Solution
Step 1: Check file reading method
The code usesfs.readFileSyncto read the file synchronously, which is valid in plugins.Step 2: Verify JSON parsing
It correctly parses the JSON string to an object usingJSON.parseand returns it.Final Answer:
This code correctly reads and parses JSON file content -> Option DQuick Check:
Sync read + JSON.parse returns object = A [OK]
Hint: Use sync read and JSON.parse in plugins for JSON files [OK]
Common Mistakes:
- Thinking JSON.parse is disallowed in plugins
- Believing async read is required
- Returning raw string instead of parsed object
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
