Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Last active September 19, 2019 20:51
Show Gist options
  • Save sauceaaron/80a569172829562853f2ed1d5b17bdbb to your computer and use it in GitHub Desktop.
Save sauceaaron/80a569172829562853f2ed1d5b17bdbb to your computer and use it in GitHub Desktop.
Demonstrate how to use
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.MutableCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
public class MutableCapabilitiesTest
{
String SAUCE_URL = "https://ondemand.saucelabs.com/wd/hub";
String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");
String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
String TUNNEL_IDENTIFIER = System.getenv("TUNNEL_IDENTIFIER");
String PARENT_TUNNEL = System.getenv("PARENT_IDENTIFIER");
String BUILD_TAG = System.getenv("BUILD_BUILDID");
String TEST_ENVIRONMENT_URL = System.getenv("TEST_ENVIRONMENT_URL");
RemoteWebDriver driver;
@Rule
public TestName testName = new TestName();
@Before
public void setup() throws MalformedURLException
{
String TEST_NAME = this.getClass().getSimpleName() + " " + testName.getMethodName();
URL url = new URL("https://ondemand.saucelabs.com/wd/hub");
MutableCapabilities sauceOpts = new MutableCapabilities();
sauceOpts.setCapability("seleniumVersion","3.141.59");
sauceOpts.setCapability("username", SAUCE_USERNAME);
sauceOpts.setCapability("accessKey", SAUCE_ACCESS_KEY);
/* if using a sauce connect tunnel */
sauceOpts.setCapability("tunnelIdentifier", TUNNEL_IDENTIFIER);
/* if using a shared tunnel started by another user, specify that username */
sauceOpts.setCapability("parentTunnel", PARENT_TUNNEL);
sauceOpts.setCapability("name", TEST_NAME);
sauceOpts.setCapability("build", BUILD_TAG);
sauceOpts.setCapability("tags", Arrays.asList("smoketest", "featuretest", "regressiontest"));
ChromeOptions chromeOpts = new ChromeOptions();
chromeOpts.setExperimentalOption("w3c", true);
chromeOpts.setCapability("platform", "any");
chromeOpts.setCapability("version", "latest");
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("sauce:options", sauceOpts);
capabilities.setCapability("goog:chromeOptions", chromeOpts);
driver = new RemoteWebDriver(url, capabilities);
}
@Test
public void exampleTest() throws MalformedURLException
{
try
{
driver.get(TEST_ENVIRONMENT_URL);
String title = driver.getTitle();
assertThat(title).contains("Expected Title");
//...implement more steps here
driver.executeScript("sauce:job-result=passed");
}
catch(AssertionError e)
{
driver.executeScript("sauce:job-result=failed");
throw e;
}
}
@After
public void teardown()
{
driver.quit();
}
}
<?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.oneshore</groupId>
<artifactId>ShoppingCartExamples</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>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.13.2</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
</project>
setx SAUCE_USERNAME <your username>
setx SAUCE_ACCESS_KEY <your access key>
setx TUNNEL_IDENTIFIER <your tunnel name>
setx PARENT_TUNNEL <username for shared tunnel>
setx TEST_ENVIRONMENT_URL <your system under test>
sc.exe --no-remove-colliding-tunnels --shared-tunnel --tunnel-identifier <your tunnel name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment