Challenge - 5 Problems
File Reading Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of reading a JSON file with Cypress?
Given the following Cypress test code that reads a JSON file and logs a property, what will be printed in the test runner console?
Cypress
cy.readFile('cypress/fixtures/user.json').then((user) => { cy.log(user.name) }) // Assume user.json content: {"name": "Alice", "age": 30}
Attempts:
2 left
💡 Hint
cy.readFile reads the file and parses JSON automatically.
✗ Incorrect
cy.readFile reads the JSON file and returns the parsed object. Accessing user.name gives the string 'Alice'.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies file content in Cypress?
You want to check that the file 'data.txt' contains the exact text 'Hello World'. Which assertion is correct?
Cypress
cy.readFile('data.txt').should( /* assertion here */ )
Attempts:
2 left
💡 Hint
Use the assertion that checks exact equality of strings.
✗ Incorrect
The 'equal' assertion checks that the file content exactly matches 'Hello World'. 'contain' checks substring, others are invalid for strings.
🔧 Debug
advanced2:00remaining
Why does this Cypress test fail to read the file?
Consider this test code:
cy.readFile('nonexistent.json').then((data) => {
cy.log(data.value)
})
Why does this test fail?
Cypress
cy.readFile('nonexistent.json').then((data) => { cy.log(data.value) })
Attempts:
2 left
💡 Hint
Check if the file path is correct and file exists.
✗ Incorrect
cy.readFile throws an error if the file does not exist. The test fails because 'nonexistent.json' is missing.
❓ framework
advanced2:00remaining
How to read a file and assert JSON property asynchronously in Cypress?
Which code snippet correctly reads 'config.json' and asserts that the 'enabled' property is true?
Attempts:
2 left
💡 Hint
Use Cypress built-in assertions for properties.
✗ Incorrect
Option A uses 'should' with property check, which is the Cypress recommended way for assertions on file content.
🧠 Conceptual
expert2:00remaining
What is the best practice for reading large files in Cypress tests?
When reading a very large file in Cypress tests, what is the best approach to avoid slowing down tests or causing memory issues?
Attempts:
2 left
💡 Hint
Think about test speed and memory when handling large data.
✗ Incorrect
Reading large files fully can slow tests and use much memory. Splitting files and reading only needed parts is best practice.