Test Overview
This test uses Cypress task command to perform a Node.js operation that reads a file's content. It verifies that the content returned by the task matches the expected text.
This test uses Cypress task command to perform a Node.js operation that reads a file's content. It verifies that the content returned by the task matches the expected text.
/// <reference types="cypress" /> // In cypress/plugins/index.js or cypress.config.js module.exports = (on, config) => { on('task', { readFileContent(filename) { const fs = require('fs') return fs.readFileSync(filename, 'utf8') } }) } // In the test file describe('Task command for Node operations', () => { it('reads file content using task command', () => { const testFile = 'cypress/fixtures/sample.txt' cy.task('readFileContent', testFile).then((content) => { expect(content).to.contain('Hello Cypress') }) }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Cypress test runner initialized | - | PASS |
| 2 | Cypress opens browser and loads test page | Browser opened with Cypress test environment | - | PASS |
| 3 | Test calls cy.task('readFileContent', 'cypress/fixtures/sample.txt') | Cypress sends task request to Node process | - | PASS |
| 4 | Node process executes task: reads file content synchronously | File 'sample.txt' content read: 'Hello Cypress! This is a sample file.' | - | PASS |
| 5 | Task returns file content to Cypress test | Cypress receives file content string | - | PASS |
| 6 | Test asserts content includes 'Hello Cypress' | Content string contains expected text | expect(content).to.contain('Hello Cypress') | PASS |
| 7 | Test ends successfully | Test passed, no errors | - | PASS |