Hey! These docs are for version 1, which is no longer officially supported. Click here for the latest version, 2-web!

### [percy-java-selenium on GitHub <i class="fa fa-github" aria-hidden="true"></i>](🔗)

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.percy/percy-java-selenium/badge.svg)](🔗)

Visual regression testing for [Selenium](🔗) Java tests with Percy.

# Installation

Make sure you have completed [Setup](🔗) of your CI service first. You can also test Percy in your [local development](🔗) environment.

Add `percy-java-selenium` to your project dependencies. If you're using Maven:

If you're using a different build system, see https://search.maven.org/artifact/io.percy/percy-java-selenium for details for your specific system.

Make sure to use at least version 0.0.3 of the SDK. (Previous versions were compiled with Java 11.)

Install the Percy agent, which will upload snapshots on your behalf. It is available as a Node module, and you can install it using [npm](🔗).

To install locally for your project:

If your project does not already have a `package.json` file in the root directory, run `npm init` and follow the prompts. Without a `package.json` present in the project, the next install step will fail.

This will install the percy agent executable in a `node_modules/` folder inside the current working directory. You can access the agent's executable at `./node_modules/.bin/percy`.

# Quick Setup

These are the minimal steps required to add visual testing to your existing Selenium code:

  1. Import the Percy selenium library into your file: `import io.percy.selenium.Percy`

  2. Create a new Percy object, with a WebDriver instance as a parameter: `Percy percy = new Percy(webDriver);`

  3. Call `percy.snapshot(SNAPSHOT_NAME)` wherever you want to save a snapshot.

  4. Wrap your run command in `percy exec --`, e.g. `./node_modules/.bin/percy exec -- mvn test`.

Next, we will go through each of these steps in detail.

# Setup

In order to start creating snapshots from your Java Selenium scripts or tests, you'll need to import the `io.percy.selenium.Percy` function from the `percy-java-selenium` library. You will need to do this in each file from which you want to take snapshots:

After you instantiate a Selenium WebDriver, create a new Percy instance:

Use `percy.snapshot(...)` to generate a snapshot:

Finally, wrap your program or test runner command in the `percy exec` command. This will start a Percy agent to receive snapshots from your tests and upload them to your Percy dashboard. For example, if you are using Maven to run your tests, your new test command becomes:

Note the double dash, `--`, between `percy exec` and your test run command.

That's it! Now, whenever CI runs, a snapshot of the app in that state will be uploaded to Percy for visual regression testing!

For an example showing how to add Percy snapshots to an existing Selenium Java test suite, see [https://github.com/percy/example-percy-java-selenium/pull/3](🔗).

# Usage

Percy constructor:

`driver` is an object that implements the `org.openqa.selenium.WebDriver` interface.

Percy snapshot methods:

`snapshotName` is a `String` that will be used as the snapshot name. It can be any string that makes sense to you to identify the page state. It should be unique and remain the same across builds.

`widths` is a `List<Integer>` representing the browser widths at which you want to take snapshots.

`minHeight` is an `Integer` specifying the minimum height of the resulting snapshot, in pixels. Defaults to 1024px.

## Examples

# Global Configuration

You can also configure Percy to use the same options over all snapshots. To see supported configuration including widths read our [Configuration doc](🔗).

# Contributing

  1. Fork it ( https://github.com/percy/percy-java-selenium/fork )

  2. Create your feature branch (`git checkout -b my-new-feature`)

  3. Commit your changes (`git commit -am 'Add some feature'`)

  4. Push to the branch (`git push origin my-new-feature`)

  5. Create a new pull request

[Throw a ★ on it!](🔗) 😄