In this video, we'll explore the latest update to Playwright v1.33 that introduces the new Locator.or() method. With this feature, you can easily create a locator that matches either of two locators, which is incredibly useful for conditional dialogs and external A/B testing scenarios where you have no control over which version you'll encounter. Stay tuned to learn more about how this update can improve your automation workflow.
In this video, Jared from Commit Quality demonstrates the new locator .or()
method introduced in Playwright version 1.33. This new method is particularly useful when dealing with A/B tests or other scenarios where elements on the page might change.
Jared walks us through an example where a filter functionality could either be a text box or a dropdown, depending on which version of the A/B test the user is experiencing. Previously with Playwright, separate tests or complex if conditions would be required to check for both elements.
However, now with the new locator .or()
method, it becomes easier to handle such situations. In the example provided, the test will pass as long as either the text box or the dropdown is visible. This ensures that the test remains robust and reliable, regardless of which element appears on the page.
To demonstrate the functionality, Jared writes a test that will:
Jared also points out that it is essential to be cautious when using this method, as it could lead to false positives if not used correctly.
Finally, Jared showcases an example from the Playwright documentation, demonstrating another use case with a security settings dialog and a new email button. The test ensures either of the elements is visible, then acts accordingly based on which element is presented.
In conclusion, the new locator .or()
method in Playwright 1.33 offers a simpler and more robust solution for situations where page elements may change due to A/B tests or other scenarios, making test writing more efficient and reliable.
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 [email protected].