microsoft / playwright-dotnet

.NET version of the Playwright testing and automation library.
https://playwright.dev/dotnet/
MIT License
2.47k stars 235 forks source link

[Feature]: Locator Collection Return Type #1689

Closed Xen0byte closed 2 years ago

Xen0byte commented 3 years ago

Feature request

I really like the concept of locators, and I can see that in the documentation now it says that they should be preferred over element handles, however, unless I'm missing something, it would appear to me that the two are not quite on functional parity just yet.

An example use case could be clicking all the buttons on a page that satisfy a selector. This is a trivial example, and a workaround would be to use the ElementHandlesAsync() method, but doesn't that defy the purpose of locators?

In more complex cases, the workaround above cannot be employed effectively or at all. Let's assume I have something like an HTML table, and I want to do something for each cell in every row. With element handles, I can do something along the lines of...

foreach (IElementHandle row in await Page.QuerySelectorAllAsync(rowSelector))
    foreach (IElementHandle cell in row.QuerySelectorAllAsync(cellSelector))
        await cell.DoSomethingAsync();

...however if I try to do something similar by using locators instead it will just not work.

My suggestion is to extend the locator API so that it supports a collection return type which could use some of the already-existing logic, e.g. the collection elements could be locator nth=1, locator nth=2, locator nth=3, etc.

Lastly, I'm not excluding the possibility that I may have not fully understood the purpose and/or usage of locators, so if that is the case then please let me know.

mxschmitt commented 2 years ago

There are currently no plans for collections, but in the next version we have more detailed docs about how to work with lists and iterate on locators: https://playwright.dev/dotnet/docs/next/locators/#lists