Custom plugin development in Cypress - Build an Automation Script
/// <reference types="cypress" /> // cypress/plugins/index.js module.exports = (on, config) => { on('task', { logMessage(message) { console.log(message) return null } }) } // cypress/e2e/plugin_test.cy.js describe('Custom plugin task test', () => { it('should log message via custom plugin task', () => { cy.task('logMessage', 'Hello from plugin') }) })
The cypress/plugins/index.js file defines a custom task named logMessage. This task receives a message and logs it to the Node.js console. Returning null indicates no value is returned to the test.
The test file plugin_test.cy.js calls cy.task('logMessage', 'Hello from plugin') to invoke the plugin task. When the test runs, the message appears in the terminal console where Cypress runs.
This setup uses Cypress's plugin system correctly, separating plugin code from test code, and uses the task API to communicate between the browser and Node environment.
Now add a custom plugin task that returns the current date and time, and write a test to verify it returns a valid date string.
Practice
Solution
Step 1: Understand what plugins do in Cypress
Plugins allow adding new commands or tasks that Cypress does not provide by default.Step 2: Differentiate from other options
Changing browser behavior or writing tests in other languages is not the role of plugins. Speeding up tests automatically is not guaranteed by plugins.Final Answer:
To add new commands or tasks that extend Cypress functionality -> Option AQuick Check:
Plugins extend Cypress features = D [OK]
- Thinking plugins change browser behavior directly
- Confusing plugins with test case languages
- Assuming plugins automatically speed up tests
Solution
Step 1: Identify the plugin file location
Custom plugins are defined in thecypress/plugins/index.jsfile where Node.js code runs.Step 2: Differentiate from support and config files
support/commands.jsis for custom commands in browser tests,integration/spec.jsis for test specs, andcypress.jsonis config.Final Answer:
cypress/plugins/index.js -> Option AQuick Check:
Plugins file = C [OK]
- Confusing plugins file with support commands file
- Choosing test spec files for plugins
- Selecting config file for plugin code
cypress/plugins/index.js:
module.exports = (on, config) => {
on('task', {
logMessage(message) {
console.log(message)
return null
}
})
}
What happens when the test calls cy.task('logMessage', 'Hello')?Solution
Step 1: Understand the plugin task definition
The plugin defines a task named 'logMessage' that logs the message to the Node.js console and returns null.Step 2: Understand cy.task behavior
Callingcy.task('logMessage', 'Hello')sends 'Hello' to the plugin task, which logs it in Node.js, not browser, and returns null.Final Answer:
The message 'Hello' is printed in the Node.js console, and the task returns null -> Option CQuick Check:
cy.task logs in Node.js console = A [OK]
- Thinking console.log prints in browser console
- Assuming tasks cannot take arguments
- Confusing tasks with custom commands
module.exports = (on, config) => {
on('task', {
multiplyNumbers(a, b) {
return a * b
}
})
}
And select the correct fix.Solution
Step 1: Understand how Cypress tasks receive arguments
Cypress tasks accept only one argument. Multiple arguments must be wrapped in an object.Step 2: Identify the error and fix
The function multiplyNumbers expects two arguments, but Cypress will pass only one. Fix by accepting a single object with properties a and b.Final Answer:
Change the task to accept a single argument object like {a, b} instead of two arguments -> Option BQuick Check:
Tasks accept one argument object = B [OK]
- Defining task functions with multiple parameters
- Using 'off' instead of 'on' to register tasks
- Thinking tasks must return Promises always
Solution
Step 1: Understand plugin environment
Plugins run in Node.js, so Node.js modules likefscan be used to read files synchronously or asynchronously.Step 2: Choose correct method to read file
cy.readFileis a test command, not available in plugins. Browser fetch is not available in Node.js plugin context. Usingfs.readFileSyncand returning parsed JSON is correct.Final Answer:
Use Node.js fs.readFileSync inside the plugin task and return parsed JSON -> Option DQuick Check:
Plugins use Node.js fs to read files = A [OK]
- Trying to use cy commands inside plugins
- Using browser APIs in Node.js plugin code
- Returning undefined instead of file content
Ready to prove it?
Pass the test and this pattern moves unknown
