Created
September 16, 2019 18:09
-
-
Save sauceaaron/7d6ba8d2a83ad4a379bf9d7c91273c98 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
| import com.saucelabs.saucerest.DataCenter; | |
| import com.saucelabs.saucerest.SauceREST; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Rule; | |
| import org.junit.Test; | |
| import org.junit.rules.TestName; | |
| import org.junit.rules.TestWatcher; | |
| import org.junit.runner.Description; | |
| import org.junit.runners.model.Statement; | |
| 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 HeadlessTestWithWatcher | |
| { | |
| String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY"); | |
| String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); | |
| String SAUCE_HEADLESS_URL = "https://ondemand.us-east-1.saucelabs.com/wd/hub"; | |
| String SAUCE_HEADLESS_API = "https://us-east-1.saucelabs.com/rest/v1"; | |
| public DesiredCapabilities capabilities; | |
| public RemoteWebDriver driver; | |
| public String sessionId; | |
| @Rule | |
| public TestName testName = new TestName(); | |
| @Rule | |
| public SauceTestWatcher watcher = new SauceTestWatcher(DataCenter.US_EAST); | |
| @Before | |
| public void setup() throws MalformedURLException | |
| { | |
| URL url = new URL(SAUCE_HEADLESS_URL); | |
| capabilities = new DesiredCapabilities(); | |
| capabilities.setCapability("username", SAUCE_USERNAME); | |
| capabilities.setCapability("accessKey", SAUCE_ACCESS_KEY); | |
| capabilities.setCapability("platform", "any"); | |
| capabilities.setCapability("browserName", "chrome"); | |
| capabilities.setCapability("version", "latest"); | |
| capabilities.setCapability("name", this.getClass().getSimpleName() + " " + testName.getMethodName()); | |
| driver = new RemoteWebDriver(url, capabilities); | |
| sessionId = driver.getSessionId().toString(); | |
| watcher.setSessionId(sessionId); | |
| } | |
| @Test | |
| public void failingHeadlessTest() | |
| { | |
| driver.get("https://saucelabs.com"); | |
| String title = driver.getTitle(); | |
| assertThat(title).contains("Hot Sauce"); | |
| } | |
| @Test | |
| public void passingHeadlessTest() | |
| { | |
| driver.get("https://saucelabs.com"); | |
| String title = driver.getTitle(); | |
| assertThat(title).contains("Sauce Labs"); | |
| } | |
| @After | |
| public void cleanup() | |
| { | |
| driver.quit(); | |
| } | |
| public class SauceTestWatcher extends TestWatcher | |
| { | |
| private ThreadLocal<String> sessionId; | |
| private SauceREST api; | |
| public SauceTestWatcher() | |
| { | |
| init(DataCenter.US); | |
| } | |
| public SauceTestWatcher(DataCenter dataCenter) | |
| { | |
| init(dataCenter); | |
| } | |
| public void init(DataCenter dataCenter) | |
| { | |
| api = new SauceREST(SAUCE_USERNAME, SAUCE_ACCESS_KEY, dataCenter); | |
| sessionId = new ThreadLocal<>(); | |
| } | |
| public void setSessionId(String sessionId) | |
| { | |
| this.sessionId.set(sessionId); | |
| } | |
| @Override | |
| public Statement apply(Statement base, Description description) | |
| { | |
| return super.apply(base, description); | |
| } | |
| protected void succeeded(Description description) | |
| { | |
| System.out.println("PASSED test " + description); | |
| api.jobPassed(sessionId.get()); | |
| } | |
| protected void failed(Throwable e, Description description) | |
| { | |
| System.out.println("FAILED test " + description); | |
| api.jobFailed(sessionId.get()); | |
| } | |
| } | |
| } |
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.examples</groupId> | |
| <artifactId>saucelabs-headless-java</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> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-surefire-plugin</artifactId> | |
| <version>2.22.2</version> | |
| <configuration> | |
| <parallel>all</parallel> | |
| <threadCount>10</threadCount> | |
| <redirectTestOutputToFile>false</redirectTestOutputToFile> | |
| </configuration> | |
| </plugin> | |
| </plugins> | |
| </build> | |
| <dependencies> | |
| <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.42</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>junit</groupId> | |
| <artifactId>junit</artifactId> | |
| <version>4.12</version> | |
| <scope>test</scope> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.assertj</groupId> | |
| <artifactId>assertj-core</artifactId> | |
| <version>3.13.2</version> | |
| </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.DataCenter; | |
| import com.saucelabs.saucerest.SauceREST; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Rule; | |
| import org.junit.Test; | |
| import org.junit.rules.TestName; | |
| 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 SimpleHeadlessTest | |
| { | |
| String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY"); | |
| String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); | |
| String SAUCE_HEADLESS_URL = "https://ondemand.us-east-1.saucelabs.com/wd/hub"; | |
| String SAUCE_HEADLESS_API = "https://us-east-1.saucelabs.com/rest/v1"; | |
| public DesiredCapabilities capabilities; | |
| public RemoteWebDriver driver; | |
| public String sessionId; | |
| public SauceREST api; | |
| @Rule | |
| public TestName testName = new TestName(); | |
| @Before | |
| public void setup() throws MalformedURLException | |
| { | |
| URL url = new URL(SAUCE_HEADLESS_URL); | |
| capabilities = new DesiredCapabilities(); | |
| capabilities.setCapability("username", SAUCE_USERNAME); | |
| capabilities.setCapability("accessKey", SAUCE_ACCESS_KEY); | |
| capabilities.setCapability("platform", "any"); | |
| capabilities.setCapability("browserName", "chrome"); | |
| capabilities.setCapability("version", "latest"); | |
| capabilities.setCapability("name", this.getClass().getSimpleName() + " " + testName.getMethodName()); | |
| driver = new RemoteWebDriver(url, capabilities); | |
| sessionId = driver.getSessionId().toString(); | |
| api = new SauceREST(SAUCE_USERNAME, SAUCE_ACCESS_KEY, DataCenter.US_EAST); | |
| } | |
| @Test | |
| public void failingHeadlessTest() | |
| { | |
| driver.get("https://saucelabs.com"); | |
| api.jobFailed(sessionId); | |
| assertThat(driver.getTitle()).contains("Hot Sauce"); | |
| } | |
| @Test | |
| public void passingHeadlessTest() | |
| { | |
| driver.get("https://saucelabs.com"); | |
| api.jobPassed(sessionId); | |
| assertThat(driver.getTitle()).contains("Sauce Labs"); | |
| } | |
| @After | |
| public void cleanup() | |
| { | |
| driver.quit(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment