Last active
March 2, 2020 23:35
-
-
Save sauceaaron/5067697f8961134c9d0fa19a35e1a96b to your computer and use it in GitHub Desktop.
Demonstrate how to update test results with JS Executor and Sauce 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.saucelabs.saucerest.SauceREST; | |
import org.junit.After; | |
import org.junit.Test; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
import org.openqa.selenium.remote.RemoteWebDriver; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class HelloCostcoTravel | |
{ | |
boolean passed; | |
String username = System.getenv("SAUCE_USERNAME"); | |
String accessKey = System.getenv("SAUCE_ACCESS_KEY"); | |
String sessionId; | |
@Test | |
public void openHomePage() throws MalformedURLException | |
{ | |
URL url = new URL("https://ondemand.saucelabs.com/wd/hub"); | |
DesiredCapabilities capabilities = new DesiredCapabilities(); | |
capabilities.setCapability("username", username); | |
capabilities.setCapability("accessKey", accessKey); | |
capabilities.setCapability("browserName", "Chrome"); | |
capabilities.setCapability("platform", "Windows 10"); | |
capabilities.setCapability("version", "latest"); | |
capabilities.setCapability("name", "Costco Travel demo 2"); | |
capabilities.setCapability("build", "Sauce API tests-1"); | |
RemoteWebDriver driver = new RemoteWebDriver(url, capabilities); | |
sessionId = driver.getSessionId().toString(); | |
driver.get("https://costcotravel.com"); | |
System.out.println(driver.getTitle()); | |
// NOTE: You can use either the sauce Javascript exector, or the REST API | |
// You don't need to use both -- that was just for the demo. | |
try | |
{ | |
assertThat(driver.getTitle()).contains("Costco"); | |
driver.executeScript("sauce:job-result=passed"); | |
passed = true; | |
} | |
catch (AssertionError e) | |
{ | |
driver.executeScript("sauce:job-result=failed"); | |
passed = false; | |
throw e; | |
} | |
driver.quit(); | |
} | |
@After | |
public void teardown() throws InterruptedException | |
{ | |
SauceREST api = new SauceREST(username, accessKey); | |
if(passed) | |
{ | |
api.jobPassed(sessionId); | |
} | |
else | |
{ | |
api.jobFailed(sessionId); | |
} | |
} | |
} | |
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>org.example</groupId> | |
<artifactId>CostcoTravelDemo</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.seleniumhq.selenium</groupId> | |
<artifactId>selenium-java</artifactId> | |
<version>3.141.59</version> | |
</dependency> | |
<dependency> | |
<groupId>org.assertj</groupId> | |
<artifactId>assertj-core</artifactId> | |
<version>3.15.0</version> | |
</dependency> | |
<dependency> | |
<groupId>com.saucelabs</groupId> | |
<artifactId>saucerest</artifactId> | |
<version>1.0.44</version> | |
</dependency> | |
</dependencies> | |
<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> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment