Skip to content

Commit 6360d35

Browse files
committed
added an example of running java tests on sauce
1 parent 25808cb commit 6360d35

6 files changed

Lines changed: 232 additions & 2 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,7 @@ build/
5050

5151
# Maven
5252
log/
53-
target/
53+
target/
54+
55+
# npm
56+
node_modules

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "appium-sample-code",
3+
"version": "1.0.0",
4+
"description": "Code examples for Appium",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/appium/sample-code.git"
12+
},
13+
"keywords": [
14+
"appium",
15+
"testcode"
16+
],
17+
"author": "appium",
18+
"license": "apache-2.0",
19+
"bugs": {
20+
"url": "https://github.com/appium/sample-code/issues"
21+
},
22+
"homepage": "https://github.com/appium/sample-code"
23+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>io.appium</groupId>
8+
<artifactId>appium-generic-test</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<repositories>
12+
<repository>
13+
<id>saucelabs-repository</id>
14+
<url>https://repository-saucelabs.forge.cloudbees.com/release</url>
15+
<releases>
16+
<enabled>true</enabled>
17+
</releases>
18+
<snapshots>
19+
<enabled>true</enabled>
20+
</snapshots>
21+
</repository>
22+
</repositories>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>io.appium</groupId>
27+
<artifactId>java-client</artifactId>
28+
<version>2.1.0</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.saucelabs</groupId>
32+
<artifactId>sauce_junit</artifactId>
33+
<version>2.1.11</version>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
38+
39+
40+
</project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import com.saucelabs.common.SauceOnDemandAuthentication;
2+
import com.saucelabs.common.SauceOnDemandSessionIdProvider;
3+
import com.saucelabs.junit.SauceOnDemandTestWatcher;
4+
import io.appium.java_client.MobileElement;
5+
import io.appium.java_client.ios.IOSDriver;
6+
import io.appium.java_client.remote.MobileCapabilityType;
7+
import junit.framework.TestCase;
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.rules.TestName;
13+
import org.openqa.selenium.remote.DesiredCapabilities;
14+
15+
import java.net.MalformedURLException;
16+
import java.net.URL;
17+
18+
import static junit.framework.Assert.assertEquals;
19+
import static junit.framework.TestCase.assertTrue;
20+
21+
public class Main implements SauceOnDemandSessionIdProvider {
22+
final private String USERNAME = System.getenv("SAUCE_USERNAME");
23+
final private String ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
24+
private SauceOnDemandAuthentication authentication = new SauceOnDemandAuthentication(USERNAME, ACCESS_KEY);
25+
26+
private IOSDriver driver;
27+
private String sessionId;
28+
29+
@Rule
30+
public SauceOnDemandTestWatcher resultReportingTestWatcher = new SauceOnDemandTestWatcher(this, authentication);
31+
@Override
32+
public String getSessionId() {
33+
return sessionId;
34+
}
35+
36+
public @Rule TestName name = new TestName();
37+
38+
@Before
39+
public void setUp() throws MalformedURLException {
40+
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
41+
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1");
42+
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone Simulator");
43+
desiredCapabilities.setCapability(MobileCapabilityType.APP, "http://appium.s3.amazonaws.com/TestApp6.0.app.zip");
44+
desiredCapabilities.setCapability("appiumVersion", "1.3.4");
45+
desiredCapabilities.setCapability("name", name.getMethodName());
46+
47+
URL sauceUrl = new URL("http://" + authentication.getUsername() + ":"+ authentication.getAccessKey() + "@ondemand.saucelabs.com:80/wd/hub");
48+
49+
driver = new IOSDriver(sauceUrl, desiredCapabilities);
50+
sessionId = driver.getSessionId().toString();
51+
}
52+
53+
@After
54+
public void tearDown() {
55+
System.out.println("Link to your job: https://saucelabs.com/jobs/" + this.getSessionId());
56+
driver.quit();
57+
}
58+
59+
@Test
60+
public void testUIComputation() {
61+
62+
// populate text fields with values
63+
MobileElement fieldOne = (MobileElement) driver.findElementByAccessibilityId("TextField1");
64+
fieldOne.sendKeys("12");
65+
66+
MobileElement fieldTwo = (MobileElement) driver.findElementsByClassName("UIATextField").get(1);
67+
fieldTwo.sendKeys("8");
68+
69+
// they should be the same size, and the first should be above the second
70+
assertTrue(fieldOne.getLocation().getY() < fieldTwo.getLocation().getY());
71+
assertEquals(fieldOne.getSize(), fieldTwo.getSize());
72+
73+
// trigger computation by using the button
74+
driver.findElementByAccessibilityId("ComputeSumButton").click();
75+
76+
// is sum equal?
77+
String sum = driver.findElementsByClassName("UIAStaticText").get(0).getText();
78+
TestCase.assertEquals(Integer.parseInt(sum), 20);
79+
}
80+
81+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import com.saucelabs.common.SauceOnDemandAuthentication;
2+
import com.saucelabs.common.SauceOnDemandSessionIdProvider;
3+
import com.saucelabs.junit.SauceOnDemandTestWatcher;
4+
import io.appium.java_client.MobileElement;
5+
import io.appium.java_client.ios.IOSDriver;
6+
import io.appium.java_client.remote.MobileCapabilityType;
7+
import junit.framework.TestCase;
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.rules.TestName;
13+
import org.openqa.selenium.remote.DesiredCapabilities;
14+
15+
import java.net.MalformedURLException;
16+
import java.net.URL;
17+
import java.util.concurrent.TimeUnit;
18+
19+
import static junit.framework.Assert.assertEquals;
20+
import static junit.framework.TestCase.assertTrue;
21+
22+
public class SimpleIOSSauceTests implements SauceOnDemandSessionIdProvider {
23+
final private String USERNAME = System.getenv("SAUCE_USERNAME");
24+
final private String ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
25+
private SauceOnDemandAuthentication authentication = new SauceOnDemandAuthentication(USERNAME, ACCESS_KEY);
26+
27+
private IOSDriver driver;
28+
private String sessionId;
29+
30+
@Rule
31+
public SauceOnDemandTestWatcher resultReportingTestWatcher = new SauceOnDemandTestWatcher(this, authentication);
32+
@Override
33+
public String getSessionId() {
34+
return sessionId;
35+
}
36+
37+
public @Rule TestName name = new TestName();
38+
39+
@Before
40+
public void setUp() throws MalformedURLException {
41+
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
42+
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1");
43+
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone Simulator");
44+
desiredCapabilities.setCapability(MobileCapabilityType.APP, "http://appium.s3.amazonaws.com/TestApp6.0.app.zip");
45+
desiredCapabilities.setCapability("appiumVersion", "1.3.4");
46+
desiredCapabilities.setCapability("name", name.getMethodName());
47+
48+
URL sauceUrl = new URL("http://" + authentication.getUsername() + ":"+ authentication.getAccessKey() + "@ondemand.saucelabs.com:80/wd/hub");
49+
50+
driver = new IOSDriver(sauceUrl, desiredCapabilities);
51+
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
52+
sessionId = driver.getSessionId().toString();
53+
}
54+
55+
@After
56+
public void tearDown() {
57+
System.out.println("Link to your job: https://saucelabs.com/jobs/" + this.getSessionId());
58+
driver.quit();
59+
}
60+
61+
@Test
62+
public void testUIComputation() {
63+
64+
// populate text fields with values
65+
MobileElement fieldOne = (MobileElement) driver.findElementByAccessibilityId("TextField1");
66+
fieldOne.sendKeys("12");
67+
68+
MobileElement fieldTwo = (MobileElement) driver.findElementsByClassName("UIATextField").get(1);
69+
fieldTwo.sendKeys("8");
70+
71+
// they should be the same size, and the first should be above the second
72+
assertTrue(fieldOne.getLocation().getY() < fieldTwo.getLocation().getY());
73+
assertEquals(fieldOne.getSize(), fieldTwo.getSize());
74+
75+
// trigger computation by using the button
76+
driver.findElementByAccessibilityId("ComputeSumButton").click();
77+
78+
// is sum equal?
79+
String sum = driver.findElementsByClassName("UIAStaticText").get(0).getText();
80+
TestCase.assertEquals(Integer.parseInt(sum), 20);
81+
}
82+
83+
}

sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SauceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void setUp() throws Exception {
8080

8181
driver = new IOSDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub", sauceUserName, sauceAccessKey)),
8282
capabilities);
83-
this.sessionId = ((RemoteWebDriver) driver).getSessionId().toString();
83+
this.sessionId = driver.getSessionId().toString();
8484
values = new ArrayList<Integer>();
8585
}
8686

0 commit comments

Comments
 (0)