Consider the following Cypress test code. What will be the text content of the parentText variable after running?
cy.get('ul#list > li.selected').parent().invoke('text').then(parentText => { cy.log(parentText); });
Remember, parent() moves up one level in the DOM tree.
The parent() command selects the immediate parent element of the matched element(s). Here, it selects the ul#list element. Invoking text on it returns all text inside the
- , including all
- children.
You want to check that the parent of a button with id submitBtn has the class form-group. Which assertion is correct?
cy.get('#submitBtn').parent()
Use the correct assertion to check for a CSS class.
The have.class assertion checks if the element has the specified CSS class. Other options check for text content or attributes, which are incorrect here.
Given this code, the test fails with Timed out retrying error. Why?
cy.get('.child-element').parent('.nonexistent-class').should('exist');
Check if the parent element matches the selector passed to parent().
The parent() command can take a selector to filter the immediate parent. If the parent does not match, the result is empty, causing the should('exist') assertion to fail.
parent() and parents() in Cypress?Choose the best explanation of how parent() and parents() differ in Cypress DOM traversal.
Think about how many levels up each command goes.
parent() moves one level up to the immediate parent. parents() moves up all levels, returning all ancestors up to the document root.
Given a button with class btn-action, you want to click its parent element and then check if that parent is visible. Which command chain is correct?
Remember Cypress commands are asynchronous and chainable; order matters.
Option A first gets the parent, asserts it is visible, then clicks it. This ensures the parent is visible before clicking. Option A clicks before asserting visibility, which may cause flaky tests. Option A clicks the button, then tries to get parent, which is invalid chaining. Option A asserts existence, not visibility.