Bird
Raised Fist0
Cypresstesting~20 mins

Page Object Model in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using the Page Object Model in Cypress testing?
easy
A. To write tests without any assertions
B. To avoid using selectors in tests
C. To speed up the browser loading time
D. To organize page interactions into reusable classes

Solution

  1. Step 1: Understand the role of Page Object Model

    The Page Object Model (POM) organizes web page elements and actions into classes to reuse code easily.
  2. Step 2: Identify the main benefit in Cypress tests

    Using POM makes tests easier to write, read, and maintain by grouping page logic separately from test logic.
  3. Final Answer:

    To organize page interactions into reusable classes -> Option D
  4. Quick Check:

    Page Object Model = reusable page classes [OK]
Hint: POM groups page actions into classes for reuse [OK]
Common Mistakes:
  • Thinking POM speeds up browser loading
  • Believing POM removes need for assertions
  • Assuming POM avoids selectors entirely
2. Which of the following is the correct way to define a method inside a Cypress Page Object class to click a button with selector button.submit?
easy
A. clickSubmit() { cy.get('button.submit').click(); }
B. clickSubmit() => { cy.get('button.submit').click(); }
C. function clickSubmit() { cy.get('button.submit').click(); }
D. clickSubmit: function() { cy.get('button.submit').click(); }

Solution

  1. Step 1: Recognize class method syntax in JavaScript

    Inside a class, methods are defined as methodName() { ... } without the function keyword or arrow syntax.
  2. Step 2: Match the correct syntax for Cypress command

    The method should call cy.get('button.submit').click(); inside the method body.
  3. Final Answer:

    clickSubmit() { cy.get('button.submit').click(); } -> Option A
  4. Quick Check:

    Class method syntax = clickSubmit() { cy.get('button.submit').click(); } [OK]
Hint: Class methods use name() { } syntax, no function keyword [OK]
Common Mistakes:
  • Using arrow functions inside class methods incorrectly
  • Adding 'function' keyword inside class method
  • Using object literal syntax inside class
3. Given this Page Object class snippet:
class LoginPage {
  visit() {
    cy.visit('/login')
  }
  enterUsername(name) {
    cy.get('#username').type(name)
  }
  enterPassword(pass) {
    cy.get('#password').type(pass)
  }
  submit() {
    cy.get('button[type=submit]').click()
  }
}

What will happen when the test calls:
const page = new LoginPage(); page.visit(); page.enterUsername('user1'); page.enterPassword('pass1'); page.submit();
medium
A. The login page loads, username and password are typed, and submit button is clicked
B. Only the login page loads, but no typing or clicking happens
C. Syntax error because methods are not async
D. The test fails because selectors are incorrect

Solution

  1. Step 1: Analyze method calls in sequence

    The test creates a LoginPage object and calls methods to visit the page, type username and password, then click submit.
  2. Step 2: Confirm Cypress commands are correct

    Selectors like #username, #password, and button[type=submit] are valid and commands are chained properly.
  3. Final Answer:

    The login page loads, username and password are typed, and submit button is clicked -> Option A
  4. Quick Check:

    Method calls execute Cypress commands in order [OK]
Hint: Methods run Cypress commands sequentially as called [OK]
Common Mistakes:
  • Thinking Cypress commands need async/await
  • Assuming selectors are invalid without checking
  • Believing only visit() runs without chaining
4. You have this Page Object method:
login(username, password) {
  cy.get('#user').type(username)
  cy.get('#pass').type(password)
  cy.get('button.login').click()
}

But your test calls cy.visit('/login') before login(), yet fails because the username input is not found. What is the likely problem?
medium
A. The click() command must come before typing
B. The selector #user is incorrect or outdated
C. The method should use cy.find() instead of cy.get()
D. You forgot to call cy.visit() before login

Solution

  1. Step 1: Identify cause of element not found error

    If the username input is not found, the selector #user might be wrong or changed on the page.
  2. Step 2: Evaluate other options

    While missing cy.visit() can cause failure, the question states the failure is specifically about the username input not found, pointing to selector issue.
  3. Final Answer:

    The selector #user is incorrect or outdated -> Option B
  4. Quick Check:

    Element not found = wrong selector [OK]
Hint: Element not found usually means wrong selector [OK]
Common Mistakes:
  • Assuming cy.find() fixes selector issues
  • Thinking click() must come before typing
  • Ignoring the need to visit page first
5. You want to create a Page Object for a form with multiple input fields and a submit button. Which approach best follows Cypress Page Object Model best practices?
hard
A. Use global variables for selectors and call Cypress commands directly in tests
B. Write all test steps directly inside the test without any class or methods
C. Create a class with methods for each field interaction and a submit method, using clear selectors
D. Create one method that types all fields and clicks submit without separate methods

Solution

  1. Step 1: Understand POM best practices

    Best practice is to create a class that groups page elements and actions into clear, reusable methods with good selectors.
  2. Step 2: Compare options

    Writing all steps in tests or using global variables reduces reusability and clarity. One big method reduces flexibility and readability.
  3. Final Answer:

    Create a class with methods for each field interaction and a submit method, using clear selectors -> Option C
  4. Quick Check:

    Clear methods + selectors = POM best practice [OK]
Hint: Use classes with clear methods for each page action [OK]
Common Mistakes:
  • Putting all steps directly in tests
  • Using global selectors without encapsulation
  • Combining all actions into one big method
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