DEV Community

Surfing Devs
Surfing Devs

Posted on

4

AI locators for Playwright πŸš€

Today I'm excited to present ai-locators for Playwright! πŸš€

// πŸ‘Ž Say goodbye to this complexity
page.locator("//div[contains(@class, 'header')]//button[contains(@class, 'login') and not(@disabled) and contains(text(), 'Sign In')]");

// 🌟 And hello to this simplicity!
page.locator("ai=the login button in the header that says Sign In");
Enter fullscreen mode Exit fullscreen mode

πŸ”— Check it out: https://github.com/lila-team/ai-locators

😫 The Problem

Let's face it - maintaining locators in Playwright tests can be a real headache! πŸ€• They depend on how elements are positioned and declared in the frontend, which changes constantly and breaks your tests.

Sure, you could follow best practices like maintaining test-ids or being consistent with roles and labels (check out these best practices). But let's be real - developers want to move fast, and tests that are hard to maintain often get left behind! πŸƒβ€β™‚οΈπŸ’¨

πŸ€– LLMs to the Rescue!

Here's where the magic happens! ✨ This project uses LLMs to generate selectors based on the HTML content and your natural description of the elements you want to find.
It's implemented as a custom selector engine so it fits perfectly into your existing Playwright codebase! 🧩

// πŸ” Search the entire page
const element = page.locator("ai=the login button");
await element.click();

// 🎯 Search inside a container
const container = page.locators('.main-div');
const element = container.locator("ai=search input");
await element.fill("foobar");
Enter fullscreen mode Exit fullscreen mode

🎨 Choose Your LLM!

When registering the custom selector (check out our README for the how-to), you can pick your favorite LLM to power the magic - as long as it follows the OpenAI API! πŸͺ„

πŸŽ‰ Great news! ai-locators is available in both python and node! Give it a try and let us know what you think! πŸ’­

πŸ”— Link to project: https://github.com/lila-team/ai-locators

ai-locators is available for Python and for Node.

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Top comments (0)

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay