I have a div element that has some text that I can locate like this -
1. page.getByTestId("parent").getByTestId("list").getByTestId("list-content").getByTestId("leaf-item").nth(1).getByTestId("leaf-item-name");
I can locate the same div in below ways as well -
2. page.getByTestId("list").getByTestId("list-content").getByTestId("leaf-item").nth(1).getByTestId("leaf-item-name");
3. page.getByTestId("list-content").getByTestId("leaf-item").nth(1).getByTestId("leaf-item-name");
4. page.getByTestId("leaf-item").nth(1).getByTestId("leaf-item-name");
5. page.getByTestId("leaf-item-name").first();
What I want to know : What is the best way to locate this element.
My line of thought : Even though 1 seems to be long locator but it gives us the sense of where that element is located in the hierarchy and also it will ensure an expected match. For example if there is another div with data-testid="leaf-item-name"
in the page, it will not interfere with this locator because it is a very specific drilled down locator and not a loose locator like 5.
Please share your feedback and line of thought keeping in mind these tests are being written for a large application.