Skip to main content

Cypress first Command

Within a set of DOM elements, the first command fetches the first DOM element from that set. This behavior of the first command in cypress is that of the exact behavior of the jquery first command.



Options we can include in the command can be:

  • Log: To display the command in the command log.
  • Timeout: Wait for some particular thing to get finished before timing out.

Correct Use


This gets the first element of the li tag.

Incorrect Use


It cannot be chained up with cy directly.


Let us see a sample example of how this works:

  <li class="uno">Hola1</li>
  <li class="tres">Hola2</li>
  <li class="des">Hola3</li>
  <li class="tez">Hola4</li>

This command for the above DOM elements yields <li class="uno">Hola1</li> because this element is in the first place.

Now let us see a real-time example of automating our traditional We will be using the parents and children command to use the first command.

If you want to know about parents and children command dive here

So let us get back to our first command.

Here are the test steps of what we are about to do. This will be similar to the parents article test.

  1. Visit the site.
  2. Click on ask a doubt.
  3. Select the parents with the ask doubt menu bar scope and get their children.
  4. Out of those children, click the first element, selecting the home tab.
  5. Perform click, which should take us back to the home page.

Let us dive in.

describe('Should load the programsbuzz website',()=> {
it('Track down parents of askdoubt and try to click home',()=>{
        cy.contains('Ask Doubt').click()
  • As we can see, we have navigated to the site and clicked the ask doubt tab.
  • We have selected its parent out of the tab, which is gva_menu_main.
  • This gva_menu_main yields children on which we will perform the first command.

Here we can see the children of the following element yield when performing the first command. will be selected.

So when clicked upon, this will perform the click on the first element out of the children yielded elements and take us back to the home page.

We can see that it has selected the first element and clicked on it.

Submitted by arilio666 on January 11, 2022

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.


At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.