Bird
Raised Fist0
Cypresstesting~10 mins

Reading file contents in Cypress - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read a file named 'example.txt' using Cypress.

Cypress
cy.[1]('example.txt').then((content) => {
  expect(content).to.exist
})
Drag options to blanks, or click blank then click option'
AreadFile
BwriteFile
Cvisit
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'writeFile' instead of 'readFile'.
Using 'visit' which is for URLs, not files.
2fill in blank
medium

Complete the code to assert that the file content includes the word 'Hello'.

Cypress
cy.readFile('example.txt').then((content) => {
  expect(content).to.[1]('Hello')
})
Drag options to blanks, or click blank then click option'
Aequal
Bhave.length
Cbe.empty
Dinclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'equal' which requires exact match.
Using 'be.empty' which checks for empty content.
3fill in blank
hard

Fix the error in the code to correctly read a JSON file and assert a property value.

Cypress
cy.readFile('data.json').then((data) => {
  expect(data.[1]).to.equal('John')
})
Drag options to blanks, or click blank then click option'
Ausername
BName
Cname
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Name' with uppercase 'N' which does not match the JSON property.
Using unrelated property names like 'username' or 'user'.
4fill in blank
hard

Fill both blanks to read a file and assert its length is greater than 10.

Cypress
cy.readFile('log.txt').then((content) => {
  expect(content.[1]).to.[2](10)
})
Drag options to blanks, or click blank then click option'
Alength
Bbe.greaterThan
Cbe.lessThan
Dlength()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length()' which is not a property but a method (strings use property).
Using 'be.lessThan' which checks for smaller values.
5fill in blank
hard

Fill all three blanks to read a JSON file, filter users older than 18, and assert the count.

Cypress
cy.readFile('users.json').then((users) => {
  const adults = users.filter(user => user.[1] [2] 18)
  expect(adults.[3]).to.equal(3)
})
Drag options to blanks, or click blank then click option'
Aage
B>
Clength
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of 'age' for filtering.
Using '<' instead of '>' operator.
Checking 'length()' instead of 'length' property.

Practice

(1/5)
1. What is the primary purpose of cy.readFile() in Cypress testing?
easy
A. To read the contents of a file during a test
B. To write data to a file during a test
C. To delete a file before running tests
D. To upload a file to the server

Solution

  1. Step 1: Understand the function name

    cy.readFile() suggests reading a file, not writing or deleting.
  2. Step 2: Recall Cypress documentation

    This command reads file contents so tests can verify data inside files.
  3. Final Answer:

    To read the contents of a file during a test -> Option A
  4. Quick Check:

    cy.readFile() reads file contents [OK]
Hint: Remember: readFile reads, writeFile writes [OK]
Common Mistakes:
  • Confusing readFile with writeFile
  • Thinking it uploads files
  • Assuming it deletes files
2. Which of the following is the correct syntax to read a JSON file named data.json using Cypress?
easy
A. cy.readFile('data.json', 'utf8')
B. cy.readFile('data.json').then((content) => { /* use content */ })
C. cy.readFile('data.json').catch((content) => { /* use content */ })
D. cy.readFile('data.json').write((content) => { /* use content */ })

Solution

  1. Step 1: Identify correct method chaining

    Using then() after cy.readFile() is correct to access file content.
  2. Step 2: Check other options

    catch() is for errors, not normal reading; write() is invalid here; passing 'utf8' alone is incomplete.
  3. Final Answer:

    cy.readFile('data.json').then((content) => { /* use content */ }) -> Option B
  4. Quick Check:

    Use then() to handle readFile content [OK]
Hint: Use then() to access file content after readFile [OK]
Common Mistakes:
  • Using catch() instead of then()
  • Trying to write after readFile
  • Passing encoding without then()
3. Given the code below, what will be logged to the console if example.txt contains the text "Hello Cypress"?
cy.readFile('example.txt').then((text) => {
  console.log(text);
});
medium
A. Empty string
B. undefined
C. An error because readFile needs JSON
D. "Hello Cypress"

Solution

  1. Step 1: Understand readFile behavior

    cy.readFile() reads file content as text by default if file is plain text.
  2. Step 2: Check the file content

    The file contains "Hello Cypress", so the callback receives this string and logs it.
  3. Final Answer:

    "Hello Cypress" -> Option D
  4. Quick Check:

    readFile logs file text content [OK]
Hint: readFile returns file content as string for text files [OK]
Common Mistakes:
  • Assuming readFile only works with JSON
  • Expecting undefined instead of content
  • Thinking it throws error for text files
4. Identify the error in the following Cypress code snippet that tries to read a file and assert its content:
cy.readFile('config.json').then((data) => {
  expect(data).to.equal('config');
});
medium
A. Incorrect file path syntax
B. Missing catch() to handle errors
C. Using to.equal() instead of to.deep.equal() for object comparison
D. readFile cannot be used with JSON files

Solution

  1. Step 1: Understand data type returned by readFile

    Reading a JSON file returns an object, not a string.
  2. Step 2: Check assertion method

    to.equal() checks strict equality, which fails for objects; to.deep.equal() is needed for object content comparison.
  3. Final Answer:

    Using to.equal() instead of to.deep.equal() for object comparison -> Option C
  4. Quick Check:

    Use deep.equal for object assertions [OK]
Hint: Use deep.equal for objects, equal for primitives [OK]
Common Mistakes:
  • Using to.equal() for objects
  • Ignoring error handling
  • Assuming readFile can't read JSON
5. You want to verify that a text file notes.txt contains the word "important" anywhere in its content using Cypress. Which code snippet correctly performs this check?
hard
A. cy.readFile('notes.txt').then((text) => { expect(text.includes('important')).to.be.true; })
B. cy.readFile('notes.txt').should('contain', 'important')
C. cy.readFile('notes.txt').then((text) => { expect(text).to.equal('important'); })
D. cy.readFile('notes.txt').then((text) => { expect(text).to.have.property('important'); })

Solution

  1. Step 1: Understand file content check

    We want to check if the word "important" is anywhere in the text, so we need to check substring presence.
  2. Step 2: Evaluate options

    cy.readFile('notes.txt').then((text) => { expect(text.includes('important')).to.be.true; }) uses includes() and asserts true, which is correct. cy.readFile('notes.txt').should('contain', 'important') is invalid because should('contain') is for DOM elements, not file content. cy.readFile('notes.txt').then((text) => { expect(text).to.equal('important'); }) checks exact equality, which is wrong. cy.readFile('notes.txt').then((text) => { expect(text).to.have.property('important'); }) wrongly uses to.have.property() on a string.
  3. Final Answer:

    cy.readFile('notes.txt').then((text) => { expect(text.includes('important')).to.be.true; }) -> Option A
  4. Quick Check:

    Use includes() to check substring in file text [OK]
Hint: Use includes() inside then() to check text content [OK]
Common Mistakes:
  • Using should('contain') on file content
  • Checking exact equality instead of substring
  • Using property assertion on string
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard