Updated Sauce Java samples to use Sauce Java helper libraries, which sets the pass/fail status after the test has been run

This commit is contained in:
Ross Rowe
2013-11-19 07:15:34 +11:00
parent c1611ace1e
commit 2fe7ea7b09
4 changed files with 97 additions and 7 deletions

View File

@@ -32,6 +32,13 @@
<version>2.6</version>
<scope>test</scope>
</dependency>
<!-- Includes the Sauce JUnit helper libraries -->
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>sauce_junit</artifactId>
<version>1.0.18</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -50,5 +57,18 @@
</plugins>
</build>
<repositories>
<repository>
<id>saucelabs-repository</id>
<url>https://repository-saucelabs.forge.cloudbees.com/release</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@@ -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 <a href="https://github.com/appium/appium">Appium</a>
* using <a href="http://saucelabs.com">Sauce Labs</a>.
*
* This test also includes the <a href="https://github.com/saucelabs/sauce-java/tree/master/junit">Sauce JUnit</a> 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.
*
* <p/>
* 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<Integer>();
}
@@ -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;
}
}

View File

@@ -37,6 +37,12 @@
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>sauce_testng</artifactId>
<version>1.0.19</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@@ -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<Integer> 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<Integer>();
}
/**
* {@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;
}
}