# TestFly Part 4: AI-assisted test generation with testfly-mcp and self-healing locators

URL: https://hakangul.lovable.app/blog/testfly-part-4-ai-assisted-test-generation-with-testfly-mcp
Author: Hakan Gül — Team Lead Senior Test Automation Engineer
Published: 2026-08-28
Updated: 2026-10-18
Category: ai-automation
Tags: TestFly, testfly-mcp, MCP, AI Testing, Self-Healing, Claude, Cursor, SDET

> Connect TestFly to Claude, Cursor and Copilot through testfly-mcp to record live browser sessions, generate Java tests with accessibility-first locators and heal broken selectors.

## English

## TestFly and the Model Context Protocol

The `testfly-mcp` companion server connects TestFly to AI coding assistants such as Claude Desktop, Cursor, Copilot and Antigravity.

## Connecting `testfly-mcp`

Add the server to your `mcp_config.json`:
```json
{
  "mcpServers": {
    "testfly-mcp": {
      "command": "npx",
      "args": ["-y", "testfly-mcp@latest"]
    }
  }
}
```

## Generating TestFly code from a user journey

Through MCP, the assistant launches a browser, records the interactions and writes TestFly code that uses accessibility-first locators. A checkout flow comes out like this:

```java
package io.testfly.examples;

import io.testfly.test.BaseTest;
import io.testfly.locator.Role;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;

public class CheckoutFlowTest extends BaseTest {

    @Test
    public void userCompletesPurchase() {
        open("/products");
        getByRole(Role.BUTTON).withName("Add to Cart").click();
        getByRole(Role.LINK, "Checkout").click();
        getByLabel("Full Name").type("Hakan Gül");
        getByRole(Role.BUTTON).withName("Place Order").click();

        assertEquals(getByTestId("order-status").getText(), "CONFIRMED");
    }
}
```

## Self-healing locators

Turn on `locators.selfHealing` in `testfly.yml`:
```yaml
locators:
  selfHealing: true
```

If an element can't be found with its primary selector, TestFly's healing engine looks at the surrounding DOM attributes (accessible name, ARIA role, nearby text) to find it, and the CI pipeline stays green.

## Türkçe

## Model Context Protocol ve TestFly

TestFly, `testfly-mcp` sunucusu üzerinden Claude Desktop, Cursor, Copilot ve Antigravity gibi yapay zeka asistanlarına doğrudan bağlanıyor.

## `testfly-mcp` kurulumu

Sunucuyu `mcp_config.json` dosyanıza ekleyin:
```json
{
  "mcpServers": {
    "testfly-mcp": {
      "command": "npx",
      "args": ["-y", "testfly-mcp@latest"]
    }
  }
}
```

## Doğal dilden TestFly kodu üretmek

Yapay zeka MCP üzerinden canlı bir tarayıcı açıyor, adımları sayfada deniyor ve erişilebilirlik standartlarına uygun TestFly kodu yazıyor. Sipariş akışı için çıkan kod şöyle:

```java
package io.testfly.examples;

import io.testfly.test.BaseTest;
import io.testfly.locator.Role;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;

public class CheckoutFlowTest extends BaseTest {

    @Test
    public void siparisTamamlamaAkisi() {
        open("/products");
        getByRole(Role.BUTTON).withName("Sepete Ekle").click();
        getByRole(Role.LINK, "Ödeme").click();
        getByLabel("Ad Soyad").type("Hakan Gül");
        getByRole(Role.BUTTON).withName("Siparişi Onayla").click();

        assertEquals(getByTestId("order-status").getText(), "ONAYLANDI");
    }
}
```

## Kendini onaran seçiciler (self-healing)

`testfly.yml` içinde self-healing özelliğini açın:
```yaml
locators:
  selfHealing: true
```

Bir seçici DOM değişikliği yüzünden bulunamazsa TestFly'ın onarım motoru alternatif ARIA ve metin özniteliklerine bakıyor ve test düşmeden tamamlanıyor.
