Created
August 12, 2019 23:19
-
-
Save fijiaaron/9ec7668e4866738752d7bfc8076cf3d1 to your computer and use it in GitHub Desktop.
Update failing test with custom data about the failure
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>example-test</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>org.seleniumhq.selenium</groupId> | |
<artifactId>selenium-java</artifactId> | |
<version>3.4.0</version> | |
</dependency> | |
<dependency> | |
<groupId>com.saucelabs</groupId> | |
<artifactId>saucerest</artifactId> | |
<version>1.0.42</version> | |
</dependency> | |
<dependency> | |
<groupId>org.assertj</groupId> | |
<artifactId>assertj-core</artifactId> | |
<version>3.13.2</version> | |
</dependency> | |
<dependency> | |
<groupId>org.testng</groupId> | |
<artifactId>testng</artifactId> | |
<version>6.14.3</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
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.saucelabs.saucerest.SauceREST; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
import org.openqa.selenium.remote.RemoteWebDriver; | |
import org.testng.ITestResult; | |
import org.testng.annotations.AfterMethod; | |
import org.testng.annotations.BeforeMethod; | |
import org.testng.annotations.Test; | |
import java.lang.reflect.Method; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.Map; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class UpdateTest | |
{ | |
private final static String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); | |
private final static String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY"); | |
private final static String BUILD_TAG = System.getenv("BUILD_TAG"); | |
private RemoteWebDriver driver; | |
private String sessionId; | |
private SauceREST api; | |
@BeforeMethod | |
public void setup(Method method) throws MalformedURLException | |
{ | |
URL url = new URL("https://ondemand.saucelabs.com/wd/hub"); | |
DesiredCapabilities capabilities = new DesiredCapabilities(); | |
capabilities.setCapability("platform", "Windows 10"); | |
capabilities.setCapability("browserName", "Internet Explorer"); | |
capabilities.setCapability("version", "latest"); | |
capabilities.setCapability("username", SAUCE_USERNAME); | |
capabilities.setCapability("accessKey", SAUCE_ACCESS_KEY); | |
capabilities.setCapability("name", method.getName()); | |
capabilities.setCapability("build", BUILD_TAG); | |
capabilities.setCapability("tags", Arrays.asList("foo", "bar", "yazoo")); | |
driver = new RemoteWebDriver(url, capabilities); | |
sessionId = driver.getSessionId().toString(); | |
api = new SauceREST(SAUCE_USERNAME, SAUCE_ACCESS_KEY); | |
driver.get("https://www.saucedemo.com"); | |
} | |
@AfterMethod | |
public void cleanup(ITestResult result) | |
{ | |
if (driver != null) | |
{ | |
driver.quit(); | |
} | |
if (result != null) | |
{ | |
if (result.isSuccess()) | |
{ | |
api.jobPassed(sessionId); | |
} else | |
{ | |
api.jobFailed(sessionId); | |
HashMap<String, Object> failure = new HashMap<>(); | |
failure.put("failing-test", result.getInstanceName() + "." + result.getName()); | |
failure.put("assertion-error", result.getThrowable().getMessage()); | |
addCustomData(failure); | |
} | |
} | |
} | |
@Test | |
public void getTitleFails() | |
{ | |
assertThat(driver.getTitle()).startsWith("Sauce Labs"); | |
} | |
@Test | |
public void getTitlePasses() | |
{ | |
assertThat(driver.getTitle()).startsWith("Swag Labs"); | |
} | |
public void addCustomData(HashMap<String, Object> customData) | |
{ | |
Map<String, Object> jobInfo = new HashMap(); | |
jobInfo.put("custom-data", customData); | |
api.updateJobInfo(sessionId, jobInfo); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment