From 2fe7ea7b098ba2145e3c7b4cc31276a3e26921ec Mon Sep 17 00:00:00 2001 From: Ross Rowe Date: Tue, 19 Nov 2013 07:15:34 +1100 Subject: [PATCH] Updated Sauce Java samples to use Sauce Java helper libraries, which sets the pass/fail status after the test has been run --- sample-code/examples/java/junit/pom.xml | 20 +++++++++ .../java/com/saucelabs/appium/SauceTest.java | 35 +++++++++++++-- sample-code/examples/java/testng/pom.xml | 6 +++ .../java/com/saucelabs/appium/SauceTest.java | 43 +++++++++++++++++-- 4 files changed, 97 insertions(+), 7 deletions(-) diff --git a/sample-code/examples/java/junit/pom.xml b/sample-code/examples/java/junit/pom.xml index b45a189c1..3d1fd51c5 100644 --- a/sample-code/examples/java/junit/pom.xml +++ b/sample-code/examples/java/junit/pom.xml @@ -32,6 +32,13 @@ 2.6 test + + + com.saucelabs + sauce_junit + 1.0.18 + test + @@ -50,5 +57,18 @@ + + + saucelabs-repository + https://repository-saucelabs.forge.cloudbees.com/release + + true + + + true + + + + \ No newline at end of file diff --git a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SauceTest.java b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SauceTest.java index de486abe4..864f59525 100644 --- a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SauceTest.java +++ b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SauceTest.java @@ -1,7 +1,11 @@ package com.saucelabs.appium; +import com.saucelabs.common.SauceOnDemandAuthentication; +import com.saucelabs.common.SauceOnDemandSessionIdProvider; +import com.saucelabs.junit.SauceOnDemandTestWatcher; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; @@ -21,13 +25,18 @@ import static org.junit.Assert.assertEquals; /** * Simple test which demonstrates how to run an Appium * using Sauce Labs. + * + * This test also includes the Sauce JUnit helper classes, which will use the Sauce REST API to mark the Sauce Job as passed/failed. + * + * In order to use the {@link SauceOnDemandTestWatcher}, the test must implement the {@link SauceOnDemandSessionIdProvider} interface. + * *

* The test relies on SAUCE_USER_NAME and SAUCE_ACCESS_KEY environment variables being set which reference * the Sauce username/access key. * * @author Ross Rowe */ -public class SauceTest { +public class SauceTest implements SauceOnDemandSessionIdProvider { private WebDriver driver; @@ -36,6 +45,20 @@ public class SauceTest { private static final int MINIMUM = 0; private static final int MAXIMUM = 10; + private String sessionId; + + /** + * Constructs a {@link SauceOnDemandAuthentication} instance using the supplied user name/access key. To use the authentication + * supplied by environment variables or from an external file, use the no-arg {@link SauceOnDemandAuthentication} constructor. + */ + public SauceOnDemandAuthentication authentication = new SauceOnDemandAuthentication(); + + /** + * JUnit Rule which will mark the Sauce Job as passed/failed when the test succeeds or fails. + */ + public @Rule + SauceOnDemandTestWatcher resultReportingTestWatcher = new SauceOnDemandTestWatcher(this, authentication); + /** * Sets up appium. You will need to either explictly set the sauce username/access key variables, or set * SAUCE_USER_NAME or SAUCE_USER_NAME environment variables to reference your Sauce account details. @@ -44,8 +67,8 @@ public class SauceTest { */ @Before public void setUp() throws Exception { - String sauceUserName = System.getenv("SAUCE_USER_NAME"); - String sauceAccessKey = System.getenv("SAUCE_USER_NAME"); + String sauceUserName = authentication.getUsername(); + String sauceAccessKey = authentication.getAccessKey(); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); capabilities.setCapability(CapabilityType.VERSION, "6.0"); @@ -55,6 +78,7 @@ public class SauceTest { driver = new RemoteWebDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub", sauceUserName, sauceAccessKey)), capabilities); + this.sessionId = ((RemoteWebDriver) driver).getSessionId().toString(); values = new ArrayList(); } @@ -86,4 +110,9 @@ public class SauceTest { WebElement texts = driver.findElement(By.tagName("staticText")); assertEquals(texts.getText(), String.valueOf(values.get(0) + values.get(1))); } + + @Override + public String getSessionId() { + return sessionId; + } } diff --git a/sample-code/examples/java/testng/pom.xml b/sample-code/examples/java/testng/pom.xml index c4ee93900..a8e652193 100644 --- a/sample-code/examples/java/testng/pom.xml +++ b/sample-code/examples/java/testng/pom.xml @@ -37,6 +37,12 @@ log4j 1.2.16 + + com.saucelabs + sauce_testng + 1.0.19 + test + diff --git a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/SauceTest.java b/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/SauceTest.java index 717607ae9..b9b6eb6c4 100644 --- a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/SauceTest.java +++ b/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/SauceTest.java @@ -1,6 +1,11 @@ package com.saucelabs.appium; +import com.saucelabs.common.SauceOnDemandAuthentication; +import com.saucelabs.common.SauceOnDemandSessionIdProvider; +import com.saucelabs.testng.SauceOnDemandAuthenticationProvider; +import com.saucelabs.testng.SauceOnDemandTestListener; +import org.apache.commons.lang.StringUtils; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; @@ -9,6 +14,7 @@ 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.Listeners; import org.testng.annotations.Test; import java.net.URL; @@ -28,14 +34,18 @@ import static org.junit.Assert.assertEquals; * * @author Ross Rowe */ -public class SauceTest { +@Listeners({SauceOnDemandTestListener.class}) +public class SauceTest implements SauceOnDemandSessionIdProvider, SauceOnDemandAuthenticationProvider { private WebDriver driver; private List values; + public SauceOnDemandAuthentication authentication; + private static final int MINIMUM = 0; private static final int MAXIMUM = 10; + private String sessionId; /** * Sets up appium. You will need to either explictly set the sauce username/access key variables, or set @@ -46,19 +56,35 @@ public class SauceTest { @BeforeMethod public void setUp() throws Exception { // set up appium - String sauceUserName = System.getenv("SAUCE_USER_NAME"); - String sauceAccessKey = System.getenv("SAUCE_ACCESS_KEY"); + String username = System.getenv("SAUCE_USER_NAME"); + String key = System.getenv("SAUCE_ACCESS_KEY"); + if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(key)) { + authentication = new SauceOnDemandAuthentication(username, key); + } else { + authentication = new SauceOnDemandAuthentication(); + } + DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS 6.0"); capabilities.setCapability("device", "iPhone Simulator"); capabilities.setCapability(CapabilityType.PLATFORM, "Mac 10.8"); capabilities.setCapability("app", "http://appium.s3.amazonaws.com/TestApp6.0.app.zip"); - driver = new RemoteWebDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub", sauceUserName, sauceAccessKey)), + driver = new RemoteWebDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub", authentication.getUsername(), authentication.getAccessKey())), capabilities); + sessionId = ((RemoteWebDriver)driver).getSessionId().toString(); values = new ArrayList(); } + /** + * {@inheritDoc} + * @return + */ + @Override + public String getSessionId() { + return sessionId; + } + @AfterMethod public void tearDown() throws Exception { driver.quit(); @@ -89,4 +115,13 @@ public class SauceTest { WebElement texts = driver.findElement(By.tagName("staticText")); assertEquals(texts.getText(), String.valueOf(values.get(0) + values.get(1))); } + + /** + * {@inheritDoc} + * @return + */ + @Override + public SauceOnDemandAuthentication getAuthentication() { + return authentication; + } }