Skip to content

Commit

Permalink
Use the new SauceREST APIs (#158)
Browse files Browse the repository at this point in the history
* Use the new SauceREST APIs

Co-authored-by: Valery Yatsynovich <valery_yatsynovich@epam.com>
  • Loading branch information
alexh-sauce and valfirst authored Jan 19, 2023
1 parent 2445ed7 commit b91eed2
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>saucerest</artifactId>
<version>1.2.0</version>
<version>2.0.2</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
65 changes: 48 additions & 17 deletions src/main/java/com/saucelabs/ci/BrowserFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
package com.saucelabs.ci;

import com.saucelabs.saucerest.SauceREST;
import com.saucelabs.saucerest.model.platform.Platform;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;
import java.util.logging.Level;
Expand Down Expand Up @@ -43,19 +45,19 @@ public BrowserFactory() {

public BrowserFactory(SauceREST sauceREST) {
if (sauceREST == null) {
this.sauceREST = new SauceREST(null, null);
this.sauceREST = new SauceREST(null, null, "US_WEST");
} else {
this.sauceREST = sauceREST;
}
try {
initializeWebDriverBrowsers();
initializeAppiumBrowsers();
} catch (JSONException e) {
} catch (JSONException | IOException e) {
logger.log(Level.WARNING, "Error retrieving browsers, attempting to continue", e);
}
}

public List<Browser> getAppiumBrowsers() throws JSONException {
public List<Browser> getAppiumBrowsers() throws JSONException, IOException {
List<Browser> browsers;
if (shouldRetrieveBrowsers()) {
browsers = initializeAppiumBrowsers();
Expand All @@ -67,7 +69,7 @@ public List<Browser> getAppiumBrowsers() throws JSONException {
return browsers;
}

public List<Browser> getWebDriverBrowsers() throws JSONException {
public List<Browser> getWebDriverBrowsers() throws JSONException, IOException {
List<Browser> browsers;
if (shouldRetrieveBrowsers()) {
browsers = initializeWebDriverBrowsers();
Expand All @@ -83,7 +85,7 @@ public boolean shouldRetrieveBrowsers() {
return lastLookup == null || CacheTimeUtil.pastAcceptableDuration(lastLookup, ONE_HOUR_IN_MILLIS);
}

private List<Browser> initializeAppiumBrowsers() throws JSONException {
private List<Browser> initializeAppiumBrowsers() throws JSONException, IOException {
List<Browser> browsers = getAppiumBrowsersFromSauceLabs();
appiumLookup = new HashMap<>();
for (Browser browser : browsers) {
Expand All @@ -93,7 +95,7 @@ private List<Browser> initializeAppiumBrowsers() throws JSONException {
return browsers;
}

private List<Browser> initializeWebDriverBrowsers() throws JSONException {
private List<Browser> initializeWebDriverBrowsers() throws JSONException, IOException {
List<Browser> browsers = getWebDriverBrowsersFromSauceLabs();
webDriverLookup = new HashMap<>();
for (Browser browser : browsers) {
Expand All @@ -103,20 +105,49 @@ private List<Browser> initializeWebDriverBrowsers() throws JSONException {
return browsers;
}

private List<Browser> getWebDriverBrowsersFromSauceLabs() throws JSONException {
String response = sauceREST.getSupportedPlatforms("webdriver");
if (response.equals("")) {
response = "[]";
}
return getBrowserListFromJson(response);
private List<Browser> getWebDriverBrowsersFromSauceLabs() throws IOException {
List<Platform> platforms = sauceREST.getPlatform().getSupportedPlatforms("webdriver").platforms;
return getBrowserListFromPlatforms(platforms);
}

private List<Browser> getAppiumBrowsersFromSauceLabs() throws IOException {
List<Platform> platforms = sauceREST.getPlatform().getSupportedPlatforms("appium").platforms;
return getBrowserListFromPlatforms(platforms);
}

private List<Browser> getAppiumBrowsersFromSauceLabs() throws JSONException {
String response = sauceREST.getSupportedPlatforms("appium");
if (response.equals("")) {
response = "[]";
public List<Browser> getBrowserListFromPlatforms(List<Platform> platforms) {
List<Browser> browsers = new ArrayList<Browser>();

for (Platform platform: platforms) {
String seleniumName = platform.apiName;

if (IEHTA.equals(seleniumName)) {
//exclude these browsers from the list, as they replicate iexplore and firefox
continue;
}

String longName = platform.longName;
String longVersion = platform.longVersion;
String shortVersion = platform.shortVersion;
String osName = platform.os;

if (platform.device != null && !platform.device.isEmpty()) {
// Appium
String device = longName;
String deviceType = null;
osName = platform.apiName; //use api_name instead of os, as os was returning Linux/Mac OS

browsers.add(createDeviceBrowser(seleniumName, longName, longVersion, osName, device, deviceType, shortVersion, "portrait"));
browsers.add(createDeviceBrowser(seleniumName, longName, longVersion, osName, device, deviceType, shortVersion, "landscape"));
continue;
}

// Webdriver
browsers.add(createBrowserBrowser(seleniumName, longName, "latest", osName, "latest"));
browsers.add(createBrowserBrowser(seleniumName, longName, longVersion, osName, shortVersion));
}
return getBrowserListFromJson(response);

return browsers;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ protected Process createProcess(String[] args, File directory) throws IOExceptio
* @throws SauceConnectException thrown if an error occurs launching Sauce Connect
*/
public Process openConnection(String username, String apiKey, int port, File sauceConnectJar, String options, PrintStream printStream, Boolean verboseLogging, String sauceConnectPath) throws SauceConnectException {
return openConnection(username, apiKey, "US", port, sauceConnectJar, options, printStream, verboseLogging, sauceConnectPath);
return openConnection(username, apiKey, "US_WEST", port, sauceConnectJar, options, printStream, verboseLogging, sauceConnectPath);
}

/**
Expand Down
48 changes: 38 additions & 10 deletions src/test/java/com/saucelabs/ci/BrowserFactoryTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.saucelabs.ci;

import com.saucelabs.saucerest.SauceREST;
import com.saucelabs.saucerest.model.platform.Platform;
import com.saucelabs.saucerest.model.platform.SupportedPlatforms;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
Expand All @@ -21,20 +25,44 @@ public class BrowserFactoryTest {

@Before
public void setUp() throws Exception {

// Mock appium response
List<Platform> appiumPlatforms = new ArrayList<Platform>();

Platform kindle = new Platform("2.3", "Amazon Kindle Fire Emulator", "android", "2.3.7.", "2.3.7.", "appium", "android", null, null, null, "Amazon Kindle Fire Emulator");
Platform kindle2 = new Platform("2.4", "Amazon Kindle Fire Emulator", "android", "2.4.7.", "2.4.7.", "appium", "android", null, null, null, "Amazon Kindle Fire Emulator");

appiumPlatforms.add(kindle);

// Mock webdriver response
List<Platform> webdriverPlatforms = new ArrayList<Platform>();
Platform kindleHD = new Platform("4.0", "Amazon Kindle Fire HD 8.9 Emulator", "android", "4.0.4.", "4.0.4.", "appium", "android", null, null, null, "Amazon Kindle Fire HD 8.9 Emulator");
Platform linux = new Platform("4", "Firefox", "firefox", "4.0.1.", null, null, "Linux", null, null, null, null);
webdriverPlatforms.add(kindleHD);
webdriverPlatforms.add(linux);

SauceREST sauceREST = mock(SauceREST.class);
com.saucelabs.saucerest.api.Platform mockPlatform = mock(com.saucelabs.saucerest.api.Platform.class);

when(
sauceREST.getSupportedPlatforms("appium")
).thenReturn(IOUtils.toString(this.getClass().getResourceAsStream("/appium.json"), UTF_8));
sauceREST.getPlatform()
).thenReturn(mockPlatform);

when(
mockPlatform.getSupportedPlatforms("appium")
).thenReturn(new SupportedPlatforms(appiumPlatforms));

when(
sauceREST.getSupportedPlatforms("webdriver")
).thenReturn(IOUtils.toString(this.getClass().getResourceAsStream("/webdriver.json"), UTF_8));
mockPlatform.getSupportedPlatforms("webdriver")
).thenReturn(new SupportedPlatforms(webdriverPlatforms));

this.browserFactory = new BrowserFactory(sauceREST);
}

@Test
public void testGetAppiumBrowsers() {
public void testGetAppiumBrowsers() throws IOException {
List<Browser> browsers = this.browserFactory.getAppiumBrowsers();
assertEquals(176, browsers.size());
assertEquals(2, browsers.size());

int elm = 0;
assertEquals("Amazon_Kindle_Fire_Emulatorlandscapeandroid2_3_7_", browsers.get(elm).getKey());
Expand Down Expand Up @@ -62,9 +90,9 @@ public void testGetAppiumBrowsers() {
}

@Test
public void testGetWebDriverBrowsers() {
public void testGetWebDriverBrowsers() throws IOException {
List<Browser> browsers = this.browserFactory.getWebDriverBrowsers();
assertEquals(801, browsers.size());
assertEquals(4, browsers.size());

int elm = 0;
assertEquals("Amazon_Kindle_Fire_HD_8_9_Emulatorlandscapeandroid4_0_4_", browsers.get(elm).getKey());
Expand All @@ -90,7 +118,7 @@ public void testGetWebDriverBrowsers() {
assertEquals(null, browsers.get(elm).getDeviceType());
assertEquals("portrait", browsers.get(elm).getDeviceOrientation());

elm = 126;
elm = 2;
assertEquals("Linuxfirefox4", browsers.get(elm).getKey());
assertEquals("Linux", browsers.get(elm).getOs());
assertEquals("firefox", browsers.get(elm).getBrowserName());
Expand All @@ -102,7 +130,7 @@ public void testGetWebDriverBrowsers() {
assertEquals(null, browsers.get(elm).getDeviceType());
assertEquals(null, browsers.get(elm).getDeviceOrientation());

elm = 137;
elm = 3;
assertEquals("Linuxfirefoxlatest", browsers.get(elm).getKey());
assertEquals("Linux", browsers.get(elm).getOs());
assertEquals("firefox", browsers.get(elm).getBrowserName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private Process testDefaultOpenConnection(String logFile, String username) throw
private Process testOpenConnection(String logFile, String username) throws IOException {
final String apiKey = "fakeapikey";
final int port = 12345;
final String dataCenter = "US";
final String dataCenter = "US_WEST";

try (InputStream resourceAsStream = getResourceAsStream(logFile)) {
when(mockProcess.getErrorStream()).thenReturn(new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)));
Expand Down
62 changes: 54 additions & 8 deletions src/test/java/com/saucelabs/sod/BrowserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,63 @@
import com.saucelabs.ci.Browser;
import com.saucelabs.ci.BrowserFactory;
import com.saucelabs.saucerest.SauceREST;
import com.saucelabs.saucerest.model.platform.Platform;
import com.saucelabs.saucerest.model.platform.SupportedPlatforms;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.Before;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author Ross Rowe
*/
public class BrowserTest {
private SauceREST sauceREST = new SauceREST(null, null);
private BrowserFactory browserFactory;

@Before
public void setUp() throws Exception {

// Mock appium response
List<Platform> appiumPlatforms = new ArrayList<Platform>();

Platform kindle = new Platform("2.3", "Amazon Kindle Fire Emulator", "android", "2.3.7.", "2.3.7.", "appium", "android", null, null, null, "Amazon Kindle Fire Emulator");
Platform kindle2 = new Platform("2.4", "Amazon Kindle Fire Emulator", "android", "2.4.7.", "2.4.7.", "appium", "android", null, null, null, "Amazon Kindle Fire Emulator");

appiumPlatforms.add(kindle);

// Mock webdriver response
List<Platform> webdriverPlatforms = new ArrayList<Platform>();
Platform kindleHD = new Platform("4.0", "Amazon Kindle Fire HD 8.9 Emulator", "android", "4.0.4.", "4.0.4.", "appium", "android", null, null, null, "Amazon Kindle Fire HD 8.9 Emulator");
Platform linux = new Platform("4", "Firefox", "firefox", "4.0.1.", null, null, "Linux", null, null, null, null);
webdriverPlatforms.add(kindleHD);
webdriverPlatforms.add(linux);

SauceREST sauceREST = mock(SauceREST.class);
com.saucelabs.saucerest.api.Platform mockPlatform = mock(com.saucelabs.saucerest.api.Platform.class);

when(
sauceREST.getPlatform()
).thenReturn(mockPlatform);

when(
mockPlatform.getSupportedPlatforms("appium")
).thenReturn(new SupportedPlatforms(appiumPlatforms));

when(
mockPlatform.getSupportedPlatforms("webdriver")
).thenReturn(new SupportedPlatforms(webdriverPlatforms));

this.browserFactory = new BrowserFactory(sauceREST);
}

@Test
public void testNames() {
Expand All @@ -29,19 +72,22 @@ public void testNames() {
@Test
public void browserList() throws Exception {

BrowserFactory factory = new BrowserFactory(sauceREST);
String browserText = IOUtils.toString(getClass().getResourceAsStream("/appium_browsers.json"), "UTF-8");
List<Browser> browsers = factory.getBrowserListFromJson(browserText);
List<Platform> appiumPlatforms = new ArrayList<Platform>();

Platform kindle = new Platform("2.3", "Amazon Kindle Fire Emulator", "android", "2.3.7.", "2.3.7.", "appium", "android", null, null, null, "Amazon Kindle Fire Emulator");

appiumPlatforms.add(kindle);

List<Browser> browsers = browserFactory.getBrowserListFromPlatforms(appiumPlatforms);
assertFalse("browsers is empty", browsers.isEmpty());

}

@Test
public void browserFromSaucelabs() {
BrowserFactory factory = new BrowserFactory(sauceREST);
List<Browser> browsers = factory.getWebDriverBrowsers();
public void browserFromSaucelabs() throws IOException {
List<Browser> browsers = browserFactory.getWebDriverBrowsers();
assertFalse("browsers is empty", browsers.isEmpty());
browsers = factory.getAppiumBrowsers();
browsers = browserFactory.getAppiumBrowsers();
assertFalse("browsers is empty", browsers.isEmpty());
}

Expand Down

0 comments on commit b91eed2

Please sign in to comment.