Skip to content

Commit

Permalink
[Java] Add backend WebElement support (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
omacranger authored May 29, 2024
1 parent 6cd6c01 commit e683b4a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
2 changes: 2 additions & 0 deletions visual-java/src/main/graphql/visual/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,8 @@ type Snapshot implements Node {
By convention, the following errors exist:
- `{"code": "TRUNCATED"}`: The image file is corrupt and was probably truncated.
- `{"code": "IMAGE_TOO_LARGE"}`: The image file exceeds the resolution / filesize limits for the diffing service.
- `{"code": "INVALID"}`: The image file is invalid or in an unsupported image format.
- `{"domCode": "DOM_TOO_LARGE"}`: [WARNING] The uploaded DOM is too large.
- `{"domCode": "DOM_MISSING"}`: [WARNING] A DOM snapshot was requested, but could not be captured.
- `{"domCode": "DOM_INVALID"}`: [WARNING] The DOM snapshot has an invalid structure.
Expand Down
35 changes: 25 additions & 10 deletions visual-java/src/main/java/com/saucelabs/visual/VisualApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.stream.Collectors;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -356,6 +357,7 @@ public void sauceVisualCheck(String snapshotName, CheckOptions options) {
diffingMethod,
Optional.ofNullable(options.getDiffingOptions()),
extractIgnoreList(options),
extractIgnoreElements(options),
this.jobId,
snapshotName,
this.sessionId,
Expand Down Expand Up @@ -479,23 +481,13 @@ private List<RegionIn> extractIgnoreList(CheckOptions options) {
return Collections.emptyList();
}

List<WebElement> ignoredElements =
options.getIgnoreElements() == null ? Arrays.asList() : options.getIgnoreElements();

List<IgnoreRegion> ignoredRegions =
options.getIgnoreRegions() == null ? Arrays.asList() : options.getIgnoreRegions();

List<VisualRegion> visualRegions =
options.getIgnoreRegions() == null ? Arrays.asList() : options.getRegions();

List<RegionIn> result = new ArrayList<>();
for (int i = 0; i < ignoredElements.size(); i++) {
WebElement element = ignoredElements.get(i);
if (validate(element) == null) {
throw new VisualApiException("options.ignoreElement[" + i + "] does not exist (yet)");
}
result.add(VisualRegion.ignoreChangesFor(element).toRegionIn());
}
for (int i = 0; i < ignoredRegions.size(); i++) {
IgnoreRegion ignoreRegion = ignoredRegions.get(i);
if (validate(ignoreRegion) == null) {
Expand All @@ -520,13 +512,36 @@ private List<RegionIn> extractIgnoreList(CheckOptions options) {
return result;
}

private List<ElementIn> extractIgnoreElements(CheckOptions options) {
List<WebElement> ignoredElements = options != null && options.getIgnoreElements() != null
? options.getIgnoreElements()
: Arrays.asList();

List<ElementIn> result = new ArrayList<>();
for (int i = 0; i < ignoredElements.size(); i++) {
WebElement element = ignoredElements.get(i);
if (validateRemoteElement(element) == null) {
throw new VisualApiException("options.ignoreElement[" + i + "] does not exist (yet)");
}
result.add(VisualRegion.ignoreChangesFor(element).toElementIn());
}
return result;
}

private WebElement validate(WebElement element) {
if (element == null || !element.isDisplayed() || element.getRect() == null) {
return null;
}
return element;
}

private WebElement validateRemoteElement(WebElement element) {
if (element != null && element instanceof RemoteWebElement && element.isDisplayed()) {
return element;
}
return null;
}

private IgnoreRegion validate(IgnoreRegion region) {
if (region == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.saucelabs.visual.graphql.type.DiffingOptionsIn;
import com.saucelabs.visual.graphql.type.DiffsConnection;
import com.saucelabs.visual.graphql.type.RegionIn;
import com.saucelabs.visual.graphql.type.ElementIn;
import com.saucelabs.visual.model.FullPageScreenshotConfig;
import java.util.Collections;
import java.util.List;
Expand All @@ -24,6 +25,8 @@ public static class CreateSnapshotFromWebDriverIn {

public final List<RegionIn> ignoreRegions;

public final List<ElementIn> ignoreElements;

public final String jobId;

public final String name;
Expand All @@ -49,6 +52,7 @@ public CreateSnapshotFromWebDriverIn(
DiffingMethod diffingMethod,
Optional<DiffingOptionsIn> diffingOptions,
List<RegionIn> ignoreRegions,
List<ElementIn> ignoreElements,
String jobId,
String name,
String sessionId,
Expand All @@ -57,6 +61,7 @@ public CreateSnapshotFromWebDriverIn(
this.diffingMethod = diffingMethod;
this.diffingOptions = diffingOptions;
this.ignoreRegions = ignoreRegions;
this.ignoreElements = ignoreElements;
this.jobId = jobId;
this.name = name;
this.sessionId = sessionId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.saucelabs.visual.model;

import com.saucelabs.visual.graphql.type.DiffingOptionsIn;
import com.saucelabs.visual.graphql.type.ElementIn;
import com.saucelabs.visual.graphql.type.RegionIn;
import java.util.EnumSet;
import org.openqa.selenium.Rectangle;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;

public class VisualRegion {
private DiffingOptionsIn options;
Expand Down Expand Up @@ -165,4 +167,14 @@ public RegionIn toRegionIn() {

return r;
}

public ElementIn toElementIn() {
ElementIn e = new ElementIn();

e.setName(this.name);
e.setDiffingOptions(this.options);
e.setId(((RemoteWebElement) this.element).getId());

return e;
}
}

0 comments on commit e683b4a

Please sign in to comment.