Bird
Raised Fist0
Cypresstesting~20 mins

First Cypress test - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cypress Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the test result of this Cypress code?

Consider this Cypress test code. What will be the test execution result?

Cypress
describe('My First Test', () => {
  it('Visits the Kitchen Sink', () => {
    cy.visit('https://example.cypress.io')
    cy.contains('type').click()
    cy.url().should('include', '/commands/actions')
  })
})
ATest fails because cy.visit URL is incorrect
BTest passes because the page contains 'type' and URL changes correctly
CTest fails due to syntax error in cy.contains line
DTest fails because cy.url assertion is wrong
Attempts:
2 left
💡 Hint

Check if the URL visited is valid and if the element with text 'type' exists on the page.

❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies the page title in Cypress?

You want to check that the page title is exactly 'Cypress.io: Kitchen Sink'. Which assertion is correct?

Acy.title().should('eq', 'Cypress.io: Kitchen Sink')
Bcy.title().should('equal', 'Cypress.io: Kitchen Sink')
Ccy.title().should('contain', 'cypress.io')
Dcy.title().should('include', 'Kitchen Sink')
Attempts:
2 left
💡 Hint

Check the exact match assertion syntax in Cypress.

❓ locator
advanced
2:00remaining
Which locator is best practice to select a button with text 'Submit' in Cypress?

Choose the best locator to select a button with visible text 'Submit' for a Cypress test.

Acy.contains('button', 'Submit')
Bcy.get('button').contains('Submit')
Ccy.get('button:contains("Submit")')
Dcy.get('button').filter(':text("Submit")')
Attempts:
2 left
💡 Hint

Consider the Cypress recommended way to find elements by text and tag.

🔧 Debug
advanced
2:00remaining
Why does this Cypress test fail with 'Timed out retrying' error?

Given this test code, why does it fail with a timeout error?

Cypress
describe('Timeout Test', () => {
  it('Waits for element', () => {
    cy.visit('https://example.cypress.io')
    cy.get('#nonexistent-element').should('be.visible')
  })
})
AThe assertion 'should be visible' is invalid syntax
Bcy.get selector syntax is incorrect causing failure
Ccy.visit URL is unreachable causing timeout
DThe element with id 'nonexistent-element' does not exist on the page, causing timeout
Attempts:
2 left
💡 Hint

Check if the element selector matches any element on the page.

❓ framework
expert
1:30remaining
What is the correct way to run setup code before all tests in a Cypress test suite?

You want to run some code once before all tests in a describe block. Which is the correct Cypress hook to use?

AbeforeEach(() => { /* setup code */ })
Binit(() => { /* setup code */ })
Cbefore(() => { /* setup code */ })
Dsetup(() => { /* setup code */ })
Attempts:
2 left
💡 Hint

Recall the difference between hooks that run once and those that run before each test.

Practice

(1/5)
1. What is the main purpose of the cy.visit() command in a Cypress test?
easy
A. To run the test suite
B. To check if an element is visible
C. To open a webpage in the test browser
D. To write test descriptions

Solution

  1. Step 1: Understand cy.visit() usage

    The cy.visit() command is used to open a webpage URL in the Cypress test browser.
  2. Step 2: Compare with other options

    Options A, B, and C describe other actions unrelated to opening a webpage.
  3. Final Answer:

    To open a webpage in the test browser -> Option C
  4. Quick Check:

    cy.visit() opens page [OK]
Hint: Remember: visit means open page URL [OK]
Common Mistakes:
  • Confusing cy.visit() with assertions
  • Thinking it runs tests instead of opening pages
  • Mixing it with test description blocks
2. Which of the following is the correct syntax to start a Cypress test block?
easy
A. function test() { describe('Test name') }
B. describe('Test name', () => { it('does something', () => { }) })
C. test('Test name', () => { })
D. run('Test name', () => { })

Solution

  1. Step 1: Identify correct Cypress test structure

    Cypress tests use describe blocks to group tests and it blocks for individual tests, both with arrow functions.
  2. Step 2: Check syntax correctness

    describe('Test name', () => { it('does something', () => { }) }) correctly shows describe with an it inside using arrow functions. Others use invalid or unsupported syntax.
  3. Final Answer:

    describe('Test name', () => { it('does something', () => { }) }) -> Option B
  4. Quick Check:

    Use describe and it blocks [OK]
Hint: Tests start with describe and it blocks [OK]
Common Mistakes:
  • Using non-Cypress functions like test() or run()
  • Missing arrow functions or braces
  • Incorrect nesting of blocks
3. Given the following Cypress test code, what will be the test result if the element with id #welcome is visible?
describe('Visibility test', () => {
  it('checks welcome message', () => {
    cy.visit('https://example.com')
    cy.get('#welcome').should('be.visible')
  })
})
medium
A. Test fails because cy.visit() is missing
B. Test passes even if the element is not visible
C. Test fails because should is used incorrectly
D. Test passes because the element is visible

Solution

  1. Step 1: Analyze test steps

    The test visits the page, then checks if the element with id #welcome is visible using should('be.visible').
  2. Step 2: Understand assertion behavior

    If the element is visible, the assertion passes and so does the test. The code syntax is correct and cy.visit() is present.
  3. Final Answer:

    Test passes because the element is visible -> Option D
  4. Quick Check:

    Visible element means test passes [OK]
Hint: Visible element + should('be.visible') = pass [OK]
Common Mistakes:
  • Assuming test fails without cy.visit()
  • Thinking should is wrong syntax
  • Ignoring element visibility status
4. Identify the error in this Cypress test code:
describe('My Test', () => {
  it('opens page', () => {
    cy.visit('https://example.com')
    cy.get('.button').should('visible')
  })
})
medium
A. Incorrect assertion keyword, should be should('be.visible')
B. Missing parentheses in should('visible')
C. Wrong selector syntax for class
D. Missing it block inside describe

Solution

  1. Step 1: Check assertion syntax

    The assertion should('visible') is incorrect. The correct form is should('be.visible') to check visibility.
  2. Step 2: Verify other parts

    The selector .button is correct for class, and it block is present. So only the assertion keyword is wrong.
  3. Final Answer:

    Incorrect assertion keyword, should be should('be.visible') -> Option A
  4. Quick Check:

    Use should('be.visible') for visibility [OK]
Hint: Use exact assertion keywords like 'be.visible' [OK]
Common Mistakes:
  • Using 'visible' instead of 'be.visible'
  • Confusing selector syntax for classes
  • Omitting it block inside describe
5. You want to write a Cypress test that visits 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?
hard
A. 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') }) })
B. describe('Submit test', () => { it('clicks and checks success', () => { cy.visit('https://example.com') cy.get('#submit-btn').click() cy.get('.success-msg').should('visible') }) })
C. describe('Submit test', () => { it('clicks and checks success', () => { cy.visit('https://example.com') cy.click('.submit-btn') cy.get('#success-msg').should('be.visible') }) })
D. describe('Submit test', () => { it('clicks and checks success', () => { cy.visit('https://example.com') cy.get('.submit-btn').click() cy.get('#success-msg').should('visible') }) })

Solution

  1. 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-btn for class and #success-msg for id. It uses cy.get().click() correctly.
  2. Step 2: Check assertion syntax

    The code uses should('be.visible'), which is the correct assertion for visibility. Other options have wrong selectors or incorrect assertion keywords.
  3. 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 A
  4. Quick Check:

    Correct selectors + click + should('be.visible') [OK]
Hint: Use correct selectors and should('be.visible') for checks [OK]
Common Mistakes:
  • Mixing class and id selectors
  • Using cy.click() instead of cy.get().click()
  • Wrong assertion keyword 'visible' instead of 'be.visible'
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