|
| 1 | +package com.saucelabs.saucebindings.junit5.examples.without; |
| 2 | + |
| 3 | +import com.saucelabs.saucebindings.SaucePlatform; |
| 4 | +import com.saucelabs.saucebindings.options.SauceOptions; |
| 5 | +import java.net.MalformedURLException; |
| 6 | +import java.net.URL; |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import org.junit.jupiter.api.AfterAll; |
| 11 | +import org.junit.jupiter.api.BeforeAll; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.TestInfo; |
| 15 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 16 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 17 | +import org.junit.jupiter.api.extension.TestWatcher; |
| 18 | +import org.openqa.selenium.Capabilities; |
| 19 | +import org.openqa.selenium.JavascriptExecutor; |
| 20 | +import org.openqa.selenium.WebDriver; |
| 21 | +import org.openqa.selenium.chrome.ChromeDriver; |
| 22 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 23 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 24 | +import org.openqa.selenium.remote.SessionId; |
| 25 | + |
| 26 | +public class ToggleLocalExample { |
| 27 | + WebDriver driver; |
| 28 | + TestInfo testInfo; |
| 29 | + SessionId sessionId; |
| 30 | + |
| 31 | + @RegisterExtension TestWatcher watcher = new SauceTestWatcher(); |
| 32 | + @RegisterExtension TestWatcher testWatcher = new LocalTestWatcher(); |
| 33 | + |
| 34 | + // To run test on Sauce Labs, change this to "false" |
| 35 | + @BeforeAll |
| 36 | + public static void disableSauce() { |
| 37 | + System.setProperty("sauce.disabled", "true"); |
| 38 | + } |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + public void setup(TestInfo testInfo) { |
| 42 | + if (isSauceDisabled()) { |
| 43 | + driver = new ChromeDriver(); |
| 44 | + } else { |
| 45 | + this.testInfo = testInfo; |
| 46 | + driver = new RemoteWebDriver(getSauceUrl(), getCapabilities()); |
| 47 | + this.sessionId = ((RemoteWebDriver) driver).getSessionId(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @AfterAll |
| 52 | + public static void resetSauce() { |
| 53 | + System.clearProperty("sauce.disabled"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void toggleLocal() { |
| 58 | + if (!isSauceDisabled()) { |
| 59 | + ((JavascriptExecutor) driver).executeScript("sauce:context=Navigating to Swag Labs"); |
| 60 | + } |
| 61 | + |
| 62 | + driver.get("https://www.saucedemo.com/"); |
| 63 | + } |
| 64 | + |
| 65 | + private static SauceOptions getSauceOptions() { |
| 66 | + ChromeOptions chromeOptions = new ChromeOptions(); |
| 67 | + chromeOptions.addArguments("--hide-scrollbars"); |
| 68 | + |
| 69 | + return SauceOptions.chrome(chromeOptions) |
| 70 | + .setPlatformName(SaucePlatform.MAC_CATALINA) |
| 71 | + .setIdleTimeout(Duration.ofSeconds(30)) |
| 72 | + .build(); |
| 73 | + } |
| 74 | + |
| 75 | + private boolean isSauceDisabled() { |
| 76 | + String value = System.getenv("SAUCE_DISABLED"); |
| 77 | + return Boolean.parseBoolean(value) || Boolean.getBoolean("sauce.disabled"); |
| 78 | + } |
| 79 | + |
| 80 | + private Capabilities getCapabilities() { |
| 81 | + ChromeOptions options = new ChromeOptions(); |
| 82 | + Map<String, Object> sauceOptions = new HashMap<>(); |
| 83 | + sauceOptions.put("username", System.getenv("SAUCE_USERNAME")); |
| 84 | + sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY")); |
| 85 | + sauceOptions.put("name", testInfo.getDisplayName()); |
| 86 | + sauceOptions.put("build", System.getProperty("build.name")); |
| 87 | + options.setCapability("sauce:options", sauceOptions); |
| 88 | + |
| 89 | + return options; |
| 90 | + } |
| 91 | + |
| 92 | + private URL getSauceUrl() { |
| 93 | + try { |
| 94 | + return new URL("https://ondemand.us-west-1.saucelabs.com/wd/hub"); |
| 95 | + } catch (MalformedURLException e) { |
| 96 | + throw new RuntimeException(e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private class LocalTestWatcher implements TestWatcher { |
| 101 | + @Override |
| 102 | + public void testSuccessful(ExtensionContext context) { |
| 103 | + driver.quit(); |
| 104 | + System.out.println("Test Successful"); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void testFailed(ExtensionContext context, Throwable cause) { |
| 109 | + driver.quit(); |
| 110 | + System.out.println("Test Failed"); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private class SauceTestWatcher implements TestWatcher { |
| 115 | + @Override |
| 116 | + public void testSuccessful(ExtensionContext context) { |
| 117 | + if (isSauceDisabled()) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + printResults(); |
| 122 | + try { |
| 123 | + ((JavascriptExecutor) driver).executeScript("sauce:job-result=passed"); |
| 124 | + driver.quit(); |
| 125 | + } catch (Exception e) { |
| 126 | + System.out.println("problem with using driver: " + e); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void testFailed(ExtensionContext context, Throwable cause) { |
| 132 | + if (isSauceDisabled()) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + printResults(); |
| 137 | + |
| 138 | + try { |
| 139 | + ((JavascriptExecutor) driver).executeScript("sauce:job-result=failed"); |
| 140 | + driver.quit(); |
| 141 | + } catch (Exception e) { |
| 142 | + System.out.println("problem with using driver: " + e); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private void printResults() { |
| 147 | + String sauceReporter = |
| 148 | + String.format( |
| 149 | + "SauceOnDemandSessionID=%s job-name=%s", sessionId, testInfo.getDisplayName()); |
| 150 | + String sauceTestLink = |
| 151 | + String.format("Test Job Link: https://app.saucelabs.com/tests/%s", sessionId); |
| 152 | + System.out.print(sauceReporter + "\n" + sauceTestLink + "\n"); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments