# TestFly Part 4: AI-Assisted Test Generation with testfly-mcp & 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-08-29
Category: ai-automation
Tags: TestFly, testfly-mcp, MCP, AI Testing, Self-Healing, Claude, Cursor, SDET

> Connect TestFly with Claude, Cursor, and Copilot via testfly-mcp. Record live browser sessions, generate accessibility-first Java code, and self-heal broken selectors.

## English

## The Next Frontier: Model Context Protocol & TestFly

TestFly bridges modern AI coding assistants (Claude Desktop, Cursor, Copilot, Antigravity) through the **`testfly-mcp`** companion server.

---

## 1. Connecting `testfly-mcp`

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

---

## 2. Generating TestFly Code from User Journeys

The AI launches a browser via MCP, records interactions, and writes clean TestFly code utilizing accessibility-first locators:

```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");
    }
}
```

---

## 3. Self-Healing Locators (`locators.selfHealing`)

Enable locator self-healing in `testfly.yml`:
```yaml
locators:
  selfHealing: true
```

When an element is not found by its primary selector, TestFly's healing engine analyzes surrounding DOM attributes (accessible name, ARIA role, nearby text) to locate the element and keep your CI pipeline green.

## Türkçe

## Yeni Dönem: Model Context Protocol ve TestFly

TestFly, **`testfly-mcp`** sunucusu aracılığıyla yapay zeka asistanlarına (Claude Desktop, Cursor, Copilot, Antigravity) doğrudan bağlanır.

---

## 1. `testfly-mcp` Entegrasyonu

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

---

## 2. Doğal Dilden TestFly Kodu Üretimi

Yapay zeka MCP üzerinden canlı tarayıcı açar, adımları dener ve erişilebilirlik standartlarına uygun TestFly kodu üretir:

```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");
    }
}
```

---

## 3. Kendi Kendini Onaran Seçiciler (Self-Healing)

`testfly.yml` içinde self-healing özelliğini etkinleştirin:
```yaml
locators:
  selfHealing: true
```

Bir seçici DOM değişikliği nedeniyle bulunamadığında TestFly'ın onarım motoru alternatif ARIA ve metin özniteliklerini analiz ederek testi düşürmeden tamamlar.
