Given the following Page Object and test code, what will be the test result?
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') }) })
Check if the selectors match typical HTML input IDs and if the assertion syntax is correct.
The Page Object methods use valid selectors and Cypress commands. The test visits the login page, fills username and password, clicks submit, and asserts the URL includes '/dashboard'. This matches expected behavior, so the test passes.
Choose the best locator strategy for selecting a login button in a Page Object class.
Consider maintainability and stability of selectors when the UI changes.
Using a custom data attribute like data-cy is best practice because it is stable and does not depend on styling or text changes. IDs can change, text can vary, and nth-child is fragile.
Given a Page Object method that returns the success message element, which assertion is correct?
class DashboardPage { getSuccessMessage() { return cy.get('.success-msg') } } const dashboard = new DashboardPage() dashboard.getSuccessMessage()
Check Cypress assertion syntax for partial text matching.
contain.text checks if the element's text includes the given substring. have.text requires exact match. include.text is invalid syntax. contain works but is less specific than contain.text.
Analyze the code and identify the cause of the failure.
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') }) })
Check if the element targeted by the selector is present on the page.
If the selector '.btn-save' does not match any element, Cypress will retry until timeout. The other options have correct syntax: click() is called properly, 'be.visible' is quoted, and cy.contains() is valid.
You want to reuse login steps across many tests using Page Object Model. Which approach is best?
Think about maintainability and reusability with Page Object Model and Cypress hooks.
Using a LoginPage class with a login() method encapsulates login steps. Calling it in beforeEach() ensures login runs before each test, promoting reuse and clarity. Writing login steps in each test duplicates code. Cypress commands are useful but separate from Page Object. Running a separate login test does not share state.