Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Last active March 31, 2020 21:59
Show Gist options
  • Save sauceaaron/ff9f2aef876a761be041e685d072ecf3 to your computer and use it in GitHub Desktop.
Save sauceaaron/ff9f2aef876a761be041e685d072ecf3 to your computer and use it in GitHub Desktop.
Check for tests with errors
<?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>org.example</groupId>
<artifactId>SauceTestWithErrorChecking</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.15.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>saucerest</artifactId>
<version>1.0.44</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
</project>
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class SauceTestInfo
{
private static Gson gson = new GsonBuilder().create();
public String id;
public String status;
public String consolidated_status;
public String passed;
public String error;
public Integer creation_time;
public Integer start_time;
public Integer modification_time;
public Integer end_time;
public Integer commands_not_successful;
public String owner;
public String name;
public String build;
public List<String> tags;
@SerializedName("custom-data")
public Object custom_data;
public String browser;
public String browser_version;
public String browser_short_version;
public String selenium_version;
public String assigned_tunnel_id;
public String log_url;
public String video_url;
public String video_secret;
@SerializedName("public")
public String publicAccess;
public String automation_backend;
public Boolean manual;
public Boolean proxied;
public Boolean breakpointed;
public Boolean collects_automator_log;
public Boolean record_screenshots;
public Boolean record_video;
public String os;
public static TestInfo fromJson(String json)
{
return gson.fromJson(json, TestInfo.class);
}
public String toString()
{
return gson.toJson(this);
}
}
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.saucelabs.saucerest.SauceREST;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
import static org.assertj.core.api.Assertions.assertThat;
public class TestWithErrorCheck
{
static String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");
static String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
static String SAUCE_URL = "https://" + SAUCE_USERNAME + ":" + SAUCE_ACCESS_KEY + "@" + "ondemand.saucelabs.com/wd/hub";
static boolean PASSED = true;
static boolean FAILED = false;
RemoteWebDriver driver;
String sessionId;
SauceREST api;
Boolean testResult = null;
SauceTestInfo testInfo;
String testError;
@Before
public void setup() throws MalformedURLException
{
URL url = new URL(SAUCE_URL);
Capabilities capabilities = getCapabilities();
driver = new RemoteWebDriver(url, capabilities);
api = new SauceREST(SAUCE_USERNAME, SAUCE_ACCESS_KEY);
}
@Test
public void testWithError()
{
// execute test steps
try
{
driver.get("https://www.saucedemo.com/");
String title = driver.getTitle();
System.out.println(title);
if (! title.isEmpty()) { throw new Exception("This is a fake exception"); }
assertThat(title).isEqualTo("Sauce Labs");
}
// handle an actual assertion failure
catch (AssertionError e)
{
testResult = FAILED;
e.printStackTrace();
throw(e);
}
// something happened that is not an assertion failure
catch (Exception e)
{
e.printStackTrace();
testError = e.getMessage();
}
}
@After
public void cleanup() throws InterruptedException
{
sessionId = driver.getSessionId().toString();
try
{
// simulate an error
if (! sessionId.isEmpty()) { throw new RuntimeException("Fake exception closing session"); }
driver.quit();
}
finally
{
updateTestResult(sessionId, testResult);
}
}
private MutableCapabilities getCapabilities()
{
MutableCapabilities sauceOptions = new MutableCapabilities();
sauceOptions.setCapability("name", "testWithError");
MutableCapabilities chromeOptions = new ChromeOptions();
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("sauce:options", sauceOptions);
capabilities.setCapability("goog:chromeOptions", chromeOptions);
capabilities.setCapability("platformName", "Windows 10");
capabilities.setCapability("browserVersion", "latest");
return capabilities;
}
private void updateTestResult(String sessionId, Boolean testStatus) throws InterruptedException
{
if (testStatus == null)
{
// PROBABLY AN ERROR OR TIMEOUT
System.out.println("test status is null");
}
else if (testStatus == PASSED)
{
System.out.println("test PASSED");
api.jobPassed(sessionId.toString());
}
else if (testStatus == FAILED)
{
System.out.println("test FAILED");
api.jobFailed(sessionId.toString());
}
else
{
System.out.println("SHOULD NEVER HAVE GOTTEN HERE -- test status unknown");
}
testInfo = getCompletedTestInfo(sessionId);
System.out.println(testInfo);
handleErrors(testInfo.error);
}
public SauceTestInfo getCompletedTestInfo(String sessionId) throws InterruptedException
{
SauceTestInfo info = null;
boolean complete = false;
int timeout = 100;
int interval = 10;
while (! complete && timeout > 0)
{
String jobInfoJSON = api.getJobInfo(sessionId);
info = SauceTestInfo.fromJson(jobInfoJSON);
DocumentContext jsonPath = JsonPath.parse(jobInfoJSON);
String status = jsonPath.read("$.status");
complete = status.equalsIgnoreCase("complete");
Thread.sleep(interval * 1000);
timeout -= interval;
}
return info;
}
public void handleErrors(String testError)
{
if (testError != null)
{
if (testError.contains("Timing out"))
{
System.out.println("Got a timeout error");
}
//... handle other errors: e.g. infrastructure error, etc.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment