Skip to main content
No Result Found

Integrate Percy with Selenium and CSharp

Learn how to integrate Selenium with CSharp automated tests and Percy to catch visual differences in your website.

Percy integrates with your tests using both Percy and BrowserStack SDK. To establish this integration, choose the appropriate SDK and refer to the following section accordingly:

You can integrate your tests using the BrowserStack SDK and manage browsers through either Automate or Percy. Percy supports the latest versions of modern browsers, including Chrome, Firefox, Edge, and Safari. With Automate, you can configure various browser names and versions using the Automate capability builder.

To establish this integration, choose the appropriate option and refer to the following section accordingly:

To integrate your CSharp based test suite with Percy using the BrowserStack SDK, follow these steps:

Prerequisites

Before you start, ensure that you have a BrowserStack Automate account using the BrowserStack SDK to integrate your test script.

Set BrowserStack credentials

Save your BrowserStack credentials as environment variables. It simplifies running your test suite from your local or CI environment. You can export the environment variables for the Username and Access Key of your BrowserStack account or you can set it in the config file.

Copy icon Copy
export BROWSERSTACK_USERNAME="YOUR_USERNAME"
export BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"
Copy icon Copy
$env:BROWSERSTACK_USERNAME="YOUR_USERNAME"
$env:BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"
Copy icon Copy
setx BROWSERSTACK_USERNAME "YOUR_USERNAME" 
setx BROWSERSTACK_ACCESS_KEY "YOUR_ACCESS_KEY" 
set BROWSERSTACK_USERNAME=YOUR_USERNAME
set BROWSERSTACK_ACCESS_KEY=YOUR_ACCESS_KEY

Install BrowserStack SDK using Maven Archetype

Run the given command to install the BrowserStack SDK and create a browserstack.yml file in the root directory of your project.

  • The browserstack.yml config file holds all the required capabilities to run tests on BrowserStack.

  • The BrowserStack.TestAdapter works on top of your framework test adapter to run parallel tests on BrowserStack across multiple platforms mentioned on the browserstack.yml file.

Copy icon Copy
dotnet add package BrowserStack.TestAdapter
dotnet build
dotnet browserstack-sdk setup --userName "YOUR_USERNAME" --accessKey "YOUR_ACCESS_KEY"
Copy icon Copy
dotnet add package BrowserStack.TestAdapter
dotnet build
dotnet browserstack-sdk setup --userName "YOUR_USERNAME" --accessKey "YOUR_ACCESS_KEY"

Update your BrowserStack config file

In the browserstack.yml file:

  1. Set percy: true.

  2. Set a projectName.
    After you run a test, an Automate project and a Percy project are created with the project name you set here.
    If a Percy project by the name you set in the browserstack.yml file already exists, your screenshots are added to the same project. However, if the name matches an App Percy project, your visual tests will continue to run, but an error message will appear in your logs. Your functional tests still run as expected.

  3. Set percyCaptureMode: manual.

Below sample browserstack.yml file shows how to set the percy, the projectName, and the percyCaptureMode parameters.

Copy icon Copy
userName: YOUR_USERNAME
accessKey: YOUR_ACCESS_KEY
platforms:
  - os: Windows
    osVersion: 11
    browserName: Chrome
    browserVersion: 103.0
  - os: Windows
    osVersion: 10
    browserName: Firefox
    browserVersion: 102.0
  - os: OS X
    osVersion: Big Sur
    browserName: Safari
    browserVersion: 14.1
parallelsPerPlatform: 1
browserstackLocal: true
buildName: browserstack-build-1
projectName: BrowserStack Sample
debug: false
percy: true
percyCaptureMode: manual

Manually add Percy screenshot method to update test script

To manually capture screenshots alongside the auto mode, implement the following steps in your test script:

  1. Import the BrowserStack Percy SDK in your test script.
  2. Add the PercySDK.screenshot(driver, name) method at required points in your test script to get the screenshots you want.

Here’s an example:

C#
Copy icon Copy
using BrowserStackSDK.Percy;
using NUnit.Framework;
namespace Tests;
public class MyTest
{
    [Test]
    public void SampleTest()
    {
        // ...
        PercySDK.Screenshot(driver, "Screenshot name");
    }
}

Run your test script

Congratulations!

You have successfully integrated Percy with BrowserStack SDK and created your first build. To see the build with snapshots of your application, visit your project in Percy.

When you run another build with visual changes to your application, Percy takes new screenshots. You can then see the comparisons between the two runs on the new build. You can access the Percy dashboard directly from the Automate dashboard.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle