Selenium for .NET

Integrating Percy with your Selenium .NET tests

šŸ‘

Covered in this doc

  • Integrating Percy with your Selenium .NET tests
  • Installing and importing the PercyIO.Selenium package
  • Calling and configuring Percy.Selenium

NugetNuget

Setup


*If you're not ready to integrate Percy, check out our 2-minute Selenium for .NET tutorial

Install @percy/cli (currently only available from NPM). This package does the heavy lifting & communication to our API:

npm install --save-dev @percy/cli
yarn add -D @percy/agent

Install the PercyIO.Selenium package (for example, with .NET CLI):

dotnet add package PercyIO.Selenium

Usage

This is an example test using the Percy.Snapshot method.

using PercyIO.Selenium;

// ... other test code

FirefoxOptions options = new FirefoxOptions();
FirefoxDriver driver = new FirefoxDriver(options);
driver.Navigate().GoToUrl("http://example.com");
ā€‹
// take a snapshot
Percy.Snapshot(driver, ".NET example");

// snapshot options using an anonymous object
Percy.Snapshot(driver, ".NET anonymous options", new {
  widths = new [] { 600, 1200 }
});

// snapshot options using a dictionary-like object
Percy.Options snapshotOptions = new Percy.Options();
snapshotOptions.Add("minHeight", 1280);
Percy.Snapshot(driver, ".NET typed options", snapshotOptions);

Running the above normally will result in the following log:

[percy] Percy is not running, disabling snapshots

When running with percy exec, and your project's PERCY_TOKEN, a new Percy build will be created and snapshots will be uploaded to your project.

set PERCY_TOKEN=[your-projects-token]

# Powershell
$Env:PERCY_TOKEN="[your-projects-token]"
export PERCY_TOKEN=[your-project-token]
$ percy exec -- [your test command]
[percy] Percy has started!
[percy] Created build #1: https://percy.io/[your-project]
[percy] Snapshot taken ".NET example"
[percy] Snapshot taken ".NET anonymous options"
[percy] Snapshot taken ".NET typed options"
[percy] Stopping percy...
[percy] Finalized build #1: https://percy.io/[your-project]
[percy] Done!

Configuration

void Percy.Snapshot(WebDriver driver, string name, object? options)

  • driver (required) - A selenium-webdriver driver instance
  • name (required) - The snapshot name; must be unique to each snapshot
  • options - An object containing various snapshot options (see per-snapshot configuration options)

Global configuration

You can also configure Percy to use the same options over all snapshots. To see supported configuration read our configuration docs