0
0
Cypresstesting~20 mins

Page Object Model in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Page Object Model Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Cypress test using Page Object Model?

Given the following Page Object and test code, what will be the test result?

Cypress
class LoginPage {
  visit() {
    cy.visit('/login')
  }
  fillUsername(name) {
    cy.get('#username').type(name)
  }
  fillPassword(pass) {
    cy.get('#password').type(pass)
  }
  submit() {
    cy.get('button[type=submit]').click()
  }
}

describe('Login Test', () => {
  it('logs in with valid credentials', () => {
    const login = new LoginPage()
    login.visit()
    login.fillUsername('user1')
    login.fillPassword('pass1')
    login.submit()
    cy.url().should('include', '/dashboard')
  })
})
ATest fails because cy.url() assertion is missing parentheses and throws a syntax error.
BTest fails because the selector '#username' is invalid and causes a timeout error.
CTest fails because the submit button selector is incorrect and click() does nothing.
DTest passes because the Page Object methods correctly interact with the page elements and URL assertion matches after login.
Attempts:
2 left
💡 Hint

Check if the selectors match typical HTML input IDs and if the assertion syntax is correct.

locator
intermediate
1:30remaining
Which locator is best practice for Page Object Model in Cypress?

Choose the best locator strategy for selecting a login button in a Page Object class.

Acy.get('button#loginBtn')
Bcy.get('button').contains('Login')
Ccy.get('[data-cy=login-button]')
Dcy.get('button:nth-child(3)')
Attempts:
2 left
💡 Hint

Consider maintainability and stability of selectors when the UI changes.

assertion
advanced
2:00remaining
Which assertion correctly verifies a success message after login?

Given a Page Object method that returns the success message element, which assertion is correct?

Cypress
class DashboardPage {
  getSuccessMessage() {
    return cy.get('.success-msg')
  }
}

const dashboard = new DashboardPage()
dashboard.getSuccessMessage()
Adashboard.getSuccessMessage().should('have.text', 'Login successful')
Bdashboard.getSuccessMessage().should('contain.text', 'Login successful')
Cdashboard.getSuccessMessage().should('include.text', 'Login successful')
Ddashboard.getSuccessMessage().should('contain', 'Login successful')
Attempts:
2 left
💡 Hint

Check Cypress assertion syntax for partial text matching.

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

Analyze the code and identify the cause of the failure.

Cypress
class ProfilePage {
  getSaveButton() {
    return cy.get('.btn-save')
  }
}

describe('Profile update', () => {
  it('saves profile changes', () => {
    const profile = new ProfilePage()
    profile.getSaveButton().click()
    cy.contains('Profile saved').should('be.visible')
  })
})
AThe selector '.btn-save' does not exist on the page, causing cy.get() to time out.
BThe click() command is missing parentheses, so the button is not clicked.
CThe cy.contains() command is used incorrectly and should be cy.get().contains().
DThe assertion 'should(be.visible)' is missing quotes around 'be.visible'.
Attempts:
2 left
💡 Hint

Check if the element targeted by the selector is present on the page.

framework
expert
2:30remaining
How to implement reusable login with Page Object Model in Cypress for multiple tests?

You want to reuse login steps across many tests using Page Object Model. Which approach is best?

ACreate a LoginPage class with login() method and call it inside a beforeEach() hook in test files.
BWrite login steps directly inside each test without Page Object to avoid abstraction overhead.
CUse Cypress commands (Cypress.Commands.add) to define login globally and call it in tests without Page Object.
DCreate a separate login.spec.js test and run it before other tests to ensure login is done.
Attempts:
2 left
💡 Hint

Think about maintainability and reusability with Page Object Model and Cypress hooks.