0
0
Cypresstesting~5 mins

cy.readFile() assertions in Cypress - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does cy.readFile() do in Cypress?

cy.readFile() reads the contents of a file from the file system during a test.

It allows you to check file content as part of your test assertions.

Click to reveal answer
beginner
How do you assert that a file contains specific text using cy.readFile()?

Use cy.readFile('path/to/file').should('include', 'expected text').

This checks that the file content includes the given text.

Click to reveal answer
intermediate
How can you assert JSON content from a file using cy.readFile()?

Read the JSON file and assert its properties like this:

cy.readFile('data.json').should((json) => {<br>  expect(json.key).to.equal('value');<br>})
Click to reveal answer
beginner
What happens if cy.readFile() tries to read a non-existent file?

The test will fail with an error saying the file was not found.

This helps catch missing or wrongly named files during testing.

Click to reveal answer
intermediate
Why is it better to use should() assertions with cy.readFile() instead of then()?

should() retries automatically until the assertion passes or times out.

This makes tests more stable if the file content changes asynchronously.

Click to reveal answer
What does cy.readFile('file.txt').should('include', 'hello') check?
AThe file path exists
BThe file contains the text 'hello'
CThe file size is greater than zero
DThe file is empty
How do you assert a JSON file has a key 'name' with value 'Alice' using cy.readFile()?
Acy.readFile('file.json').should((json) =&gt; { expect(json.name).to.equal('Alice') })
Bcy.readFile('file.json').should('include', 'Alice')
Ccy.readFile('file.json').should('have.property', 'name', 'Alice')
Dcy.readFile('file.json').then(json =&gt; json.name === 'Alice')
What happens if cy.readFile() tries to read a missing file?
AThe test passes silently
BThe test retries reading forever
CThe test fails with a file not found error
DThe test skips the file check
Why prefer should() over then() for assertions on cy.readFile()?
A<code>then()</code> cannot access file content
B<code>then()</code> retries the assertion automatically
C<code>should()</code> runs faster than <code>then()</code>
D<code>should()</code> retries the assertion until it passes or times out
Which is a valid way to check that a file contains the exact text 'Hello World'?
Acy.readFile('file.txt').should('equal', 'Hello World')
Bcy.readFile('file.txt').should('include', 'Hello')
Ccy.readFile('file.txt').should('contain', 'World')
Dcy.readFile('file.txt').should('have.length', 11)
Explain how to use cy.readFile() to verify JSON file content in a Cypress test.
Think about how to access JSON keys and check their values.
You got /3 concepts.
    Describe the benefits of using should() assertions with cy.readFile() instead of then().
    Consider what happens if the file content changes after the first read.
    You got /3 concepts.