The test fails with "Cannot read property 'click' of null". What is the likely cause?
medium
A. cy.get() cannot be used after cy.visit()
B. The selector '#submit' is invalid syntax
C. cy.visit() does not open the page
D. The page did not finish loading before cy.get ran
Solution
Step 1: Understand the error message
The error means the element with id 'submit' was not found, so click() failed.
Step 2: Consider timing of commands
If the page is not fully loaded before cy.get(), the element may not exist yet causing failure.
Final Answer:
The page did not finish loading before cy.get ran -> Option D
Quick Check:
Element missing = page not loaded yet [OK]
Hint: Wait for page load before querying elements [OK]
Common Mistakes:
Assuming selector syntax is wrong
Thinking cy.visit() does not open page
Believing cy.get() can't follow cy.visit()
5. You want to visit a page and ensure it loads only if the URL contains 'dashboard'. Which of these Cypress code snippets correctly does this?
hard
A. cy.visit('/dashboard').then(() => cy.url().should('include', 'dashboard'))
B. cy.visit('/dashboard', { onLoad: () => cy.url().should('include', 'dashboard') })
C. cy.visit('/dashboard').url().should('include', 'dashboard')
D. cy.visit('/dashboard').should('contain', 'dashboard')
Solution
Step 1: Understand chaining after cy.visit()
Since cy.visit() returns a chainable, you can use .then() to run assertions after the page loads.
Step 2: Check each option's correctness
cy.visit('/dashboard').url().should('include', 'dashboard') tries to call url() directly on cy.visit(), which is invalid. cy.visit('/dashboard', { onLoad: () => cy.url().should('include', 'dashboard') }) uses a non-existent onLoad option. cy.visit('/dashboard').should('contain', 'dashboard') tries to assert page content with should('contain') on cy.visit(), which is invalid. cy.visit('/dashboard').then(() => cy.url().should('include', 'dashboard')) correctly waits for visit to complete, then asserts URL includes 'dashboard'.
Final Answer:
cy.visit('/dashboard').then(() => cy.url().should('include', 'dashboard')) -> Option A
Quick Check:
Use .then() after cy.visit() for assertions [OK]
Hint: Use .then() after cy.visit() to assert URL [OK]
Common Mistakes:
Using invalid options like onLoad
Calling url() directly on cy.visit()
Trying to assert content on cy.visit()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.