Created
October 25, 2024 13:36
-
-
Save nadvolod/7955e8b02b839eb69d02817caa33896f 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.saucebindings.junit5.examples.without; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.TestInfo; | |
import org.junit.jupiter.api.extension.ExtensionContext; | |
import org.junit.jupiter.api.extension.RegisterExtension; | |
import org.junit.jupiter.api.extension.TestWatcher; | |
import org.openqa.selenium.Capabilities; | |
import org.openqa.selenium.JavascriptExecutor; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.chrome.ChromeOptions; | |
import org.openqa.selenium.remote.RemoteWebDriver; | |
import org.openqa.selenium.remote.SessionId; | |
public class QuickStartExample { | |
WebDriver driver; | |
TestInfo testInfo; | |
SessionId sessionId; | |
@RegisterExtension SauceTestWatcher watcher = new SauceTestWatcher(); | |
@BeforeEach | |
public void startSession(TestInfo testInfo) { | |
this.testInfo = testInfo; | |
driver = new RemoteWebDriver(getSauceUrl(), getCapabilities()); | |
sessionId = ((RemoteWebDriver) driver).getSessionId(); | |
} | |
@Test | |
public void quickStartExample() { | |
((JavascriptExecutor) driver).executeScript("sauce:context=Navigating to Swag Labs"); | |
driver.get("https://www.saucedemo.com/"); | |
} | |
private Capabilities getCapabilities() { | |
ChromeOptions options = new ChromeOptions(); | |
Map<String, Object> sauceOptions = new HashMap<>(); | |
sauceOptions.put("username", System.getenv("SAUCE_USERNAME")); | |
sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY")); | |
sauceOptions.put("name", testInfo.getDisplayName()); | |
sauceOptions.put("build", System.getProperty("build.name")); | |
options.setCapability("sauce:options", sauceOptions); | |
return options; | |
} | |
private URL getSauceUrl() { | |
try { | |
return new URL("https://ondemand.us-west-1.saucelabs.com/wd/hub"); | |
} catch (MalformedURLException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private class SauceTestWatcher implements TestWatcher { | |
@Override | |
public void testSuccessful(ExtensionContext context) { | |
printResults(); | |
try { | |
((JavascriptExecutor) driver).executeScript("sauce:job-result=passed"); | |
driver.quit(); | |
} catch (Exception e) { | |
System.out.println("problem with using driver: " + e); | |
} | |
} | |
@Override | |
public void testFailed(ExtensionContext context, Throwable cause) { | |
printResults(); | |
try { | |
((JavascriptExecutor) driver).executeScript("sauce:job-result=failed"); | |
driver.quit(); | |
} catch (Exception e) { | |
System.out.println("problem with using driver: " + e); | |
} | |
} | |
private void printResults() { | |
String sauceReporter = | |
String.format( | |
"SauceOnDemandSessionID=%s job-name=%s", sessionId, testInfo.getDisplayName()); | |
String sauceTestLink = | |
String.format("Test Job Link: https://app.saucelabs.com/tests/%s", sessionId); | |
System.out.print(sauceReporter + "\n" + sauceTestLink + "\n"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment