Selenium for Java tutorial
Covered in this doc
- Cloning an example Java application tested with Selenium
- Making some changes and seeing the snapshots and visual diffs in Percy
The tutorial assumes you're already familiar with Java and Selenium and focuses on using it with Percy. You'll still be able to follow along if you're not familiar with Java Selenium, but we won't spend time introducing Java Selenium concepts.
This tutorial assumes you have installed the following:
- Java 8 and Maven 3.6 or later
- node with npm
The tutorial also assumes you have Node 12+ with npm and git installed.
Clone the example application and install dependencies:
$ git clone https://github.com/percy/example-percy-java-selenium.git $ cd example-percy-java-selenium $ make install
The example app and its tests will now be ready to go. You can explore the app
by opening the
src/main/resources/index.html file in a browser.
Sign in to Percy and create a new project. You can name the project "todo" if you'd like. After you've created the project, you'll be shown a token environment variable.
In the shell window you're working in, export the token environment variable:
$ export PERCY_TOKEN="<your token here>"
$ set PERCY_TOKEN="<your token here>" # PowerShell $ $Env:PERCY_TOKEN="<your token here>"
Note: Usually this would only be set up in your CI environment, but to keep things simple we'll configure it in your shell so that Percy is enabled in your local environment.
Check out a new branch for your work in this tutorial (we'll call this branch
tutorial-example), then run tests & take snapshots:
$ git checkout -b tutorial-example $ make test
This will run the app's Java Selenium tests, which contain calls to create Percy snapshots. The snapshots will then be uploaded to Percy for comparison. Percy will use the Percy token you used in Step 2 to know which organization and project to upload the snapshots to.
You can view the screenshots in Percy now if you want, but there will be no visual comparisons yet. You'll see that Percy shows you that these snapshots come from your
Use your text editor to edit
index.html and introduce some visual changes. For example, you can
add inline CSS to bold the "Clear completed" button on line 32. After the change, that line looks
<button class="clear-completed" style="font-weight:bold">Clear completed</button>
Commit the change:
$ git commit -am "Emphasize 'Clear completed' button"
Run the tests with snapshots again:
$ make test
This will run the tests again and take new snapshots of our modified application. The new snapshots
will be uploaded to Percy and compared with the previous snapshots, showing any visual diffs.
At the end of the test run output, you will see logs from Percy confirming that the snapshots were
successfully uploaded and giving you a direct URL to check out any visual diffs.
Visit your project in Percy and you'll see a new build with the visual comparisons between the two
runs. Click anywhere on the Build 2 row. You can see the original snapshots on the left, and the new
snapshots on the right.
Percy has highlighted what's changed visually in the app! Snapshots with the largest changes are
shown first You can click on the highlight to reveal the underlying screenshot.
If you scroll down, you'll see that no other test cases were impacted by our changes to the 'Clear
completed' button. The unchanged snapshots appear grouped together at the bottom of the list.
From here, you can try making your own changes to the app and tests, if you like. If you do, re-run
the tests and you'll see any visual changes reflected in Percy.
Updated about 1 year ago
This tutorial used an app with Selenium tests and Percy already added. You can check out our Selenium for Java docs to learn how to add Percy to your own tests.