Learn how to integrate Appium with Java automated tests and Percy to catch visual differences in your application.
App 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:
An application is uploaded on App Automate, and an app URL is generated. Utilize the generated app URL in the BrowserStack configuration file as the app parameter
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.
Maven Archetype provides a template to quickly configure your project. Copy & run the following command on your terminal/command prompt to add browserstack-java-sdk dependency in your pom.xml and browserstack.yml file in your project.
Set a projectName.
After you run a test, an App Automate project and an App Percy project are created with the project name you set here.
If a App 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 a 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.
Set percyCaptureMode: manual.
Below sample browserstack.yml file shows how to set the percy, the projectName, and the percyCaptureMode parameters.
// ...imports
import com.browserstack.AppPercySDK;
public class TestNG extends AppiumTest {
@Test
public void test() throws Exception {
// ...
AppPercySDK.screenshot(driver, "My Screenshot");
// ...
}
}
Run your test script
Congratulations!
You have successfully integrated App 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, App Percy takes new screenshots. You can then see the comparisons between the two runs on the new build. You can access the App Percy dashboard directly from the App Automate dashboard.
Integrate App Percy with your test suite to run visual tests. To do that, follow these steps:
In order to conduct Percy visual testing for Java Appium, you must possess the following dependencies:
Appium version < 2.0.1
Node 12+ with npm
Create new app project on percy dashboard
Sign in to Percy and create a new app type project. After you’ve created the project, you’ll be shown a PERCY_TOKEN environment variable. Percy will use the PERCY_TOKEN to know which organisation and project to upload the screenshots to. You will need this PERCY_TOKEN in next steps.
Set the project token as an environment variable
Run the given command to set PERCY_TOKEN as an environment variable:
Below is an example test using the percy.screenshot function. Use this function wherever you have to take a screenshot.
```Java
// import AppPercy
import io.percy.appium.AppPercy;
public class Example {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
new URL("http://hub.browserstack.com/wd/hub"), caps);
AppPercy percy = new AppPercy(driver);
//Percy Screenshot function which captures and uploads screenshot to percy
percy.screenshot("First Screenshot");
driver.quit();
}
}
```
// import AppPercy
import io.percy.appium.AppPercy;
public class Example {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
new URL("http://hub.browserstack.com/wd/hub"), caps);
AppPercy percy = new AppPercy(driver);
//Percy Screenshot functionwhich captures and uploads screenshot to percy
percy.screenshot("First Screenshot");
driver.quit();}}
We are passing the required arguments.percy.screenshot method.
name (required) - The screenshot name; must be unique to each screenshot
Additional screenshot options (overrides any project options):
fullScreen - (optional) It indicates if the app is a full screen
options - (optional) screenshot params: Use ScreenshotOptions to set following params to override
deviceName - Device name on which screenshot is taken
statusBarHeight - int; Height of status bar for the device
navBarHeight - int; Height of navigation bar for the device
orientation - Orientation of the application
FullPage: boolean; [alpha] only supported on App Automate driver sessions [ needs @percy/cli 1.20.2+ ]
In case scrollview is overlapping with other app elements. Offsets can be provided to reduce the area which needs to be considered for scrolling:
topScrollviewOffset :(optional) [alpha] offset from top of scrollview [ needs @percy/cli 1.20.2+ ]; int
bottomScrollviewOffset: (optional) - [alpha] offset from bottom of scrollview [ needs @percy/cli 1.20.2+ ]; int
ScreenLengths: int; [alpha] max screen lengths for fullPage [ needs @percy/cli 1.20.2+ ]
ScrollableXpath (optional) - [alpha] scrollable element xpath for fullpage [ needs @percy/cli 1.20.2+ ]; string
ScrollableId (optional) - [alpha] scrollable element accessibility id for fullpage [ needs @percy/cli 1.20.2+ ]; string
IgnoreRegionXpaths (optional) - elements xpaths that user want to ignore in visual diff [ needs @percy/cli 1.23.0+ ]; list of string
IgnoreRegionAccessibilityIds (optional) - elements accessibility_ids that user want to ignore in visual diff [ needs @percy/cli 1.23.0+ ]; list of string
IgnoreRegionAppiumElements (optional) - appium elements that user want to ignore in visual diff [ needs @percy/cli 1.23.0+ ]; list of appium element object
CustomIgnoreRegions (optional) - Custom locations that users want to ignore in visual diffs, with (0, 0) starting at the top left (requires @percy/cli 1.23.0+); list of ignore_region objects.
IgnoreRegion:-
Description: This class represents a rectangular area on a screen that needs to be ignored for visual diff.
constructor:-
```Java
var ignoreRegion = new IgnoreRegion();
ignoreRegion.setTop() = top;
ignoreRegion.setBottom = bottom;
ignoreRegion.setLeft = left;
ignoreRegion.setRight = right;
```
var ignoreRegion = new IgnoreRegion();
ignoreRegion.setTop()=top;
ignoreRegion.setBottom = bottom;
ignoreRegion.setLeft = left;
ignoreRegion.setRight = right;
Parameters:
top (int): Top coordinate of the ignore region.
bottom (int): Bottom coordinate of the ignore region.
left (int): Left coordinate of the ignore region.
right (int): Right coordinate of the ignore region.
Raises IllegalArgumentException:If top, bottom, left, or right is less than 0, or top is greater than or equal to bottom, or left is greater than or equal to right.
valid: Ignore region should be within the boundaries of the screen.
Run your test script
Run your tests using percy app:exec.
If you are unable to use the percy app:exec command or prefer to run your tests using IDE run options, you can use the percy app:exec:start and percy app:exec:stop commands. To learn more, visit Run Percy.
[percy] Percy has started![percy] Created build #1: https://percy.io/[your-project][percy] Snapshot taken "Java example"[percy] Stopping percy...
[percy] Finalized build #1: https://percy.io/[your-project][percy] Done!
Congratulations!
You have successfully created your first build on App Percy. 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 snapshots. You can then see the comparisons between the two runs on the new build.
Did this page help you?
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
ON THIS PAGE
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