DEV Community

Surfing Devs
Surfing Devs

Posted on

2 1 1 1

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay