cypress/ ├── e2e/ # Test files go here │ └── example.cy.js # Your first test file ├── fixtures/ # Test data files (JSON, etc.) ├── support/ # Custom commands and reusable code │ ├── commands.js # Custom Cypress commands │ └── e2e.js # Support file loaded before tests cypress.config.js # Cypress configuration file package.json # Project dependencies and scripts
First Cypress test - Framework Patterns
- Test Layer (cypress/e2e): Contains test scripts that describe user actions and assertions.
- Support Layer (cypress/support): Holds reusable commands and setup code to keep tests clean.
- Fixtures Layer (cypress/fixtures): Stores static test data like JSON files for data-driven tests.
- Configuration (cypress.config.js): Defines environment settings, base URLs, and test options.
Use cypress.config.js to set base URL, test retries, and browser options.
Manage environments by defining env variables inside the config or via CLI.
Store sensitive data like credentials in environment variables or cypress.env.json (excluded from version control).
// cypress.config.js example
import { defineConfig } from 'cypress'
export default defineConfig({
e2e: {
baseUrl: 'https://example.com',
supportFile: 'cypress/support/e2e.js',
env: {
username: 'testuser',
password: 'secret'
},
retries: 1
}
})
Cypress generates detailed test reports in the terminal and browser.
Use plugins like mochawesome for HTML reports.
Integrate Cypress tests in CI/CD pipelines (GitHub Actions, Jenkins) by running npx cypress run.
Fail builds automatically if tests fail, ensuring quality before deployment.
- Keep tests independent and repeatable; avoid relying on previous test state.
- Use
cypress/support/commands.jsto create reusable custom commands. - Use fixtures for test data to separate data from test logic.
- Use explicit assertions to verify expected outcomes clearly.
- Run tests in headless mode in CI for faster feedback.
Where in this folder structure would you add a new test file for a login feature?
Answer: Add the new test file inside the cypress/e2e/ folder, for example cypress/e2e/login.cy.js.
Practice
cy.visit() command in a Cypress test?Solution
Step 1: Understand
Thecy.visit()usagecy.visit()command is used to open a webpage URL in the Cypress test browser.Step 2: Compare with other options
Options A, B, and C describe other actions unrelated to opening a webpage.Final Answer:
To open a webpage in the test browser -> Option CQuick Check:
cy.visit()opens page [OK]
- Confusing
cy.visit()with assertions - Thinking it runs tests instead of opening pages
- Mixing it with test description blocks
Solution
Step 1: Identify correct Cypress test structure
Cypress tests usedescribeblocks to group tests anditblocks for individual tests, both with arrow functions.Step 2: Check syntax correctness
describe('Test name', () => { it('does something', () => { }) }) correctly showsdescribewith anitinside using arrow functions. Others use invalid or unsupported syntax.Final Answer:
describe('Test name', () => { it('does something', () => { }) }) -> Option BQuick Check:
Use describe and it blocks [OK]
- Using non-Cypress functions like test() or run()
- Missing arrow functions or braces
- Incorrect nesting of blocks
#welcome is visible?describe('Visibility test', () => {
it('checks welcome message', () => {
cy.visit('https://example.com')
cy.get('#welcome').should('be.visible')
})
})Solution
Step 1: Analyze test steps
The test visits the page, then checks if the element with id#welcomeis visible usingshould('be.visible').Step 2: Understand assertion behavior
If the element is visible, the assertion passes and so does the test. The code syntax is correct andcy.visit()is present.Final Answer:
Test passes because the element is visible -> Option DQuick Check:
Visible element means test passes [OK]
- Assuming test fails without
cy.visit() - Thinking
shouldis wrong syntax - Ignoring element visibility status
describe('My Test', () => {
it('opens page', () => {
cy.visit('https://example.com')
cy.get('.button').should('visible')
})
})Solution
Step 1: Check assertion syntax
The assertionshould('visible')is incorrect. The correct form isshould('be.visible')to check visibility.Step 2: Verify other parts
The selector.buttonis correct for class, anditblock is present. So only the assertion keyword is wrong.Final Answer:
Incorrect assertion keyword, should be should('be.visible') -> Option AQuick Check:
Use should('be.visible') for visibility [OK]
- Using 'visible' instead of 'be.visible'
- Confusing selector syntax for classes
- Omitting it block inside describe
https://example.com, clicks a button with class .submit-btn, and then checks if a success message with id #success-msg is visible. Which of the following test codes correctly implements this?Solution
Step 1: Verify selectors and commands
describe('Submit test', () => { it('clicks and checks success', () => { cy.visit('https://example.com') cy.get('.submit-btn').click() cy.get('#success-msg').should('be.visible') }) }) uses correct selectors:.submit-btnfor class and#success-msgfor id. It usescy.get().click()correctly.Step 2: Check assertion syntax
The code usesshould('be.visible'), which is the correct assertion for visibility. Other options have wrong selectors or incorrect assertion keywords.Final Answer:
describe('Submit test', () => { it('clicks and checks success', () => { cy.visit('https://example.com') cy.get('.submit-btn').click() cy.get('#success-msg').should('be.visible') }) }) -> Option AQuick Check:
Correct selectors + click + should('be.visible') [OK]
- Mixing class and id selectors
- Using cy.click() instead of cy.get().click()
- Wrong assertion keyword 'visible' instead of 'be.visible'
Ready to prove it?
Pass the test and this pattern moves unknown
