---
title: "Reusable Snippets in TestDriver"
sidebarTitle: "Reusable Snippets"
description: "Discover how to modularize your test workflows using reusable YAML snippets in TestDriver for improved maintainability and scalability."
icon: "repeat"
---

import GitignoreWarning from "/snippets/gitignore-warning.mdx";

Reusable snippets in TestDriver allow you to modularize your test steps by creating smaller, reusable YAML files that can be embedded into larger test workflows. This approach improves test maintainability, reduces duplication, and makes your test suite more organized and scalable.

---

## What are reusable snippets?

Reusable snippets are YAML files containing a set of test steps that perform a specific task, such as logging in, navigating to a page, or setting up test prerequisites. These snippets can be referenced in other test files using the [`run`](/commands/run) command, enabling you to reuse common actions across multiple tests.

---

## Why use reusable snippets?

1. **Maintainability**: Update a snippet in one place, and all tests using it will automatically reflect the changes.
2. **Reusability**: Avoid duplicating common actions like login or setup across multiple tests.
3. **Modularity**: Break down complex workflows into smaller, manageable pieces.
4. **Consistency**: Ensure uniform behavior across tests by reusing the same logic.

---

## How to create and use reusable snippets

<Steps>
<Step title="Create a Snippet">

Save a YAML file containing the reusable steps. For example, create a `snippets/login.yaml` file for logging into an application:

```yaml
version: 6.0.0
steps:
  - prompt: Log in to the application
    commands:
      - command: hover-text
        text: Email address
        description: email input field label
        action: click
      - command: type
        text: ${TD_USERNAME} # Use environment variable for username
      - command: hover-text
        text: Password
        description: password input field label
        action: click
      - command: type
        text: ${TD_PASSWORD} # Use environment variable for password
      - command: hover-text
        text: Log In
        description: log in button
        action: click
```

</Step>

<Step title="Reference the Snippet in a Test">

Use the [`run`](/commands/run) command to include the snippet in your main test file. For example:

```yaml
version: 6.0.0
steps:
  - prompt: Log in and navigate to the dashboard
    commands:
      - command: run
        file: snippets/login.yaml
      - command: hover-text
        text: Dashboard
        description: dashboard link in the navigation bar
        action: click
```

</Step>

<Step title="Parameterize Inputs">

Use environment variables to pass dynamic data like usernames and passwords. Define these variables in your `.env` file or CI/CD pipeline:

```bash
TD_USERNAME=your_username
TD_PASSWORD=your_password
```

<GitignoreWarning/>
</Step>
</Steps>

## Example: Combining multiple snippets

You can chain multiple snippets together to create complex workflows. For example:

```yaml
version: 6.0.0
steps:
  - prompt: Log in, search for a product, and add it to the cart
    commands:
      - command: run
        file: snippets/login.yaml
      - command: run
        file: snippets/search_product.yaml
      - command: run
        file: snippets/add_to_cart.yaml
```

---

## Best practices for reusable snippets

1. **Organize Snippets**: Store reusable snippets in a dedicated `snippets/` directory for better organization.
2. **Use Descriptive Names**: Name snippet files clearly (for example, `login.yaml`, `setup.yaml`) to indicate their purpose.
3. **Test Snippets Independently**: Validate each snippet before integrating it into larger workflows.
4. **Parameterize Inputs**: Use placeholders or environment variables for dynamic data.
5. **Document Snippets**: Add comments or documentation to explain the purpose and usage of each snippet.

---

## Benefits of reusable snippets

- **Efficiency**: Save time by reusing existing logic.
- **Scalability**: Easily extend your test suite by combining snippets.
- **Consistency**: Ensure uniform behavior across tests.
- **Reduced Maintenance**: Centralize updates to common actions.

---

Reusable snippets are a cornerstone of efficient test design in TestDriver. By modularizing your tests, you can create a scalable, maintainable, and reusable test suite that adapts to your application's evolving needs. Start by creating snippets for common actions like login or navigation, and expand your library as your test suite grows.
