Last active
July 1, 2019 22:35
-
-
Save sauceaaron/94c8f05e8a01e681077d2b61902c0d03 to your computer and use it in GitHub Desktop.
Setting Test Name in Sauce Labs with TestNG
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 org.openqa.selenium.remote.DesiredCapabilities; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| import org.testng.annotations.Test; | |
| import java.lang.reflect.Method; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| public class DesiredCapabilitiesTest | |
| { | |
| String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); | |
| String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY"); | |
| String SAUCE_URL = "https://ondemand.saucelabs.com/wd/hub"; | |
| String SELENIUM_PLATFORM = "Windows 10"; | |
| String SELENIUM_BROWSER = "Chrome"; | |
| String SELENIUM_VERSION = "latest"; | |
| @Test | |
| public void testNameSetUsingDesiredCapabilities(Method method) throws MalformedURLException | |
| { | |
| URL url = new URL(SAUCE_URL); | |
| String TEST_NAME = this.getClass().getSimpleName() + " " + method.getName(); | |
| DesiredCapabilities capabilities = new DesiredCapabilities(); | |
| capabilities.setCapability("username", SAUCE_USERNAME); | |
| capabilities.setCapability("accessKey", SAUCE_ACCESS_KEY); | |
| capabilities.setCapability("platform", SELENIUM_PLATFORM); | |
| capabilities.setCapability("browserName", SELENIUM_BROWSER); | |
| capabilities.setCapability("version", SELENIUM_VERSION); | |
| capabilities.setCapability("name", TEST_NAME); | |
| RemoteWebDriver driver = new RemoteWebDriver(url, capabilities); | |
| driver.get("https://saucelabs.com"); | |
| System.out.println(driver.getTitle()); | |
| driver.quit(); | |
| } | |
| } |
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 org.openqa.selenium.remote.DesiredCapabilities; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| import org.testng.annotations.AfterMethod; | |
| import org.testng.annotations.BeforeMethod; | |
| import org.testng.annotations.Test; | |
| import java.lang.reflect.Method; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| public class JavascriptExecutorTest | |
| { | |
| String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); | |
| String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY"); | |
| String SAUCE_URL = "https://ondemand.saucelabs.com/wd/hub"; | |
| String SELENIUM_PLATFORM = "Windows 10"; | |
| String SELENIUM_BROWSER = "Chrome"; | |
| String SELENIUM_VERSION = "latest"; | |
| String TEST_NAME; | |
| RemoteWebDriver driver; | |
| @BeforeMethod | |
| public void setup(Method method) throws MalformedURLException | |
| { | |
| URL url = new URL(SAUCE_URL); | |
| DesiredCapabilities capabilities = new DesiredCapabilities(); | |
| capabilities.setCapability("username", SAUCE_USERNAME); | |
| capabilities.setCapability("accessKey", SAUCE_ACCESS_KEY); | |
| capabilities.setCapability("platform", SELENIUM_PLATFORM); | |
| capabilities.setCapability("browserName", SELENIUM_BROWSER); | |
| capabilities.setCapability("version", SELENIUM_VERSION); | |
| driver = new RemoteWebDriver(url, capabilities); | |
| TEST_NAME = this.getClass().getSimpleName() + " " + method.getName(); | |
| } | |
| @Test | |
| public void testNameSetUsingJavaScriptExecutor(Method method) | |
| { | |
| driver.get("https://saucelabs.com"); | |
| System.out.println(driver.getTitle()); | |
| } | |
| @AfterMethod | |
| public void teardown() | |
| { | |
| driver.executeScript("sauce:job-name=" + TEST_NAME); | |
| driver.quit(); | |
| } | |
| } |
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>TestNG-Java-Maven-Example</artifactId> | |
| <version>1.0-SNAPSHOT</version> | |
| <dependencies> | |
| <dependency> | |
| <groupId>org.testng</groupId> | |
| <artifactId>testng</artifactId> | |
| <version>6.14.3</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.seleniumhq.selenium</groupId> | |
| <artifactId>selenium-java</artifactId> | |
| <version>3.141.59</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.SauceREST; | |
| import org.openqa.selenium.MutableCapabilities; | |
| import org.openqa.selenium.chrome.ChromeOptions; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| import org.testng.annotations.AfterMethod; | |
| import org.testng.annotations.Test; | |
| import java.lang.reflect.Method; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.util.HashMap; | |
| public class SauceRestApiTest | |
| { | |
| String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); | |
| String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY"); | |
| String SAUCE_URL = "https://ondemand.saucelabs.com/wd/hub"; | |
| String PLATFORM_NAME = "Windows 10"; | |
| String BROWSER_NAME = "Chrome"; | |
| String BROWSER_VERSION = "latest"; | |
| String SELENIUM_VERSION = "3.141.59"; | |
| String TEST_NAME; | |
| RemoteWebDriver driver; | |
| String sessionId; | |
| @Test | |
| public void testNameSetUsingSauceRestApi(Method method) throws MalformedURLException | |
| { | |
| TEST_NAME = this.getClass().getSimpleName() + " " + method.getName(); | |
| URL url = new URL(SAUCE_URL); | |
| MutableCapabilities capabilities = new MutableCapabilities(); | |
| capabilities.setCapability("platformName", PLATFORM_NAME); | |
| capabilities.setCapability("browserName", BROWSER_NAME); | |
| capabilities.setCapability("browserVersion", BROWSER_VERSION); | |
| ChromeOptions chromeOptions = new ChromeOptions(); | |
| chromeOptions.setCapability("w3c", true); | |
| MutableCapabilities sauceOptions = new MutableCapabilities(); | |
| sauceOptions.setCapability("username", SAUCE_USERNAME); | |
| sauceOptions.setCapability("accessKey", SAUCE_ACCESS_KEY); | |
| sauceOptions.setCapability("seleniumVersion", SELENIUM_VERSION); | |
| capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); | |
| capabilities.setCapability("sauce:options", sauceOptions); | |
| driver = new RemoteWebDriver(url, capabilities); | |
| sessionId = driver.getSessionId().toString(); | |
| driver.get("https://saucelabs.com"); | |
| System.out.println(driver.getTitle()); | |
| driver.quit(); | |
| } | |
| @AfterMethod | |
| public void report() | |
| { | |
| HashMap<String, Object> jobInfo = new HashMap<>(); | |
| jobInfo.put("name", TEST_NAME); | |
| jobInfo.put("passed", true); | |
| SauceREST api = new SauceREST(SAUCE_USERNAME, SAUCE_ACCESS_KEY); | |
| api.updateJobInfo(sessionId, jobInfo); | |
| } | |
| } |
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 org.openqa.selenium.MutableCapabilities; | |
| import org.openqa.selenium.chrome.ChromeOptions; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| import org.testng.annotations.AfterMethod; | |
| import org.testng.annotations.BeforeMethod; | |
| import org.testng.annotations.Test; | |
| import java.lang.reflect.Method; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| public class W3CCapabilitiesTest | |
| { | |
| String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); | |
| String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY"); | |
| String SAUCE_URL = "https://ondemand.saucelabs.com/wd/hub"; | |
| String PLATFORM_NAME = "Windows 10"; | |
| String BROWSER_NAME = "Chrome"; | |
| String BROWSER_VERSION = "latest"; | |
| String SELENIUM_VERSION = "3.141.59"; | |
| String TEST_NAME; | |
| RemoteWebDriver driver; | |
| @BeforeMethod | |
| public void setup(Method method) throws MalformedURLException | |
| { | |
| TEST_NAME = this.getClass().getSimpleName() + " " + method.getName(); | |
| URL url = new URL(SAUCE_URL); | |
| MutableCapabilities capabilities = new MutableCapabilities(); | |
| capabilities.setCapability("platformName", PLATFORM_NAME); | |
| capabilities.setCapability("browserName", BROWSER_NAME); | |
| capabilities.setCapability("browserVersion", BROWSER_VERSION); | |
| ChromeOptions chromeOptions = new ChromeOptions(); | |
| chromeOptions.setCapability("w3c", true); | |
| MutableCapabilities sauceOptions = new MutableCapabilities(); | |
| sauceOptions.setCapability("username", SAUCE_USERNAME); | |
| sauceOptions.setCapability("accessKey", SAUCE_ACCESS_KEY); | |
| sauceOptions.setCapability("seleniumVersion", SELENIUM_VERSION); | |
| sauceOptions.setCapability("name", TEST_NAME); | |
| capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); | |
| capabilities.setCapability("sauce:options", sauceOptions); | |
| driver = new RemoteWebDriver(url, capabilities); | |
| } | |
| @Test | |
| public void testNameSetUsingW3cCapabilities(Method method) | |
| { | |
| driver.get("https://saucelabs.com"); | |
| System.out.println(driver.getTitle()); | |
| } | |
| @AfterMethod | |
| public void teardown() | |
| { | |
| driver.quit(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment