JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Working with Multiple Elements in Cypress

--

https://youtu.be/eMLVTX7AyNk

In this Cypress tutorial, we will cover how you can work with multiple elements. We will verify the length of the list as well as assert the text of all the list items.

Test Scenario

In this test scenario, we will try to get the length of the menu link items below as well as get the text of each of the list items.

.each()

Since we will be working with the list items, we will need to iterate through the list. Cypress provides a handy .each() function to iterate through Array-like structures. Let’s see how we can use it –

Asserting the list text

Once we are able to iterate through the list using the .each() command, we can get access to each individual list item. We can then use the .text() command to access the text of the list items.

In the above code, we are asserting the text using 2 ways –

  • Via the .text() command – this way is useful if you would like to modify the text in some way before asserting it
  • Via the .wrap() and .should() command – this is the natural way to assert the text in Cypress Depending on your own use case you can pick either way to assert the text.

Check out the video below to see learn how to work with multiple elements in Cypress –

Thanks for reading!

More content at plainenglish.io

--

--

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Written by Dilpreet Johal

SDET Architect | YouTuber | Tech Blogger | Love to explore new tools and technologies. Get access to all the courses— https://sdetunicorns.com/

No responses yet

Write a response