Created
November 19, 2019 19:52
-
-
Save sauceaaron/6f8c9b334d5fd1e1ee9bfa9f78661117 to your computer and use it in GitHub Desktop.
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
| package com.saucelabs.test.results; | |
| import com.google.gson.Gson; | |
| import com.google.gson.GsonBuilder; | |
| 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 SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); | |
| static String SAUCE_ACCESS_KEY = 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; maximum -29d | |
| 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<TestResponse> allErrors = new ArrayList<>(); | |
| List<TestResponse> timeoutErrors = new ArrayList<>(); | |
| List<TestResponse> internalServerErrors = new ArrayList<>(); | |
| List<TestResponse> 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(SAUCE_USERNAME, SAUCE_ACCESS_KEY); | |
| 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<TestResponse> items = jsonPath.getList("items", TestResponse.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()); | |
| // output results of all tests with a particular Error | |
| infrastructureErrors.forEach(test-> { | |
| System.out.println(test.asJson()); | |
| }); | |
| } | |
| public static List<TestResponse> filterByError(List<TestResponse> items, String errorMessage) | |
| { | |
| return items.stream().filter( | |
| test -> test.error.contains(errorMessage) | |
| ).collect(Collectors.toList()); | |
| } | |
| public class TestResponse | |
| { | |
| 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; | |
| public String asJson() | |
| { | |
| Gson gson = new GsonBuilder().create(); | |
| return gson.toJson(this); | |
| } | |
| } | |
| } |
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</groupId> | |
| <artifactId>saucelabs-analytics-api-errors</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>rest-assured</artifactId> | |
| <version>3.3.0</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.saucelabs</groupId> | |
| <artifactId>saucerest</artifactId> | |
| <version>1.0.39</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.google.code.gson</groupId> | |
| <artifactId>gson</artifactId> | |
| <version>2.8.4</version> | |
| <scope>compile</scope> | |
| </dependency> | |
| </dependencies> | |
| </project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment