Verify that a Cypress plugin extends Cypress commands
Preconditions (2)
✅ Expected Result: The test should run successfully using the new custom command added by the plugin
/// <reference types="cypress" /> // cypress/support/commands.js Cypress.Commands.add('loginByCustomCommand', (email, password) => { cy.get('input[name=email]').type(email) cy.get('input[name=password]').type(password) cy.get('button[type=submit]').click() }) // cypress/e2e/login_spec.cy.js describe('Login with custom plugin command', () => { it('should login successfully using the custom command', () => { cy.visit('https://example.cypress.io/login') cy.loginByCustomCommand('user@example.com', 'Password123!') cy.url().should('include', '/dashboard') }) })
The Cypress.Commands.add function is used to add a new custom command called loginByCustomCommand. This command types the email and password into the login form and clicks submit.
The test file login_spec.cy.js uses this new command to perform login. It visits the login page, calls the custom command with test credentials, and then asserts that the URL includes '/dashboard' indicating successful login.
This shows how plugins extend Cypress by adding reusable commands that simplify test scripts.
Now add data-driven testing with 3 different sets of login credentials using the custom command
cypress/plugins/index.js file as per Cypress structure.module.exports = (on, config) => {
on('task', {
log(message) {
console.log(message)
return null
}
})
}on('task', {...}) to define a custom task named 'log' that prints a message to the Node console.module.exports = (on, config) => {
on('task', {
readFile(path) {
return fs.readFileSync(path, 'utf8')
}
})
}fs.readFileSync but does not import the 'fs' module, causing a runtime error.const fs = require('fs')
module.exports = (on, config) => {
on('task', {
readJson(path) {
const content = fs.readFileSync(path, 'utf8')
return JSON.parse(content)
}
})
}fs.readFileSync to read the file synchronously, which is valid in plugins.JSON.parse and returns it.Pass the test and this pattern moves unknown