Using the Locator.All() method added in v1.29
In the video, the host named Jared from a software company Commit Quality presents a short tutorial on the 'All' method, a new feature added to the locator object in the Playwright framework, specifically added as part of version 1.29.
The 'All' method comes into play when our locator points to a list of elements, and it returns an array of the located elements instead of a single element. This method significantly simplifies handling multiple locator values and efforts into looping over elements.
Jared takes an example of a 'Computer Database' website, the mission being to grab all computer names listed in the database using 'tbody', 'td' and anchor tags and consequently clicking into each name listed.
Being part of the old method, he initially demonstrates using a for-loop iterating over the count of locator elements. He then clicks on each element and navigates back to the original page. Jared points out the fact that the code is not neat and has to use the (nth) method redundantly as it iterates through the loop.
As a more efficient alternative, he introduces the new 'All' method, which simplifies the code significantly. Here, instead of using a standard for loop, he uses a 'for of' loop, which helps to iterate over the array of locators directly, sourced from iterable objects yielded by the 'All' method, hence significantly tidier and cleaner.
To summarize, the 'All' method is a powerful new addition in Playwright version 1.29 that makes dealing with multiple element locators more straightforward and the scripting code cleaner. As always, he encourages viewers to like, subscribe, and ask any questions they may have in comments.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email firstname.lastname@example.org.