Skip to content

Commit bced07a

Browse files
Update java sample
1 parent af86ada commit bced07a

9 files changed

Lines changed: 303 additions & 5 deletions

sample-code/examples/java/junit/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<dependency>
1818
<groupId>io.appium</groupId>
1919
<artifactId>java-client</artifactId>
20-
<version>3.0.0</version>
20+
<version>3.2.0</version>
2121
</dependency>
2222
<dependency>
2323
<groupId>com.googlecode.json-simple</groupId>

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

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

6161
/**
6262
* Page Object best practice is to describe interactions with target
63-
* elements by methods. This methods describe business logic of the page/screen.
63+
* elements by methods. These methods describe business logic of the page/screen.
6464
* Here test interacts with lazy instantiated elements directly.
6565
* It was done so just for obviousness
6666
*/

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

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

6161
/**
6262
* Page Object best practice is to describe interactions with target
63-
* elements by methods. This methods describe business logic of the page/screen.
63+
* elements by methods. These methods describe business logic of the page/screen.
6464
* Here test interacts with lazy instantiated elements directly.
6565
* It was done so just for obviousness
6666
*/

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

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

6767
/**
6868
* Page Object best practice is to describe interactions with target
69-
* elements by methods. This methods describe business logic of the page/screen.
69+
* elements by methods. These methods describe business logic of the page/screen.
7070
* Here test interacts with lazy instantiated elements directly.
7171
* It was done so just for obviousness
7272
*/
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.saucelabs.appium;
2+
3+
import com.saucelabs.appium.page_object.PageObjectWithCustomizedTimeOuts;
4+
import io.appium.java_client.MobileElement;
5+
import io.appium.java_client.android.AndroidDriver;
6+
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
7+
import io.appium.java_client.pagefactory.TimeOutDuration;
8+
import io.appium.java_client.remote.MobileCapabilityType;
9+
import org.junit.After;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.openqa.selenium.WebDriver;
13+
import org.openqa.selenium.remote.DesiredCapabilities;
14+
import org.openqa.selenium.support.PageFactory;
15+
16+
import java.io.File;
17+
import java.net.URL;
18+
import java.util.Calendar;
19+
import java.util.List;
20+
import java.util.concurrent.TimeUnit;
21+
22+
import static org.junit.Assert.assertEquals;
23+
24+
/**
25+
* This sample just demonstrates that the time out customization/changing
26+
* works fine
27+
*/
28+
public class AndroidPageObjectTest_TimeOutManagement {
29+
30+
private WebDriver driver;
31+
private PageObjectWithCustomizedTimeOuts pageObjectWithCustomizedTimeOuts;
32+
private TimeOutDuration timeOutDuration;
33+
private final static long ACCEPTABLE_DELTA_MILLS = 1500; //Android UIAutomator sometimes
34+
//is very slow
35+
36+
@Before
37+
public void setUp() throws Exception {
38+
File classpathRoot = new File(System.getProperty("user.dir"));
39+
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
40+
File app = new File(appDir, "ApiDemos-debug.apk");
41+
DesiredCapabilities capabilities = new DesiredCapabilities();
42+
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
43+
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
44+
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
45+
timeOutDuration = new TimeOutDuration(1, TimeUnit.SECONDS); /* This object can be passed through
46+
the constructor of AppiumFieldDecorator. It allows users to set general timeout
47+
of the waiting for elements conveniently and change it when it is needed.
48+
*/
49+
50+
pageObjectWithCustomizedTimeOuts = new PageObjectWithCustomizedTimeOuts();
51+
//This time out is set because test can be run on slow Android SDK emulator
52+
PageFactory.initElements(new AppiumFieldDecorator(driver, timeOutDuration),
53+
pageObjectWithCustomizedTimeOuts);
54+
}
55+
56+
@After
57+
public void tearDown() throws Exception {
58+
driver.quit();
59+
}
60+
61+
private static void checkTimeDifference(long expectedTime,
62+
TimeUnit timeUnit, long currentMillis) {
63+
long expectedMillis = TimeUnit.MILLISECONDS.convert(expectedTime,
64+
timeUnit);
65+
try {
66+
assertEquals(true,
67+
((currentMillis - expectedMillis) < ACCEPTABLE_DELTA_MILLS)
68+
&& ((currentMillis - expectedMillis) >= 0));
69+
}
70+
catch (Error e){
71+
String message = String.valueOf(expectedTime) + " " + timeUnit.toString() + " current duration in millis " +
72+
String.valueOf(currentMillis) + " Failed";
73+
throw new RuntimeException(message, e);
74+
}
75+
}
76+
77+
private long getBenchMark(List<MobileElement> stubElements) {
78+
long startMark = Calendar.getInstance().getTimeInMillis();
79+
stubElements.size();
80+
long endMark = Calendar.getInstance().getTimeInMillis();
81+
return endMark - startMark;
82+
}
83+
84+
/**
85+
* Please read about Page Object design pattern here:
86+
* https://code.google.com/p/selenium/wiki/PageObjects
87+
*/
88+
/**
89+
* Page Object best practice is to describe interactions with target
90+
* elements by methods. These methods describe business logic of the page/screen.
91+
* Here test interacts with lazy instantiated elements directly.
92+
* It was done so just for obviousness
93+
*/
94+
95+
@Test
96+
public void test() {
97+
checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT,
98+
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
99+
System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT)
100+
+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine");
101+
102+
timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS);
103+
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
104+
System.out.println("Change time: " + String.valueOf(15500000) + " "
105+
+ TimeUnit.MICROSECONDS.toString() + ": Fine");
106+
107+
timeOutDuration.setTime(3, TimeUnit.SECONDS);
108+
checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
109+
System.out.println("Change time: " + String.valueOf(3) + " "
110+
+ TimeUnit.SECONDS.toString() + ": Fine");
111+
}
112+
113+
@Test
114+
public void test2() {
115+
checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT,
116+
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
117+
System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT)
118+
+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine");
119+
120+
checkTimeDifference(5, TimeUnit.SECONDS,
121+
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2));
122+
System.out.println(String.valueOf(5)
123+
+ " " + TimeUnit.SECONDS.toString() + ": Fine");
124+
125+
126+
timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS);
127+
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
128+
System.out.println("Change time: " + String.valueOf(15500000) + " "
129+
+ TimeUnit.MICROSECONDS.toString() + ": Fine");
130+
131+
checkTimeDifference(5, TimeUnit.SECONDS,
132+
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2));
133+
System.out.println(String.valueOf(5)
134+
+ " " + TimeUnit.SECONDS.toString() + ": Fine");
135+
136+
}
137+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.saucelabs.appium.driver_service;
2+
3+
import io.appium.java_client.MobileElement;
4+
import io.appium.java_client.android.AndroidDriver;
5+
import io.appium.java_client.remote.MobileCapabilityType;
6+
import io.appium.java_client.service.local.AppiumDriverLocalService;
7+
import io.appium.java_client.service.local.AppiumServiceBuilder;
8+
import io.appium.java_client.service.local.flags.GeneralServerFlag;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
import org.openqa.selenium.Platform;
12+
import org.openqa.selenium.remote.DesiredCapabilities;
13+
14+
import java.io.File;
15+
import java.io.FileInputStream;
16+
import java.util.Properties;
17+
18+
/**
19+
* Please make sure that:
20+
* - there is an installed node.js and the path to the folder which contains node.js executable is defined at
21+
* PATH environmental variable
22+
* - there is an installed appium.js (via npm)
23+
* - all appium servers are shut down
24+
* - take a look at custom_node_path.properties. If these files don't exist then please
25+
* set your values or set up Appium for desktop
26+
*/
27+
28+
/*
29+
This sample shows how to use AppiumServiceBuilder and AppiumDriverLocalService
30+
*/
31+
public class DriverServiceSample {
32+
33+
private static Properties properties;
34+
35+
@BeforeClass
36+
public static void beforeClass() throws Exception{
37+
File file = new File("src/test/java/com/saucelabs/appium/driver_service/custom_node_path.properties");
38+
FileInputStream fileInput = new FileInputStream(file);
39+
properties = new Properties();
40+
properties.load(fileInput);
41+
fileInput.close();
42+
}
43+
44+
private static File findCustomNode(){
45+
Platform current = Platform.getCurrent();
46+
if (current.is(Platform.WINDOWS))
47+
return new File(String.valueOf(properties.get("path.to.custom.node.win")));
48+
49+
if (current.is(Platform.MAC))
50+
return new File(String.valueOf(properties.get("path.to.custom.node.macos")));
51+
52+
return new File(String.valueOf(properties.get("path.to.custom.node.linux")));
53+
}
54+
55+
@Test
56+
public void checkTheAbilityToStartADriverWithTheDefaultServerAndNode(){
57+
File classpathRoot = new File(System.getProperty("user.dir"));
58+
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
59+
File app = new File(appDir, "ApiDemos-debug.apk");
60+
DesiredCapabilities capabilities = new DesiredCapabilities();
61+
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
62+
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
63+
new AndroidDriver<MobileElement>(capabilities).quit();
64+
}
65+
66+
@Test
67+
public void checkTheAbilityToStartADriverWithTheDefaultServerAndNotDefaultNode(){
68+
System.setProperty(AppiumServiceBuilder.APPIUM_NODE_PROPERTY, findCustomNode().getAbsolutePath());
69+
70+
File classpathRoot = new File(System.getProperty("user.dir"));
71+
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
72+
File app = new File(appDir, "ApiDemos-debug.apk");
73+
74+
DesiredCapabilities capabilities = new DesiredCapabilities();
75+
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
76+
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
77+
78+
try {
79+
new AndroidDriver<MobileElement>(capabilities).quit();
80+
}
81+
finally {
82+
System.clearProperty(AppiumServiceBuilder.APPIUM_NODE_PROPERTY);
83+
}
84+
85+
}
86+
87+
@Test
88+
public void checkTheAbilityToBuildServiceAndStartADriver(){
89+
90+
File classpathRoot = new File(System.getProperty("user.dir"));
91+
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
92+
File app = new File(appDir, "ApiDemos-debug.apk");
93+
94+
AppiumServiceBuilder builder = new AppiumServiceBuilder().withAppiumJS(findCustomNode()).withArgument(GeneralServerFlag.APP,
95+
app.getAbsolutePath()).withArgument(GeneralServerFlag.LOG_LEVEL, "info").usingAnyFreePort() /*and so on*/;
96+
97+
DesiredCapabilities capabilities = new DesiredCapabilities();
98+
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
99+
100+
new AndroidDriver<MobileElement>(builder, capabilities).quit();
101+
new AndroidDriver<MobileElement>(builder.build(), capabilities).quit();
102+
103+
}
104+
105+
@Test
106+
public void checkTheAbilityToUseServiceSeparatelyFromADriver(){
107+
108+
File classpathRoot = new File(System.getProperty("user.dir"));
109+
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
110+
File app = new File(appDir, "ApiDemos-debug.apk");
111+
112+
AppiumServiceBuilder builder = new AppiumServiceBuilder().withAppiumJS(findCustomNode()).withArgument(GeneralServerFlag.APP,
113+
app.getAbsolutePath()).withArgument(GeneralServerFlag.LOG_LEVEL, "info").usingAnyFreePort() /*and so on*/;
114+
AppiumDriverLocalService service = builder.build();
115+
116+
DesiredCapabilities capabilities = new DesiredCapabilities();
117+
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
118+
119+
service.start();
120+
try {
121+
new AndroidDriver<MobileElement>(service.getUrl(), capabilities).quit();
122+
}
123+
finally {
124+
service.stop();
125+
}
126+
127+
}
128+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
path.to.custom.node.win=C:/Program Files (x86)/Appium/node_modules/appium/bin/appium.js
2+
path.to.custom.node.macos=/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js
3+
path.to.custom.node.linux=specify your path on your own

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

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

6363
/**
6464
* Page Object best practice is to describe interactions with target
65-
* elements by methods. This methods describe business logic of the page/screen.
65+
* elements by methods. These methods describe business logic of the page/screen.
6666
* Here test interacts with lazy instantiated elements directly.
6767
* It was done so just for obviousness
6868
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.saucelabs.appium.page_object;
2+
3+
import io.appium.java_client.MobileElement;
4+
import io.appium.java_client.pagefactory.WithTimeout;
5+
import org.openqa.selenium.support.FindBy;
6+
7+
import java.util.List;
8+
import java.util.concurrent.TimeUnit;
9+
10+
public class PageObjectWithCustomizedTimeOuts {
11+
12+
/**
13+
* Page Object best practice is to describe interactions with target
14+
* elements by methods. This methods describe business logic of the page/screen.
15+
* Here lazy instantiated elements are public.
16+
* It was done so just for obviousness
17+
*/
18+
19+
@FindBy(className = "OneClassWhichDoesNotExist")
20+
public List<MobileElement> stubElements;
21+
22+
/*Any timeout of the waiting for element/list of elements
23+
can be customized if the general time duration is
24+
not suitable. E.g. the element is rendered for a long time
25+
or the element is used just for instant checkings/assertions
26+
*/
27+
@WithTimeout(time = 5, unit = TimeUnit.SECONDS)
28+
@FindBy(className = "OneAnotherClassWhichDoesNotExist")
29+
public List<MobileElement> stubElements2;
30+
}

0 commit comments

Comments
 (0)