JSON fixture files in Cypress - Build an Automation Script
describe('Login Test with JSON Fixture', () => { beforeEach(() => { cy.visit('https://example.com/login'); }); it('logs in using credentials from fixture', () => { cy.fixture('user.json').then((user) => { cy.get('#username').type(user.username); cy.get('#password').type(user.password); cy.get('#loginBtn').click(); cy.url().should('eq', 'https://example.com/dashboard'); cy.get('#welcomeMsg').should('contain.text', user.username); }); }); });
This test uses Cypress to automate login using data from a JSON fixture file named user.json. The beforeEach hook opens the login page before each test. Inside the test, cy.fixture('user.json') loads the JSON data asynchronously. Then, it types the username and password into the input fields identified by their IDs. After clicking the login button, the test asserts that the URL is the dashboard URL and that the welcome message contains the username from the fixture. This approach keeps test data separate from test code, making tests easier to maintain and more secure.
Now add data-driven testing with 3 different user credentials stored in the JSON fixture file.
Practice
JSON fixture files in Cypress tests?Solution
Step 1: Understand fixture file role
Fixture files hold static data like user info or responses to use in tests.Step 2: Identify purpose in Cypress
They separate test data from code, making tests cleaner and reusable.Final Answer:
To store test data separately from test code for easy reuse -> Option AQuick Check:
Fixture files = test data storage [OK]
- Thinking fixtures contain test logic
- Confusing fixtures with config files
- Using fixtures to write assertions
user.json in a Cypress test?Solution
Step 1: Recall Cypress fixture command
Cypress usescy.fixture()to load fixture files from the fixtures folder.Step 2: Check method names
Onlycy.fixture()is the correct Cypress command; others do not exist or are incorrect.Final Answer:
cy.fixture('user.json').then((data) => { /* use data */ }) -> Option CQuick Check:
Use cy.fixture() to load JSON files [OK]
- Using non-existent commands like cy.loadFixture
- Confusing cy.readFile with cy.fixture
- Omitting the .then() to access data
user.json with content:{"name": "Alice", "age": 30}What will the following Cypress code print to the console?
cy.fixture('user.json').then((user) => {
console.log(user.name);
console.log(user.age + 5);
})Solution
Step 1: Understand fixture data structure
The JSON has keys "name" with value "Alice" and "age" with value 30 (number).Step 2: Analyze console.log statements
First logs user.name which is "Alice". Second logs user.age + 5, which is 30 + 5 = 35.Final Answer:
"Alice" and 35 -> Option BQuick Check:
Access JSON keys correctly = "Alice" and 35 [OK]
- Treating numbers as strings and concatenating
- Using wrong keys or misspelling
- Expecting undefined from valid fixture keys
cy.fixture('config.json').then((config) => {
expect(config.timeout).to.equal(5000)
})But the test fails with
TypeError: Cannot read property 'timeout' of undefined. What is the most likely cause?Solution
Step 1: Analyze error message
The error saysconfigis undefined, soconfig.timeoutfails.Step 2: Check fixture loading causes
If the fixture file is missing or misnamed,cy.fixture()returns undefined, causing this error.Final Answer:
The fixture fileconfig.jsondoes not exist or is misnamed -> Option DQuick Check:
Missing fixture file = undefined data [OK]
- Assuming expect syntax causes undefined error
- Ignoring fixture file path or name typos
- Not validating JSON file existence
users.json containing an array of user objects:[{"id":1,"name":"Bob"},{"id":2,"name":"Carol"},{"id":3,"name":"Dave"}]How can you write a Cypress test to check that the user with
id 2 has the name "Carol"?Solution
Step 1: Understand fixture data structure
The fixture is an array of objects, each withidandname.Step 2: Find user with id 2 and check name
Usefind()to get the user object whereid === 2, then check itsname.Final Answer:
cy.fixture('users.json').then(users => { expect(users.find(u => u.id === 2).name).to.equal('Carol') }) -> Option AQuick Check:
Use find() to locate object by id [OK]
- Using wrong array index instead of find()
- Comparing id directly to name
- Accessing properties on the whole array
Ready to prove it?
Pass the test and this pattern moves unknown
