Skip to content

Commit

Permalink
[java] Support SAUCE_REGION (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-kerem authored Jul 4, 2024
1 parent 7057d7d commit 9e8fe86
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
18 changes: 18 additions & 0 deletions visual-java/src/main/java/com/saucelabs/visual/DataCenter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.saucelabs.visual;

import com.saucelabs.visual.exception.VisualApiException;

public enum DataCenter {
US_WEST_1("https://api.us-west-1.saucelabs.com/v1/visual/graphql"),
US_EAST_4("https://api.us-east-4.saucelabs.com/v1/visual/graphql"),
Expand All @@ -10,4 +12,20 @@ public enum DataCenter {
DataCenter(String endpoint) {
this.endpoint = endpoint;
}

static DataCenter fromSauceRegion(String sauceRegion) {
if (sauceRegion == null) {
return US_WEST_1;
}
switch (sauceRegion) {
case "us-west-1":
return US_WEST_1;
case "eu-central-1":
return EU_CENTRAL_1;
case "us-east-4":
return US_EAST_4;
default:
throw new VisualApiException("Unknown region: " + sauceRegion);
}
}
}
27 changes: 25 additions & 2 deletions visual-java/src/main/java/com/saucelabs/visual/VisualApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
public class VisualApi {
private static final Logger log = LoggerFactory.getLogger(VisualApi.class);

private static String resolveEndpoint() {
return DataCenter.fromSauceRegion(EnvironmentVariables.SAUCE_REGION).endpoint;
}

/** Creates a VisualApi instance using builder style */
public static class Builder {
private final RemoteWebDriver driver;
Expand All @@ -39,7 +43,7 @@ public static class Builder {
private FullPageScreenshotConfig fullPageScreenshotConfig;

public Builder(RemoteWebDriver driver, String username, String accessKey) {
this(driver, username, accessKey, DataCenter.US_WEST_1.endpoint);
this(driver, username, accessKey, resolveEndpoint());
}

public Builder(RemoteWebDriver driver, String username, String accessKey, DataCenter region) {
Expand Down Expand Up @@ -112,6 +116,17 @@ public VisualApi build() {
private FullPageScreenshotConfig fullPageScreenshotConfig;
private String sessionMetadataBlob;

/**
* Creates a VisualApi instance for a given Visual Backend {@link DataCenter}
*
* @param driver The {@link org.openqa.selenium.WebDriver} instance where the tests should run at
* @param username SauceLabs username
* @param accessKey SauceLabs access key
*/
public VisualApi(RemoteWebDriver driver, String username, String accessKey) {
this(driver, resolveEndpoint(), username, accessKey);
}

/**
* Creates a VisualApi instance for a given Visual Backend {@link DataCenter}
*
Expand Down Expand Up @@ -218,7 +233,15 @@ private WebdriverSessionInfoQuery.Result webdriverSessionInfo() {
WebdriverSessionInfoQuery query =
new WebdriverSessionInfoQuery(
new WebdriverSessionInfoQuery.WebdriverSessionInfoIn(this.jobId, this.sessionId));
return this.client.execute(query, WebdriverSessionInfoQuery.Data.class).result;
try {
WebdriverSessionInfoQuery.Data response =
this.client.execute(query, WebdriverSessionInfoQuery.Data.class);
return response.result;
} catch (VisualApiException e) {
log.error(
"Sauce Visual: No WebDriver session found. Please make sure WebDriver and Sauce Visual data centers are aligned.");
throw e;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public <D> D execute(GraphQLOperation operation, Class<D> responseType)

JsonNode rootNode = objectMapper.readTree(responseString);
JsonNode dataField = rootNode.get("data");
JsonNode errors = rootNode.get("errors");

if (errors != null) {
JsonNode message = errors.findValue("message");
if (message != null) {
throw new VisualApiException(message.asText());
} else {
throw new VisualApiException("Unexpected error");
}
}

return objectMapper.treeToValue(dataField, responseType);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public class EnvironmentVariables {

private EnvironmentVariables() {}

public static final String SAUCE_REGION = System.getenv("SAUCE_REGION");

public static final String PROJECT_NAME = System.getenv("SAUCE_VISUAL_PROJECT");
public static final String BRANCH_NAME = System.getenv("SAUCE_VISUAL_BRANCH");
public static final String DEFAULT_BRANCH_NAME = System.getenv("SAUCE_VISUAL_DEFAULT_BRANCH");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.saucelabs.saucebindings.UnhandledPromptBehavior;
import com.saucelabs.saucebindings.junit5.SauceBaseTest;
import com.saucelabs.saucebindings.options.SauceOptions;
import com.saucelabs.visual.DataCenter;
import com.saucelabs.visual.VisualApi;
import com.saucelabs.visual.junit5.TestMetaInfoExtension;
import org.junit.jupiter.api.Test;
Expand All @@ -24,11 +23,7 @@ public SauceOptions createSauceOptions() {
@Test
void checkLoginPage() {
VisualApi visual =
new VisualApi(
driver,
DataCenter.US_WEST_1,
System.getenv("SAUCE_USERNAME"),
System.getenv("SAUCE_ACCESS_KEY"));
new VisualApi(driver, System.getenv("SAUCE_USERNAME"), System.getenv("SAUCE_ACCESS_KEY"));
driver.get("https://www.saucedemo.com");
visual.sauceVisualCheck("Login page");
System.out.println("Sauce Visual: " + visual.getBuild().getUrl());
Expand Down

0 comments on commit 9e8fe86

Please sign in to comment.