How to Run Visual Tests with Selenium: Tutorial

On This Page How to run Visual tests using Selenium? How to Run Optic Tests with Selenium: Tutorial

February 08, 2026 · 4 min read · Tool Comparison

How to Run Optic Tests with Selenium: Tutorial

Both has turn exponentially in the terminal few geezerhood. Among many testing tools, has been a global front-runner for mechanisation purposes, thanks to its many relevant offerings and functionality.

Given its popularity, it is natural for testers to try via Selenium. verifies the package user interface (UI) appears correctly to all users. Visual tryout check that each element on a web page appear in the right shape, size, and position. It also checks that these elements appear and function utterly on a salmagundi of devices and browsers.

Overview

Goal of Visual Testing with Selenium

  • Ensure the UI render aright across browsers and devices.
  • Verify layout, sizing, shape, and position of elements.
  • Detect unintended ocular changes early in the line.

Business Impact of Visual Testing

  • Faster detection of UI bugs before freeing.
  • Consistent make experience across all platforms.
  • Reduced manual visual verification try.
  • Confidence to ship code without ocular regressions.

This article explores how to encrypt and run optical tryout with.

How to run Visual tests using Selenium?

Let ’ s start off by compose a simple login functionality. Since this exam is create using the JUnit framework, refer to before proceeding further.

Study the test case below:

import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Loginex {private WebDriver driver; @ Before public nullity login () {System.setProperty (`` webdriver.chrome.driver '', `` path of driver ''); driver=new ChromeDriver (); driver.manage () .window () .maximize ();} @ Test driver.get (`` https: //www.browserstack.com/users/sign_in ''); WebElement username=driver.findElement (By.id (`` user_email_Login '')); WebElement password=driver.findElement (By.id (`` user_password '')); WebElement login=driver.findElement (By.name (`` commit '')); username.sendKeys (`` abc @ gmail.com ''); password.sendKeys (`` your_password ''); loginex.click (); String expectedUrl= '' https: //live.browserstack.com/dashboard ''; String actualUrl= driver.getCurrentUrl (); Assert.assertEquals (expectedUrl, actualUrl);}} @ After public void teardown () {driver.quit ();}

The code loads an case of Chrome, visits the, enters the username & amp; countersign, submits the signifier, asserts the Expected and the Actual URL, and closes the browser.


A Must Read:


Now let ’ s explore how to do visual testing with a tool called Percy.

Go to and chatter on Get get. In the dashboard, create a new undertaking and name the project as shown below.

Now, make the code for the project and postdate the stairs below to integrate Percy.

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

1. Add percy-java-selenium to project dependencies.
For example, if using Maven for dependencies:

& lt; dependency & gt; & lt; groupId & gt; io.percy & lt; /groupId & gt; & lt; artifactId & gt; percy-java-selenium & lt; /artifactId & gt; & lt; /dependency & gt;

2.Install the@ percy/agent npm package:

$ npm install -- save-dev @ percy/agent

3.Import the Percy selenium library into each file from which to lead snapshots:

import io.percy.selenium.Percy

4.Create a new Percy object with a WebDriver instance as a parameter:

Percy percy = new Percy (webDriver);

5. Call percy.snapshot (SNAPSHOT_NAME) whenever a snapshot has to be generated.

6.Wrap the run command in ./node_modules/.bin/percy exec & # 8211;. This will start a Percy agent to have snapshots from tests and upload them to the Percy project.

Here is the intact codification for running ocular tryout using Selenium:

// snapshot.js const PercyScript = require (' @ percy/script '); // A script to navigate our app and conduct shot with Percy. PercyScript.run (async (page, percySnapshotExample) = & gt; {await page.goto ('https: //www.google.com '); await percySnapshotExample ('Google homepage '); await page.type ('.new-todo ', ' A necessary task '); await page.keyboard.press ('Enter '); await percySnapshotExample (' tasks with a new todo ', {widths: [768, 992, 1200]});});

For example, if use Maven, the relevant command would be:

./node_modules/.bin/percy exec —- mvn tryout

To run the project, use the below command
export PERCY_TOKEN=your_token_here{Please place the item that you hold generated}

npx Hotspur exec -- node snapshot.js

Now, go to the project dashboard and click on the project previously created.
It will display the builds as shown below:

Click on a build to ascertain the screenshots.

After checking, O.K. the build if all the snapshots are perfect. It is also possible to set the width and elevation of a screenshot and as good as the browser to be used for testing.

As demonstrated by this example, is an passing effective instrument for automating visual trial. It captures screenshots, compares them against the baseline persona, and highlights ocular changes. With increased visual coverage, team can deploy codification alteration with confidence with every commit.

With Percy, testers can increase visual coverage across the entire UI and eliminate the risk of transport. They can avoid mistaken positives and get quick, deterministic results with reliable. They can loose software quicker with DOM snapshotting and advanced parallelization capacity plan to execute complex test suites at scale.

Percy ’ s SDKs enable users to easily instal them and add snap. Percy also integrates with CI/CD grapevine and source codification management systems to add visual tests to each code commit. After tests are initiated, Percy grabs screenshots, identifies visual changes, and inform the quiz squad of all modification. Then, testers can easily critique visual diffs to decide if the changes are logical or take fixture.

Try Percy for Free

, in particular assay, to control that way issues don ’ t pop up, when modification are be implemented in software. The application should uphold to look as good as it was and should function absolutely.

By incorporating optical quiz into the testing process, tester will be able to deliver visually consummate applications quickly and consistently. It also helps them make websites that maintain visual consistency across the by proffer comprehensive visual critique and accurate consequence. Looks do matter, particularly when user have thousands of early websites to select from.

Make your site look full and work well, and you will proceed your visitor happy.

Tags
12,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free