But your tests fail with "Cannot read property 'click' of undefined". What is the likely problem?
medium
A. You forgot to call cy.visit() before using the command.
B. The selector 'button.submit' does not match any element on the page.
C. Custom commands cannot use cy.get inside them.
D. The command name 'clickButton' is reserved and cannot be used.
Solution
Step 1: Understand the error message
"Cannot read property 'click' of undefined" means cy.get did not find the element, so it returned undefined.
Step 2: Check possible causes
If the selector does not match any element, cy.get returns nothing, causing the error when calling click.
Final Answer:
The selector 'button.submit' does not match any element on the page. -> Option B
Quick Check:
Selector missing element = click fails [OK]
Hint: Check if selector matches elements before clicking [OK]
Common Mistakes:
Assuming cy.get can't be used in custom commands
Forgetting to visit the page (may cause no elements)
Thinking command names are reserved keywords
5. You want to create a custom command fillForm that fills multiple fields on a page. Which approach best reduces duplication and improves test readability?
hard
A. Create fillForm to accept an object with field names and values, then fill each field inside the command.
B. Write separate commands for each field and call them one by one in tests.
C. Copy and paste the same cy.get().type() lines in every test needing the form filled.
D. Use cy.exec() to run external scripts that fill the form.
Solution
Step 1: Consider duplication and readability
Copy-pasting code (Copy and paste the same cy.get().type() lines in every test needing the form filled.) causes duplication and harder maintenance.
Step 2: Evaluate options for custom commands
Creating one command that accepts an object and fills all fields (Create fillForm to accept an object with field names and values, then fill each field inside the command.) centralizes logic and improves readability.
Step 3: Analyze other options
Separate commands (Write separate commands for each field and call them one by one in tests.) increase calls and complexity; cy.exec (Use cy.exec() to run external scripts that fill the form.) is not for UI actions.
Final Answer:
Create fillForm to accept an object with field names and values, then fill each field inside the command. -> Option A
Quick Check:
One command with params reduces duplication best [OK]
Hint: Use one paramized command for multiple fields [OK]
Common Mistakes:
Writing many small commands instead of one flexible one
Copy-pasting code in tests instead of reusing commands
Using cy.exec for UI interactions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.