> 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="https://2622859358-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FErSaU4WjIuLC7zfVEh8L%2Fuploads%2FcSAiIGs42WZ2jl4gldf0%2Fimage.png?alt=media&amp;token=9e28cf07-a549-4fdc-be86-73366d434294" 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="https://2622859358-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FErSaU4WjIuLC7zfVEh8L%2Fuploads%2Fup8WfgXRvZ4yPbuZNSVT%2Fimage.png?alt=media&amp;token=b383d9ee-3dd1-4dc6-bd88-9d4f9d521e8c" 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 can be run independently or within the context of a calling test case, depending on their setup and dependencies.

To run a Fragment **independently**:

1. Open the Fragment in the editor.
2. Run the Fragment directly.

To run a Fragment **with a calling test case**:

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

💡 Use **Run with** when the Fragment depends on setup, variables, browser sessions, or other context provided by the calling test case.

:star2:Prefer opening the browser in the main test case so the Fragment can reuse the session.

```gherkin
Given browser "chrome"
Given fragment "Complete checkout"
When I run the fragment
```

Open a browser inside the Fragment only when a separate session or user is required.

**Example:** User 1 submits a form in Browser 1, and User 2 verifies it in Browser 2.

## 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
