Updated Java Appium examples and documentation

This commit is contained in:
Ross Rowe
2013-03-29 16:17:51 +11:00
parent b4a7ddd022
commit 07a7a70c2f
10 changed files with 138 additions and 48 deletions

View File

@@ -0,0 +1,18 @@
Sample Appium JUnit project
---
This contains the source code for running sample [Appium](http://github.com/appium/appium) tests using [JUnit](http://www.junit.org).
In order to run the tests, you will need to install [Apache Maven](http://maven.apache.org), and Appium (according to the Appium [installation instructions](https://github.com/appium/appium).
You will then need to start appium, eg:
grunt appium
To compile and run all tests, run:
mvn test
To run a single test, run:
mvn -Dtest=com.saucelabs.appium.SimpleTest test

View File

@@ -6,7 +6,7 @@
<artifactId>sauce_appium_junit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sauce_appium_junit</name>
<description>Sample Appium tests using JUnit</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
@@ -14,12 +14,6 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>sauce_junit</artifactId>
<version>LATEST</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
@@ -46,6 +40,13 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>

View File

@@ -19,6 +19,12 @@ import java.util.Random;
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>.
* <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 {
@@ -30,11 +36,16 @@ public class SauceTest {
private static final int MINIMUM = 0;
private static final int MAXIMUM = 10;
/**
* 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.
*
* @throws Exception
*/
@Before
public void setUp() throws Exception {
// set up appium
String sauceUserName = System.getenv("SAUCE_USER_NAME");
String sauceAccessKey = System.getenv("SAUCE_ACCESS_KEY");
String sauceAccessKey = System.getenv("SAUCE_USER_NAME");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS 6.0");
capabilities.setCapability("device", "iPhone Simulator");
@@ -51,7 +62,6 @@ public class SauceTest {
driver.quit();
}
private void populate() {
//populate text fields with two random number
List<WebElement> elems = driver.findElements(By.tagName("textField"));

View File

@@ -19,6 +19,9 @@ import java.util.Random;
import static org.junit.Assert.assertEquals;
/**
* Simple <a href="https://github.com/appium/appium">Appium</a> test which runs against a local Appium instance deployed
* with the 'TestApp' iPhone project which is included in the Appium source distribution.
*
* @author Ross Rowe
*/
public class SimpleTest {
@@ -33,8 +36,7 @@ public class SimpleTest {
@Before
public void setUp() throws Exception {
// set up appium
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "../../../apps/TestApp/build/Release-iphonesimulator");
File appDir = new File(System.getProperty("user.dir"), "../../../apps/TestApp/build/Release-iphonesimulator");
File app = new File(appDir, "TestApp.app");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS");
@@ -50,6 +52,17 @@ public class SimpleTest {
driver.quit();
}
@Test
public void testUIComputation() throws Exception {
// populate text fields with values
populate();
// trigger computation by using the button
WebElement button = driver.findElement(By.tagName("button"));
button.click();
// is sum equal ?
WebElement texts = driver.findElement(By.tagName("staticText"));
assertEquals(texts.getText(), String.valueOf(values.get(0) + values.get(1)));
}
private void populate() {
//populate text fields with two random number
@@ -61,19 +74,4 @@ public class SimpleTest {
values.add(rndNum);
}
}
@Test
public void testUIComputation() throws Exception {
// populate text fields with values
populate();
// trigger computation by using the button
WebElement button = driver.findElement(By.tagName("button"));
button.click();
// is sum equal ?
WebElement texts = driver.findElement(By.tagName("staticText"));
assertEquals(texts.getText(), String.valueOf(values.get(0) + values.get(1)));
}
}

View File

@@ -11,14 +11,12 @@ import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.*;
import java.io.File;
import java.net.URL;
@@ -27,6 +25,9 @@ import java.util.List;
import static org.junit.Assert.*;
/**
* <a href="https://github.com/appium/appium">Appium</a> test which runs against a local Appium instance deployed
* with the 'UICatalog' iPhone project which is included in the Appium source distribution.
*
* @author Ross Rowe
*/
public class UICatalogTest {
@@ -46,7 +47,7 @@ public class UICatalogTest {
capabilities.setCapability(CapabilityType.VERSION, "6.0");
capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
capabilities.setCapability("app", app.getAbsolutePath());
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@After
@@ -95,6 +96,7 @@ public class UICatalogTest {
}
@Test
@Ignore("Currently failing due to IllegalArgumentException: Superclass has no null constructors but no arguments were given")
public void testScreenshot() {
//make screenshot and get is as base64
WebDriver augmentedDriver = new Augmenter().augment(driver);
@@ -107,6 +109,7 @@ public class UICatalogTest {
}
@Test
@Ignore("Currently failing because no element with a tag name of 'segmentedControl' can be found")
public void testAttributes() {
//go to the toolbar section
openMenuPosition(8);
@@ -159,7 +162,7 @@ public class UICatalogTest {
List<WebElement> elements = driver.findElements(By.tagName("staticText"));
//trigger modal alert with cancel & ok buttons
WebElement triggerOkCancel = elements.get(14);
WebElement triggerOkCancel = elements.get(24);
triggerOkCancel.click();
Alert alert = driver.switchTo().alert();
//check if title of alert is correct
@@ -230,4 +233,17 @@ public class UICatalogTest {
assertNotSame(source_main, source_textfields);
}
public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
private RemoteTouchScreen touch;
public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
touch = new RemoteTouchScreen(getExecuteMethod());
}
public TouchScreen getTouch() {
return touch;
}
}
}