Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sauce Visual Espresso docs #2988

Merged
merged 16 commits into from
Dec 10, 2024
248 changes: 248 additions & 0 deletions docs/visual-testing/integrations/espresso.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
---
sidebar_label: Espresso
---

import ClippingDescription from '../_partials/_clipping-description.md';

# Espresso Integration

You can use the Sauce Visual Espresso plugin to set up an integration with the Sauce Labs platform and start comparing the visual snapshots generated during your tests.

## Introduction

This guide requires an existing Android JUnit setup.

Check warning on line 13 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L13

[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.
Raw output
{"message": "[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 13, "column": 53}}}, "severity": "WARNING"}

You can alternatively take a look to our [example repository](#examples).

Sauce Visual Espresso plugin provides a library exposing a `VisualClient` object that provides the method

Check warning on line 17 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L17

[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.
Raw output
{"message": "[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 17, "column": 106}}}, "severity": "WARNING"}
kb-kerem marked this conversation as resolved.
Show resolved Hide resolved
`sauceVisualCheck()` to take a screenshot and send it to Sauce Visual for comparison.

## Quickstart

### Step 1: Add Sauce Visual dependency

Add [Sauce Visual](https://central.sonatype.com/artifact/com.saucelabs.visual/visual-espresso) dependency to your build.gradle

```groovy reference
https://github.com/saucelabs/visual-examples/blob/main/espresso/app/build.gradle#L106
```

_Note: You can find the latest versions available [here](https://central.sonatype.com/artifact/com.saucelabs.visual/visual-espresso)._

Check warning on line 30 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L30

[sauce.MeaningfulLinkWords] Improve SEO and accessibility by rewriting 'here' in the link text.
Raw output
{"message": "[sauce.MeaningfulLinkWords] Improve SEO and accessibility by rewriting 'here' in the link text.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 30, "column": 52}}}, "severity": "WARNING"}

### Step 2: Configure Visual Testing integration

Declare a VisualClient instance as class variable

```java
import com.saucelabs.visual.VisualClient;

static VisualClient visual = VisualClient.builder(sauceUsername, sauceAccessKey)
.buildName("Sauce Demo Test")
.build();
```

To enhance efficiency in managing tests, it's important to provide a specific test name and suite name for each test. This practice allows Sauce Visual to effectively organize snapshots into coherent groups. As a result, it simplifies the review process, saving time and effort in navigating through test results and understanding the context of each snapshot.

Moreover, our Espresso binding offers an automated solution to this process. By integrating the following code snippet into your tests, the Java Binding can automatically assign appropriate test names and suite names, streamlining your testing workflow.

```java
import com.saucelabs.visual.junit.TestMetaInfoRule;

import org.junit.Rule;

public class MyJunitTestClass {

Check warning on line 54 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L54

[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.
Raw output
{"message": "[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 54, "column": 1}}}, "severity": "WARNING"}
@Rule
public TestMetaInfoRule testMetaInfoRule = new TestMetaInfoRule();
}
```

Don't forget to finish the build

```java
import org.junit.AfterClass;

@AfterClass
public static void tearDown() {
if (visual != null) {
visual.finish();
}
}
```

### Step 3: Add visual tests in your tests

Check warning on line 73 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L73

[sauce.Headings] 'Step 3: Add visual tests in your tests' should use title case capitalization.
Raw output
{"message": "[sauce.Headings] 'Step 3: Add visual tests in your tests' should use title case capitalization.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 73, "column": 5}}}, "severity": "WARNING"}

Add a check to one of your tests:


```java
import org.junit.Test;

@Test
void checkLogin() {

Check warning on line 83 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L83

[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.
Raw output
{"message": "[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 83, "column": 1}}}, "severity": "WARNING"}
visual.sauceVisualCheck("Before Login");
kb-kerem marked this conversation as resolved.
Show resolved Hide resolved
}
```

### Step 4: Configure your Sauce Labs credentials

Check warning on line 88 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L88

[sauce.Headings] 'Step 4: Configure your Sauce Labs credentials' should use title case capitalization.
Raw output
{"message": "[sauce.Headings] 'Step 4: Configure your Sauce Labs credentials' should use title case capitalization.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 88, "column": 5}}}, "severity": "WARNING"}

Sauce Visual Espresso expects Sauce Labs username and access key to be passed when initiating the client.

Check warning on line 90 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L90

[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.
Raw output
{"message": "[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 90, "column": 106}}}, "severity": "WARNING"}
It's recommended to define and pass your Sauce Labs credentials to `VisualClient` as custom `BuildConfig` fields.

Check warning on line 91 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L91

[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.
Raw output
{"message": "[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 91, "column": 114}}}, "severity": "WARNING"}

```groovy
android {
buildConfigField "String", "SAUCE_USERNAME", "YOUR_SAUCE_USERNAME"
buildConfigField "String", "SAUCE_ACCESS_KEY", "YOUR_SAUCE_ACCESS_KEY"
}
```

```java
import com.saucelabs.visual.VisualClient;

static VisualClient visualClient = VisualClient.builder(
BuildConfig.SAUCE_USERNAME,
BuildConfig.SAUCE_ACCESS_KEY)
.buildName("Espresso Basic Sample")
.build();
```

Username and Access Key can be retrieved from https://app.saucelabs.com/user-settings.

Check warning on line 110 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L110

[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.
Raw output
{"message": "[sauce.EOLWhitespace] Remove whitespace characters from the end of the line.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 110, "column": 87}}}, "severity": "WARNING"}

### Step 5: Run the test

Upon executing your tests for the first time under this step, a visual baseline is automatically created in our system. This baseline serves as the standard for all subsequent tests. As new tests are run, they are compared to this original baseline, with any deviations highlighted to signal visual changes. These comparisons are integral for detecting any unintended visual modifications early in your development cycle. All test builds, including the initial baseline and subsequent runs, can be monitored and managed through the Sauce Labs platform at [Sauce Visual Builds](https://app.saucelabs.com/visual/builds).

Remember, the baseline is established during the initial run, and any subsequent visual differences detected will be marked for review.

## Advanced usage

Check warning on line 118 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L118

[sauce.Headings] 'Advanced usage' should use title case capitalization.
Raw output
{"message": "[sauce.Headings] 'Advanced usage' should use title case capitalization.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 118, "column": 4}}}, "severity": "WARNING"}

### Customizing Your Build Lifecycle

Following builder methods are available for `VisualClient` if you'd like to manage your build lifecyle externally.

- `buildId(String buildId)`: For advanced users, a user-supplied Sauce Labs Visual build ID. Can be used to create builds in advance using the GraphQL API or CLI tool. This can be used to parallelize tests with multiple browsers, shard, or more. By default, this is not set and we create / finish a build during setup / teardown.
- `customId(String customId)`: For advanced users, a user-supplied custom ID to identify this build. Can be used in CI to identify / check / re-check the status of a single build. Usage suggestions: CI pipeline ID.

### Build attributes

Check warning on line 127 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L127

[sauce.Headings] 'Build attributes' should use title case capitalization.
Raw output
{"message": "[sauce.Headings] 'Build attributes' should use title case capitalization.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 127, "column": 5}}}, "severity": "WARNING"}

When creating the service in `VisualClient`, extra fields can be set to define the context, thus acting on which baselines new snapshots will be compared to. ([More info on baseline matching](../../visual-testing.md#baseline-matching))

It needs to be defined through the `VisualApi.Builder` object.

Methods available:

- `buildName(String buildName)`: Sets the name of the build
- `projectName(String project)`: Sets the name of the project
- `branchName(String branch)`: Sets the name of the branch
- `defaultBranchName(String defaultBranch)`: Sets the name of the default branch

Example:

```java
import com.saucelabs.visual.VisualClient;

visual = VisualClient.builder(username, accessKey)
.buildName("Sauce Demo Test")
.branchName("main")
.projectName("Java examples")
.build();
```

### Ignored regions

Check warning on line 152 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L152

[sauce.Headings] 'Ignored regions' should use title case capitalization.
Raw output
{"message": "[sauce.Headings] 'Ignored regions' should use title case capitalization.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 152, "column": 5}}}, "severity": "WARNING"}

#### Component-based ignored region

Check warning on line 154 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L154

[sauce.Headings] 'Component-based ignored region' should use title case capitalization.
Raw output
{"message": "[sauce.Headings] 'Component-based ignored region' should use title case capitalization.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 154, "column": 6}}}, "severity": "WARNING"}

Sauce Visual provides a way to ignore a list of components.

An ignored component can be a specific element from the page.

Those ignored components are specified when requesting a new snapshot.

Example:

```java
import com.saucelabs.visual.VisualCheckOptions;;

visualClient.sauceVisualCheck("Inventory Page",
VisualCheckOptions.builder()
.ignore(withId(R.id.changeTextBt))
.build());
```

#### User-specified ignored region

Check warning on line 173 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L173

[sauce.Headings] 'User-specified ignored region' should use title case capitalization.
Raw output
{"message": "[sauce.Headings] 'User-specified ignored region' should use title case capitalization.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 173, "column": 6}}}, "severity": "WARNING"}

Alternatively, ignored regions can be user-specified areas. A region is defined by four elements.

- `x`, `y`: The location of the top-left corner of the ignored region
- `width`: The width of the region to ignore
- `height`: The height of the region to ignore

_Note: all values are pixels_

You can also give a name to a region that will be visible in Sauce Visual UI using `name`

Example:

```java

import com.saucelabs.visual.VisualCheckOptions;
import com.saucelabs.visual.model.Region;

visualClient.sauceVisualCheck("Inventory Page",
VisualCheckOptions.builder()
.ignore(Region.builder()
.name("Demo region")
.x(100)
.y(100)
.width(200)
.height(200)
.build())
.build());
```

### Capturing the DOM snapshot

Check warning on line 204 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L204

[sauce.Headings] 'Capturing the DOM snapshot' should use title case capitalization.
Raw output
{"message": "[sauce.Headings] 'Capturing the DOM snapshot' should use title case capitalization.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 204, "column": 5}}}, "severity": "WARNING"}

Sauce Visual does not capture DOM snapshots by default. This setting can be changed globally via the build options.

Example:
```java
import com.saucelabs.visual.VisualCheckOptions;

static VisualClient visualClient = VisualClient.builder(
sauceUsername,
sauceAccessKey)
.buildName("Espresso Basic Sample")
.captureDom(true)
.build();
```

You can alternatively enable DOM capturing when initating a visual check.

```java
import com.saucelabs.visual.VisualCheckOptions;

visualClient.sauceVisualCheck("Inventory Page",
VisualCheckOptions.builder()
.captureDom(true)
.build());
```

### Clip to an Element

Check warning on line 231 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L231

[sauce.Headings] 'Clip to an Element' should use title case capitalization.
Raw output
{"message": "[sauce.Headings] 'Clip to an Element' should use title case capitalization.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 231, "column": 5}}}, "severity": "WARNING"}

<ClippingDescription />

Example:

```java
import com.saucelabs.visual.VisualCheckOptions;
kb-kerem marked this conversation as resolved.
Show resolved Hide resolved

visualClient.sauceVisualCheck("Visible Sale Banner",
VisualCheckOptions.builder()
.clipElement(withId(R.id.changeTextBt))
.build());
```

## Examples

Click [here](https://github.com/saucelabs/visual-examples/tree/main/espresso) to see the example project.

Check warning on line 248 in docs/visual-testing/integrations/espresso.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/visual-testing/integrations/espresso.md#L248

[sauce.MeaningfulLinkWords] Improve SEO and accessibility by rewriting 'here' in the link text.
Raw output
{"message": "[sauce.MeaningfulLinkWords] Improve SEO and accessibility by rewriting 'here' in the link text.", "location": {"path": "docs/visual-testing/integrations/espresso.md", "range": {"start": {"line": 248, "column": 8}}}, "severity": "WARNING"}
1 change: 1 addition & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,7 @@ module.exports = {
'visual-testing/integrations/python',
'visual-testing/integrations/python-robot-framework',
'visual-testing/integrations/playwright',
'visual-testing/integrations/espresso'
],
},
'visual-testing/cli',
Expand Down