Created
March 15, 2019 20:23
-
-
Save fijiaaron/6d1ef1ce65c8cb40df2c7307373e2cbf to your computer and use it in GitHub Desktop.
Using the Sauce Labs Analytics REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.mashape.unirest.http.HttpResponse; | |
import com.mashape.unirest.http.Unirest; | |
import com.mashape.unirest.http.exceptions.UnirestException; | |
import com.mashape.unirest.request.HttpRequest; | |
import io.restassured.path.json.JsonPath; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class GetErroredTests | |
{ | |
static String username = System.getenv("SAUCE_USERNAME"); | |
static String accessKey = System.getenv("SAUCE_ACCESS_KEY"); | |
static String TESTS_ENDPOINT = "https://saucelabs.com/rest/v1/analytics/tests"; | |
public static void main(String[] args) throws UnirestException | |
{ | |
String time_range = "-1d"; // -1d, -1h, -1m, -1s | |
String scope = "me"; // me, organization, single | |
String status = "errored"; // errored, complete, passed, failed | |
int from = 0; //starting test sequence | |
int size = 100; | |
int max = 10000; | |
boolean has_more; | |
List<TestResponseItem> allErrors = new ArrayList<>(); | |
List<TestResponseItem> timeoutErrors = new ArrayList<>(); | |
List<TestResponseItem> internalServerErrors = new ArrayList<>(); | |
List<TestResponseItem> infrastructureErrors = new ArrayList<>(); | |
do | |
{ | |
HashMap<String, Object> parameters = new HashMap<>(); | |
parameters.put("time_range", time_range); | |
parameters.put("scope", scope); | |
parameters.put("status", status); | |
parameters.put("pretty", true); | |
parameters.put("size", size); | |
parameters.put("from", from); | |
HttpRequest request = Unirest.get(TESTS_ENDPOINT) | |
.queryString(parameters) | |
.basicAuth(username, accessKey); | |
System.out.println("request URL: " + request.getUrl()); | |
HttpResponse<String> response = request.asString(); | |
System.out.println("status: " + response.getStatus() + " " + response.getStatusText()); | |
String body = response.getBody(); | |
System.out.println("body:" + body); | |
JsonPath jsonPath = JsonPath.from(body); | |
has_more = jsonPath.getBoolean(("has_more")); | |
List<TestResponseItem> items = jsonPath.getList("items", TestResponseItem.class); | |
// collect all errored tests | |
allErrors.addAll((items)); | |
// filter and collect timeout errors | |
timeoutErrors.addAll(filterByError(items, "Test did not see a new command")); | |
// filter and collect internal server errors | |
internalServerErrors.addAll(filterByError(items, "Internal Server Error")); | |
// filter and collect infrastructure errors | |
infrastructureErrors.addAll(filterByError(items, "Infrastructure Error")); | |
// get the next 100 tests until all are collected or max is achieved | |
from+= size; | |
System.out.println("has_more: " + has_more); | |
System.out.println("current set: " + items.size()); | |
} | |
while (has_more && from <= max); | |
System.out.println("allErrors: " + allErrors.size()); | |
System.out.println("timeoutErrors: " + timeoutErrors.size()); | |
System.out.println("internalServerErrors: " + internalServerErrors.size()); | |
System.out.println("infrastructureErrors: " + infrastructureErrors.size()); | |
} | |
public static List<TestResponseItem> filterByError(List<TestResponseItem> items, String errorMessage) | |
{ | |
return items.stream().filter( | |
test -> test.error.contains(errorMessage) | |
).collect(Collectors.toList()); | |
} | |
public class TestResponseItem | |
{ | |
public String id; | |
public String owner; | |
public String ancestor; | |
public String name; | |
public String build; | |
public String creation_time; | |
public String start_time; | |
public String end_time; | |
public String duration; | |
public String status; | |
public String error; | |
public String os; | |
public String os_normalized; | |
public String browser; | |
public String browser_normalized; | |
public String details_url; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.saucelabs.example</groupId> | |
<artifactId>AnalyticsAPIExample</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>8</source> | |
<target>8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<dependency> | |
<groupId>com.mashape.unirest</groupId> | |
<artifactId>unirest-java</artifactId> | |
<version>1.4.9</version> | |
</dependency> | |
<dependency> | |
<groupId>io.rest-assured</groupId> | |
<artifactId>json-path</artifactId> | |
<version>3.3.0</version> | |
</dependency> | |
<dependency> | |
<groupId>com.google.code.gson</groupId> | |
<artifactId>gson</artifactId> | |
<version>2.8.4</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment