In this article, we will learn to use writeFile(write to a file) and readFile(read from a file) in cypress. Let’s automate the below scenario:

  1. Write to a text file test1.txt using writeFile
  2. Append content to the end of the text file test1.txt using the flag a+
  3. Write to a JSON file test2.json using writeFile
  4. Validate the content of both text and JSON file using readFile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
describe('Example for writeFile and readFile', function () {

    it('Write to a text file test1.txt using writeFile', function () {
        cy.writeFile('cypress/fixtures/test1.txt', 'Testersdock.com\n')
    })

    it('Append content to the end of the text file test1.txt using the flag a+', function () {
        cy.writeFile('cypress/fixtures/test1.txt', 'Info Hub for Testers', { flag: 'a+' })
    })

    it('Write to a JSON file test2.json using writeFile', function () {
        cy.writeFile('cypress/fixtures/test2.json', { firstname: 'Alapan', lastname: 'Das' })
    })

    it('Validate the content of both text and JSON file using readFile', function () {
        cy.readFile('cypress/fixtures/test1.txt').should('contain', 'Testersdock')
        cy.readFile('cypress/fixtures/test1.txt').should('eq', 'Testersdock.com\nInfo Hub for Testers')
        cy.readFile('cypress/fixtures/test2.json').its('firstname').should('eq', 'Alapan')
    })
   
})

1. cy.writeFile(‘cypress/fixtures/test1.txt’, ‘Testersdock.com\n’) – This will write the text Testersdock.com to the file test1.txt located at cypress/fixtures. The \n denotes new line, meaning the next texts should start from a new line.

2. cy.writeFile(‘cypress/fixtures/test1.txt’, ‘Info Hub for Testers’, { flag: ‘a+’ }) – This will append the text Info Hub for Testers to the end of the file test1.txt. Since we used \n previously so this text will be written in a new line. Whenever you are using writeFile, with every run, writeFile replaces the data, meaning that the previously saved data will be replaced by a new set of data. So if you want to preserve the old saved data, use the flag a+ to append the new data to the file.

3. cy.writeFile(‘cypress/fixtures/test2.json’, { firstname: ‘Alapan’, lastname: ‘Das’ }) – This will write the json content { firstname: ‘Alapan’, lastname: ‘Das’ } to the file test2.json located at cypress/fixtures.

4. cy.readFile(‘cypress/fixtures/test1.txt’).should(‘contain’, ‘Testersdock’) – This will read the content of the file test1.txt located at cypress/fixtures and check that it contains the text Testersdock. It may contain other texts but it should definitely contain Testersdock.

5. cy.readFile(‘cypress/fixtures/test1.txt’).should(‘eq’, ‘Testersdock.com\nInfo Hub for Testers’) – This will read the content of the file test1.txt located at cypress/fixtures and validates that the text file contents exactly is Testersdock.com\nInfo Hub for Testers. It may not contain other texts (or even a space) and has to match exactly.

6. cy.readFile(‘cypress/fixtures/test2.json’).its(‘firstname’).should(‘eq’, ‘Alapan’) – This will read the content of the file test2.json located at cypress/fixtures and check that the firstname key has the value Alapan.

After execution, the test1.txt and test2.json files should look like this –

text file after writeFile

Do check out 🙂

Github: https://github.com/alapanme/Cypress-Automation
All Cypress Articles: https://testersdock.com/cypress-tutorial/