Skip to content

Instantly share code, notes, and snippets.

@fijiaaron
Last active March 22, 2019 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fijiaaron/53032776401e6ea633380d9accb882e3 to your computer and use it in GitHub Desktop.
Save fijiaaron/53032776401e6ea633380d9accb882e3 to your computer and use it in GitHub Desktop.
Use GSON to parse Sauce Labs getJobInfo JSON response into a POJO object
package com.saucelabs.api.responses;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class GetJobInfoResponse
{
public String assigned_tunnel_id;
public String automation_backend;
public String breakpointed;
public String browser;
public String browser_short_version;
public String browser_version;
public String build;
public String collects_automator_log;
public int commands_not_successful;
public String consolidated_status;
public int creation_time;
@SerializedName("custom-data")
public String customData;
public int end_time;
public String error;
public String id;
public String log_url;
public boolean manual;
public int modification_time;
public String name;
public String os;
public String owner;
public Boolean passed;
public boolean proxied;
@SerializedName("public")
public String sharing;
public boolean record_screenshots;
public boolean record_video;
public String selenium_version;
public int start_time;
public String status;
public String video_secret;
public String video_url;
public List<String> tags;
}
import com.google.gson.Gson;
import com.saucelabs.api.responses.GetJobInfoResponse;
import com.saucelabs.saucerest.SauceREST;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.TimeZone;
public class SimpleTest
{
static final String SAUCE_URL = "https://ondemand.saucelabs.com/wd/hub";
static final String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");
static final String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
String SELENIUM_PLATFORM = "Windows 10";
String SELENIUM_BROWSER = "Chrome";
String SELENIUM_VERSION = "latest";
URL sauceURL;
DesiredCapabilities capabilities;
RemoteWebDriver driver;
String sessionId;
SauceREST api;
@Rule
public TestName testName = new TestName();
@Before
public void setup() throws MalformedURLException
{
sauceURL = new URL(SAUCE_URL);
capabilities = new DesiredCapabilities();
capabilities.setCapability("platform", SELENIUM_PLATFORM);
capabilities.setCapability("browserName", SELENIUM_BROWSER);
capabilities.setCapability("version", SELENIUM_VERSION);
capabilities.setCapability("username", SAUCE_USERNAME);
capabilities.setCapability("accessKey", SAUCE_ACCESS_KEY);
capabilities.setCapability("name", getTestName());
driver = new RemoteWebDriver(sauceURL, capabilities);
sessionId = driver.getSessionId().toString();
api = new SauceREST(SAUCE_USERNAME, SAUCE_ACCESS_KEY);
}
@After
public void teardown() throws InterruptedException
{
driver.executeScript("sauce:job-result=passed");
if (driver != null) {
driver.quit();
}
Thread.sleep(10000);
checkStatus();
}
public void checkStatus()
{
String json = api.getJobInfo(sessionId);
System.out.println(json);
Gson gson = new Gson();
GetJobInfoResponse jobInfo = gson.fromJson(json, GetJobInfoResponse.class);
System.out.println(jobInfo.name);
System.out.println(jobInfo.status);
// calculate how long the test took to execute
LocalDateTime startTime = fromTimestamp(jobInfo.start_time);
LocalDateTime endTime = fromTimestamp(jobInfo.end_time);
System.out.println(Duration.between(startTime, endTime).getSeconds());
}
@Test
public void openURL()
{
driver = new RemoteWebDriver(sauceURL, capabilities);
driver.get("https://saucelabs.com/");
System.out.println(driver.getSessionId());
}
public String getTestName()
{
return this.getClass().getSimpleName() + " " + testName.getMethodName();
}
public LocalDateTime fromTimestamp(long seconds)
{
return LocalDateTime.ofInstant(Instant.ofEpochSecond(seconds),
TimeZone.getDefault().toZoneId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment