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:
- Write to a text file test1.txt using writeFile
- Append content to the end of the text file test1.txt using the flag a+
- Write to a JSON file test2.json using writeFile
- 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 –
Do check out 🙂
Github:Â https://github.com/alapanme/Cypress-Automation
All Cypress Articles: https://testersdock.com/cypress-tutorial/
Awesome work Alapan!! Can you help me find how to read a text from the page and write to the file? for example, I wanted the above header to be written in a file. Expected is “LEAVE A REPLY” in a text file but it’s not happening:
cy.get(‘#reply-title > span’)
.then((text) => {cy.writeFile(‘integration/screenshots/Dummy.txt’, text.body)
})
It should be:
cy.writeFile(‘cypress/integration/screenshots/Dummy.txt’, text)
Hi Alpan
I have to read firstname from json file and store it in constant string.How can i store it
beforeEach block:
beforeEach(function () {
cy.fixture(‘testdata.json’).then(function (testdata) {
this.testdata = testdata
}) })
In your test use it as:
this.testdata.username
I want to read a text file data and then at the end of each line I want to append a text .
Hi Alapan, I am just learning to use Cypress and haven’t learned enough JavaScript yet, so maybe my question is silly. I need to read a short TXT file and assign its content to a string variable to use in the same script. How do I assign the value? Thank you.
You can do it in two ways:
1. using an alias .as –
cy.readFile(‘cypress/fixtures/test1.txt’).as(‘text’)
cy.get(‘@text’).then((text) => {
cy.log(text) //text contains the contains the string from txt file
})
2. Directly using .then() –
cy.readFile(‘cypress/fixtures/test1.txt’).then((text) => {
cy.log(text) //text contains the contains the string from txt file
})
Hi,
Im unable to append json data to file each time ..could you please help me