Let me set the scene. It's a Thursday afternoon. The UI team pushed changes overnight, and now a third of my Playwright tests are screaming about locators that no longer exist. The element is still on the page. I can see it with my own eyes. But the selector I wrote three weeks ago? Gone. Irrelevant. A relic of a simpler time. This is the locator problem — and if you've spent any time in test automation, you know it intimately. Selectors break. Frequently. Quietly. Sometimes right before a release demo, if the universe is feeling particularly creative.

So I did what any reasonable engineer does when confronted with a maintenance spiral: I asked an AI to handle it.

After a bit of research, I discovered Gemini — Google's powerful large language model (LLM) has a free tier that offers a robust API. So with nothing much to lose and everything to gain, I embarked on the following journey.


Interacting with the Gemini API

Since I am primarily interested in using Gemini to resolve page locators that cannot be found, I created a simple helper method that accepts the failed locator and page dom as parameters which are passed to the API along with the prompt displayed below.

Gemini Helper Function The most challenging part of this process was developing the prompt. Since I am in the experimental phase of the project and am using the Gemini Free Tier, I had to be mindful of token limits while developing a prompt that contained enough context for Gemini to understand the problem and deliver a useful page locator.

It took a lot of trial and error, but ultimately I decided to move forward with the prompt noted above. I plan on revisiting this again the future in order to develop a more concise prompt that delivers the same results.

Adjusting the test spec

With the Gemini helper method in place, I created an automation test spec that registers a user for an account on a mock banking website. To test the Gemini integration, I am intentionally attempting to locate the "Register" button using a bad locator in a try/catch block.

When the test fails to find the Register button, the catch block is executed and the failed locator along with the page dom are sent to the Gemini helper method. Gemini Helper Function
I wanted to add visibility into the process, so I added print statements to log the suggested locator returned by Gemini.
The suggested locator is visibile in the Playwright test runner output. Gemini Helper Function
Gemini returns the suggested locator ( role=button[name="Register"] ) to the test spec, which then attempts to locate the element and perform a click action.

Putting it All Together

The Register button is successfully located and clicked, allowing the test to proceed to the next step which is to assert the welcome message is displayed confirming the user has successfully registered for an account. Gemini Helper Function

Conclusion

The integration of Google Gemini into a Playwright test suite to create a self-healing layer for locators is an exciting development in the world of test automation. While there are still challenges to overcome, such as optimizing the prompt and managing token limits, the potential benefits of this approach are significant. By leveraging the power of LLMs, we can reduce the maintenance burden of test automation and create more resilient test suites that can adapt to changes in the application under test.