Test Overview
This test checks that a Cypress custom command correctly extends Cypress commands by adding a custom command. It verifies the custom command runs and produces the expected result.
This test checks that a Cypress custom command correctly extends Cypress commands by adding a custom command. It verifies the custom command runs and produces the expected result.
/// <reference types="cypress" /> // cypress/support/commands.js Cypress.Commands.add('login', (username, password) => { cy.get('#username').type(username) cy.get('#password').type(password) cy.get('#login-btn').click() }) // cypress/integration/login_spec.js describe('Login with custom command', () => { it('should login successfully using the custom login command', () => { cy.visit('https://example.cypress.io/login') cy.login('user1', 'password123') cy.url().should('include', '/dashboard') cy.get('h1').should('contain.text', 'Dashboard') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Cypress test runner initialized | — | PASS |
| 2 | Browser opens and navigates to 'https://example.cypress.io/login' | Login page with username, password fields and login button is visible | — | PASS |
| 3 | Custom command 'login' is called with username 'user1' and password 'password123' | Username and password fields are filled, login button is clicked | — | PASS |
| 4 | Test waits for navigation to '/dashboard' page | Dashboard page loads with heading 'Dashboard' | URL includes '/dashboard' | PASS |
| 5 | Check that the page heading contains text 'Dashboard' | Heading element visible with correct text | Heading text contains 'Dashboard' | PASS |
| 6 | Test ends successfully | Test passed, no errors | — | PASS |
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