Skip to content

Commit

Permalink
[java] clip element support (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-kerem authored Aug 5, 2024
1 parent 2eefeda commit 23e78d3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
18 changes: 18 additions & 0 deletions visual-java/src/main/java/com/saucelabs/visual/CheckOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public CheckOptions(
DiffingOptionsIn diffingOptions,
Boolean captureDom,
String clipSelector,
WebElement clipElement,
FullPageScreenshotConfig fullPageScreenshotConfig) {
this.ignoreElements = ignoreElements;
this.ignoreRegions = ignoreRegions;
Expand All @@ -39,6 +40,7 @@ public CheckOptions(
this.diffingMethod = diffingMethod;
this.captureDom = captureDom;
this.clipSelector = clipSelector;
this.clipElement = clipElement;
this.fullPageScreenshotConfig = fullPageScreenshotConfig;
this.diffingOptions = diffingOptions;
}
Expand All @@ -53,6 +55,7 @@ public CheckOptions(
private DiffingOptionsIn diffingOptions;
private Boolean captureDom;
private String clipSelector;
private WebElement clipElement;
private FullPageScreenshotConfig fullPageScreenshotConfig;

public static class Builder {
Expand All @@ -65,6 +68,7 @@ public static class Builder {
private DiffingOptionsIn diffingOptions;
private Boolean captureDom;
private String clipSelector;
private WebElement clipElement;
private FullPageScreenshotConfig fullPageScreenshotConfig;

public Builder withIgnoreElements(List<WebElement> ignoreElements) {
Expand Down Expand Up @@ -107,6 +111,11 @@ public Builder withFullPageConfig(FullPageScreenshotConfig fullPageScreenshotCon
return this;
}

public Builder withClipElement(WebElement clipElement) {
this.clipElement = clipElement;
return this;
}

public Builder disableOnly(EnumSet<DiffingFlag> flags) {
this.diffingOptions = new DiffingOptionsIn();
DiffingFlag.setAll(this.diffingOptions, true);
Expand Down Expand Up @@ -144,6 +153,7 @@ public CheckOptions build() {
diffingOptions,
captureDom,
clipSelector,
clipElement,
fullPageScreenshotConfig);
}
}
Expand Down Expand Up @@ -216,6 +226,14 @@ public void setClipSelector(String clipSelector) {
this.clipSelector = clipSelector;
}

public WebElement getClipElement() {
return clipElement;
}

public void setClipElement(WebElement clipElement) {
this.clipElement = clipElement;
}

public FullPageScreenshotConfig getFullPageScreenshotConfig() {
return fullPageScreenshotConfig;
}
Expand Down
15 changes: 10 additions & 5 deletions visual-java/src/main/java/com/saucelabs/visual/VisualApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
Expand Down Expand Up @@ -115,6 +116,7 @@ public VisualApi build() {
private Boolean captureDom;
private FullPageScreenshotConfig fullPageScreenshotConfig;
private String sessionMetadataBlob;
private final RemoteWebDriver driver;

/**
* Creates a VisualApi instance for a given Visual Backend {@link DataCenter}
Expand Down Expand Up @@ -181,11 +183,12 @@ public VisualApi(
this.jobId = jobIdString == null ? sessionId : jobIdString;
this.build = VisualBuild.getBuildOnce(this, buildAttributes);
this.sessionMetadataBlob = this.webdriverSessionInfo().blob;
this.driver = driver;
}

VisualApi(
String jobId,
String sessionId,
RemoteWebDriver driver,
VisualBuild build,
String sessionMetadataBlob,
String url,
Expand All @@ -201,7 +204,8 @@ public VisualApi(
}
this.build = build;
this.jobId = jobId;
this.sessionId = sessionId;
this.sessionId = driver.getSessionId().toString();
this.driver = driver;
this.client = new GraphQLClient(url, username, accessKey);
this.sessionMetadataBlob = sessionMetadataBlob;
}
Expand Down Expand Up @@ -403,9 +407,10 @@ public void sauceVisualCheck(String snapshotName, CheckOptions options) {
input.setCaptureDom(captureDom);
}

String clipSelector = options.getClipSelector();
if (clipSelector != null) {
input.setClipSelector(clipSelector);
if (options.getClipElement() != null) {
input.setClipElement(options.getClipElement());
} else if (options.getClipSelector() != null) {
input.setClipElement(this.driver.findElement(By.cssSelector(options.getClipSelector())));
}

FullPageScreenshotConfig fullPageScreenshotConfig =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;

public class CreateSnapshotFromWebDriverMutation implements GraphQLOperation {
public static final String OPERATION_DOCUMENT =
Expand Down Expand Up @@ -43,6 +45,8 @@ public static class CreateSnapshotFromWebDriverIn {

public Optional<String> clipSelector = Optional.empty();

public Optional<String> clipElement = Optional.empty();

public Optional<FullPageScreenshotConfig> fullPageConfig = Optional.empty();

public Optional<DiffingOptionsIn> diffingOptions = Optional.empty();
Expand Down Expand Up @@ -84,6 +88,10 @@ public void setClipSelector(String clipSelector) {
this.clipSelector = Optional.of(clipSelector);
}

public void setClipElement(WebElement clipElement) {
this.clipElement = Optional.of(((RemoteWebElement) clipElement).getId());
}

public void setFullPageConfig(FullPageScreenshotConfig fullPageConfig) {
this.fullPageConfig = Optional.ofNullable(fullPageConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class VisualApiTest {
void sauceVisualResultsTest() throws IllegalAccessException {
VisualApi api =
new VisualApi(
"", "", new VisualBuild(null, null, null, null, null, null), "", "", "u", "k");
"", null, new VisualBuild(null, null, null, null, null, null), "", "", "u", "k");
GraphQLClient mockGraphQLClient = mock(GraphQLClient.class);
when(mockGraphQLClient.execute(any(), any()))
.thenReturn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.saucelabs.saucebindings.UnhandledPromptBehavior;
import com.saucelabs.saucebindings.junit5.SauceBaseTest;
import com.saucelabs.saucebindings.options.SauceOptions;
import com.saucelabs.visual.CheckOptions;
import com.saucelabs.visual.VisualApi;
import com.saucelabs.visual.junit5.TestMetaInfoExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.By;

@ExtendWith({TestMetaInfoExtension.class})
class LoginPageIT extends SauceBaseTest {
Expand All @@ -25,7 +27,12 @@ void checkLoginPage() {
VisualApi visual =
new VisualApi(driver, System.getenv("SAUCE_USERNAME"), System.getenv("SAUCE_ACCESS_KEY"));
driver.get("https://www.saucedemo.com");
visual.sauceVisualCheck("Login page");
visual.sauceVisualCheck(
"Login page",
new CheckOptions.Builder()
.withClipElement(
driver.findElement(By.cssSelector("input[data-test=\"login-button\"]")))
.build());
System.out.println("Sauce Visual: " + visual.getBuild().getUrl());
}
}

0 comments on commit 23e78d3

Please sign in to comment.