> For the complete documentation index, see [llms.txt](https://docs.testfirst.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.testfirst.com/testfirst-editor/fragments.md).

# Fragments

## What are Fragments?

Fragments are **reusable blocks of test steps** that you can use across multiple test cases.

They help you avoid duplication and make your tests easier to maintain.

### Why Use Fragments

Use Fragments when:

* The same steps are repeated across tests (e.g., login, navigation)
* You want to **standardize common flows**
* You want to update logic **in one place instead of many**

💡 Update a Fragment once → all dependent tests benefit automatically.

## Create a Fragment

### Extract from an Existing Test Case

You can convert existing steps into a reusable Fragment directly from a test case.

**Steps:**

1. Select the steps you want to reuse
2. Click the **more options** <img src="/files/TNRPE6NYfUzIyEsybXhR" alt="" data-size="line"> button next to the **Preview** button
3. Click **Extract**
4. Enter a name in the **Extract as Fragment** modal
5. Click **Create**

#### Replace Source Steps

* When enabled → the selected steps are replaced with a Fragment call
* If disabled → the Fragment is created, but the original steps remain

💡 This is the fastest way to refactor duplicate logic into reusable components.

### Create from Scratch

1. Open the **TestFirst Editor**
2. Open the library from the left toolbar, then switch to the **Fragment** tab (next to the **Test Case** tab)
3. Right-click → **New Fragment**, or click the more options icon <img src="/files/XSKkjWeK7cDYzOe99f5A" alt="" data-size="line"> in the toolbar → **New Fragment**
4. Enter a fragment name and double-click to open it
5. Add steps using Gherkin or the Recorder
6. Save the Fragment

#### Use a Fragment in a Test Case

Once created, Fragments can be reused in test cases.

#### **Example**

```
Scenario: Login and access dashboard
    When I run fragment "TCF1"
    Then "Welcome" is displayed
```

### Running a Fragment

Fragments are **not standalone test cases** and usually cannot be executed independently.

To run a Fragment:

1. Open the Fragment in the editor
2. Use the **Run with** dropdown
3. Select a test case that uses this Fragment
4. Run the test

💡 The Fragment will execute **within the context of the selected test case**, including its setup and dependencies.

## When to Use Fragments

Fragments are ideal for:

* Login / authentication flows
* Repeated navigation steps
* Setup or precondition steps
* Complex reusable sequences

***

## Best Practices

* Keep Fragments **small and focused**
* Use clear, descriptive names (e.g., `Login Flow`, `Create User`)
* Avoid putting too many unrelated steps in one Fragment
* Reuse instead of duplicating logic

### Things to Keep in Mind

* Changes to a Fragment affect all test cases using it
* Fragments should represent **reusable behaviour**, not full test cases
* Keep them stable to avoid breaking multiple tests
