# Selenium

Add Buddy visual testing to Selenium tests in Java, C#, or JavaScript - install the plugin, take snapshots, and run sessions with the bdy CLI.

This guide shows how to add Buddy Visual Tests to an existing Selenium test suite. Plugins are available for **Java**, **C#**, and **JavaScript**. Snapshots taken in your tests are uploaded on every run and compared against the approved baseline.

## Prerequisites

- [`bdy` CLI installed](/docs/cli/getting-started.md) (requires Node.js v20+)
- Suite token exported as `BUDDY_VT_TOKEN` - copy it from the [suite settings](/docs/tests/visual-tests/suite-settings.md) (in Buddy pipelines the token is set automatically)

## Java

Add the dependency to `pom.xml`:

```xml
<dependency>
    <groupId>works.buddy.visual-tests</groupId>
    <artifactId>plugin</artifactId>
    <version>1.0.2</version> <!-- check for the latest version -->
</dependency>
```

Initialize the driver and plugin in your test class:

```java
private static WebDriver driver;
private static VisualTestsPlugin visualTestPlugin;

@BeforeAll
static void setupAll() {
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    visualTestPlugin = new VisualTestsPlugin(driver, false);
}
```

Take snapshots in your tests:

```java
visualTestPlugin.takeSnapshot("Snapshot name");
```

Run the tests through the CLI:

```bash
bdy tests visual session create "mvn test"
```

## C#

Add the package to your project:

```bash
dotnet add package BuddyWorks.VisualTests.Selenium
```

Initialize the driver and plugin in your test class:

```csharp
static IWebDriver driver = null;
static VisualTestsPlugin visualTestsPlugin = null;

[AssemblyInitialize]
public static void AssemblyInit(TestContext context)
{
    driver = new ChromeDriver();
    visualTestsPlugin = new VisualTestsPlugin(driver, suppressErrors: false);
}
```

Take snapshots in your tests:

```csharp
await visualTestsPlugin.TakeSnapshotAsync("Snapshot name");
```

Run the tests through the CLI:

```bash
bdy tests visual session create "dotnet test TestProject.csproj"
```

## JavaScript

Install the plugin:

```bash
npm i @buddy-works/visual-tests-selenium
```

Initialize the driver and plugin in your test file:

```javascript
import { Builder } from "selenium-webdriver";
import VisualTestsPlugin from "@buddy-works/visual-tests-selenium";

const driver = await new Builder().forBrowser("chrome").build();
const visualTestsPlugin = new VisualTestsPlugin(driver);
```

Take snapshots in your tests:

```javascript
await visualTestsPlugin.takeSnap('Snapshot name');
```

Run the tests through the CLI:

```bash
bdy tests visual session create "node test.js"
```

Whichever language you use, when the run finishes the session appears in the suite with all captured snapshots rendered across the browsers and devices configured in the [suite settings](/docs/tests/visual-tests/suite-settings.md). See [Reviewing & Approvals](/docs/tests/visual-tests/reviewing-approvals.md) for the next step.

## Let AI do it

If you use an AI coding agent, paste this prompt to have it wire up visual tests in your existing tests:

```
Add Buddy Visual Tests to my Selenium test suite:
1. Install the Buddy visual tests plugin for my language:
   - Java: Maven dependency works.buddy.visual-tests:plugin
   - C#: NuGet package BuddyWorks.VisualTests.Selenium
   - JavaScript: npm package @buddy-works/visual-tests-selenium
2. Initialize a VisualTestsPlugin instance next to the WebDriver setup
   (one shared instance for the test class/file).
3. After each meaningful, stable UI state in the tests, take a snapshot:
   - Java: visualTestPlugin.takeSnapshot("<descriptive-snapshot-name>");
   - C#: await visualTestsPlugin.TakeSnapshotAsync("<descriptive-snapshot-name>");
   - JavaScript: await visualTestsPlugin.takeSnap('<descriptive-snapshot-name>');
4. Use kebab-case snapshot names; group related views with "/" (e.g. "checkout/step-1").
5. Do not change any test logic or assertions.
The tests will be executed through the Buddy CLI, e.g.:
bdy tests visual session create "mvn test"
```


---
Original source: https://buddy.works/docs/tests/visual-tests/e2e-frameworks/selenium