Verify user can select a nested element using chained selectors
Preconditions (3)
✅ Expected Result: The first <li> item inside the container's <ul> is clicked and has the class 'selected' applied
describe('Chaining selectors test', () => { it('should click the first list item inside container and verify selection', () => { cy.visit('https://example.com/sample-page') cy.get('div.container') .find('ul') .find('li.item') .first() .click() .should('have.class', 'selected') }) })
This test opens the sample page URL first using cy.visit(). Then it uses cy.get('div.container') to find the container div. Next, it chains .find('ul') to locate the unordered list inside that container. Then it chains .find('li.item') to get all list items with class 'item' inside the
.first() selects the first such .should('have.class', 'selected'). This chaining ensures we only look inside the container and list, making the selector precise and maintainable.Now add data-driven testing to click the first <li> item inside containers with different classes: 'container', 'container-alt', and 'container-special'. Verify each clicked item has class 'selected'.
cy.get() followed by .find() means:cy.get() usagecy.get() selects a parent or root element on the page..find() usage.find() searches inside the previously selected element for child elements.cy.get('#login')..find() to locate child button.find('button') to get the button inside the form.cy.get('.menu').find('li.active')cy.get('.menu').find('li.active')li elements with class 'active' inside those 'menu' elements.li elements with class 'active' inside elements with class 'menu' -> Option Dcy.find('.container').get('button.submit')cy.find() is not a valid Cypress command; cy.get() should be used first to select elements.cy.get('.container').find('button.submit').find before get is incorrect -> Option Ali has a button inside. Which Cypress code correctly clicks the button inside the third li element within a ul with class 'items'?cy.get('ul.items') selects the ul with class 'items'.li children and pick the third.find('li').eq(2) selects the third li (index starts at 0).li and click.find('button').click() clicks the button inside the selected li.Pass the test and this pattern moves unknown