Skip to content

Commit 2908a55

Browse files
Merge pull request appium-boneyard#92 from vikramvi/fixes_for_java
First round of fixes for java related examples
2 parents c5f53d5 + 5e33325 commit 2908a55

14 files changed

Lines changed: 72 additions & 67 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.4.1</version>
20+
<version>4.0.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_Simple.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public class AndroidPageObjectTest_Simple {
2727

2828
@Before
2929
public void setUp() throws Exception {
30-
File appDir = new File("src/test/java/io/appium/java_client");
30+
File classpathRoot = new File(System.getProperty("user.dir"));
31+
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
3132
File app = new File(appDir, "ApiDemos-debug.apk");
3233
DesiredCapabilities capabilities = new DesiredCapabilities();
3334
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
@@ -73,14 +74,14 @@ public void checkThatElementsWereNotFoundByIOSUIAutomator(){
7374

7475
@Test
7576
public void checkThatElementWasNotFoundByIOSUIAutomator(){
76-
NoSuchElementException nsee = null;
77+
String nsee = null;
7778
try{
7879
apiDemosPageObject.iosTextView.getAttribute("text");
7980
}
8081
catch (Exception e){
81-
nsee = (NoSuchElementException) e;
82+
nsee = e.getClass().getName();
8283
}
83-
Assert.assertNotNull(nsee);
84+
Assert.assertEquals(nsee,"org.openqa.selenium.NoSuchElementException");
8485
}
8586

8687
@Test

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

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

4141
@Test
4242
public void apiDemo(){
43-
WebElement el = driver.findElement(By.xpath(".//*[@text=Animation]"));
43+
WebElement el = driver.findElement(By.xpath(".//*[@text='Animation']"));
4444
assertEquals("Animation", el.getText());
4545
el = driver.findElementByClassName("android.widget.TextView");
4646
assertEquals("API Demos", el.getText());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void setUp() throws Exception {
3535
DesiredCapabilities capabilities = new DesiredCapabilities();
3636
capabilities.setCapability("deviceName", "iPhone 6");
3737
capabilities.setCapability("platformName", "iOS");
38-
capabilities.setCapability("platformVersion", "8.1");
38+
capabilities.setCapability("platformVersion", "9.3");
3939
capabilities.setCapability("browserName", "safari");
4040
driver = new IOSDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"),
4141
capabilities);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public void setUp() throws Exception {
6969
String sauceUserName = authentication.getUsername();
7070
String sauceAccessKey = authentication.getAccessKey();
7171
DesiredCapabilities capabilities = new DesiredCapabilities();
72-
capabilities.setCapability("platformVersion", "7.1");
73-
capabilities.setCapability("deviceName", "iPhone Simulator");
72+
capabilities.setCapability("platformVersion", "9.3");
73+
capabilities.setCapability("deviceName", "iPhone 6");
7474
capabilities.setCapability("appiumVersion", "1.3.4");
7575
capabilities.setCapability("app", "https://appium.s3.amazonaws.com/TestApp7.1.app.zip");
7676

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import org.apache.http.client.methods.HttpGet;
1919
import org.apache.http.impl.client.DefaultHttpClient;
2020
import org.apache.http.util.EntityUtils;
21+
2122
import org.json.simple.JSONObject;
23+
import org.json.simple.JSONArray;
2224
import org.json.simple.parser.JSONParser;
25+
2326
import org.junit.After;
2427
import org.junit.Before;
2528
import org.junit.Test;
@@ -52,7 +55,7 @@ public void setUp() throws Exception {
5255
File appDir = new File(System.getProperty("user.dir"), "../../../apps/TestApp/build/release-iphonesimulator");
5356
File app = new File(appDir, "TestApp.app");
5457
DesiredCapabilities capabilities = new DesiredCapabilities();
55-
capabilities.setCapability("platformVersion", "8.1");
58+
capabilities.setCapability("platformVersion", "9.3");
5659
capabilities.setCapability("deviceName", "iPhone 6");
5760
capabilities.setCapability("app", app.getAbsolutePath());
5861
driver = new IOSDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
@@ -116,7 +119,7 @@ public void testBasicAlert() throws Exception {
116119
@Test
117120
public void testBasicButton() throws Exception {
118121
WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));
119-
assertEquals("ComputeSumButton", button.getText());
122+
assertEquals("Compute Sum", button.getText());
120123
}
121124

122125
@Test
@@ -168,16 +171,17 @@ public void testFindElementsByClassName() throws Exception {
168171
assertEquals(String.valueOf(number), sumLabel.getText());
169172
}
170173

174+
//Fix for Simulator https://github.com/appium/appium/issues/6315
171175
@Test
172176
public void testAttribute() throws Exception {
173-
Random random = new Random();
177+
Random random = new Random();
174178

175179
WebElement text = driver.findElement(By.xpath("//UIATextField[1]"));
176180

177181
int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM;
178182
text.sendKeys(String.valueOf(number));
179183

180-
assertEquals("TextField1", text.getAttribute("name"));
184+
assertEquals("IntegerA", text.getAttribute("name"));
181185
assertEquals("TextField1", text.getAttribute("label"));
182186
assertEquals(String.valueOf(number), text.getAttribute("value"));
183187
}
@@ -199,21 +203,26 @@ public void testLocation() throws Exception {
199203

200204
Point location = button.getLocation();
201205

202-
assertEquals(94, location.getX());
203-
assertEquals(122, location.getY());
206+
assertEquals(110, location.getX());
207+
assertEquals(143, location.getY());
204208
}
205209

210+
//https://jsonformatter.curiousconcept.com/
211+
206212
@Test
207213
public void testSessions() throws Exception {
208214
HttpGet request = new HttpGet("http://localhost:4723/wd/hub/sessions");
209215
@SuppressWarnings("resource")
210216
HttpClient httpClient = new DefaultHttpClient();
211217
HttpResponse response = httpClient.execute(request);
212218
HttpEntity entity = response.getEntity();
219+
213220
JSONObject jsonObject = (JSONObject) new JSONParser().parse(EntityUtils.toString(entity));
214-
221+
JSONArray lang= (JSONArray) jsonObject.get("value");
222+
JSONObject innerObj = (JSONObject) lang.iterator().next();
223+
215224
String sessionId = driver.getSessionId().toString();
216-
assertEquals(jsonObject.get("sessionId"), sessionId);
225+
assertEquals(innerObj.get("id"), sessionId);
217226
}
218227

219228
@Test

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

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.http.client.methods.HttpGet;
2222
import org.apache.http.impl.client.DefaultHttpClient;
2323
import org.apache.http.util.EntityUtils;
24+
import org.json.simple.JSONArray;
2425
import org.json.simple.JSONObject;
2526
import org.json.simple.parser.JSONParser;
2627
import org.junit.After;
@@ -43,6 +44,10 @@
4344
* with the 'UICatalog' iPhone project which is included in the Appium source distribution.
4445
*
4546
* @author Ross Rowe
47+
*
48+
* Running below Test Cases on Simulator needs few steps as below
49+
* Unzip UICatalog.zip and build for simulator per below blog
50+
* http://samwize.com/2015/03/11/xcode-commands-to-build-app-and-run-on-simulator/
4651
*/
4752
@SuppressWarnings("deprecation")
4853
public class UICatalogTest {
@@ -58,7 +63,7 @@ public void setUp() throws Exception {
5863
File appDir = new File(classpathRoot, "../../../apps/UICatalog/build/release-iphonesimulator");
5964
File app = new File(appDir, "UICatalog.app");
6065
DesiredCapabilities capabilities = new DesiredCapabilities();
61-
capabilities.setCapability("platformVersion", "8.1");
66+
capabilities.setCapability("platformVersion", "9.3");
6267
capabilities.setCapability("deviceName", "iPhone 6");
6368
capabilities.setCapability("app", app.getAbsolutePath());
6469
driver = new IOSDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
@@ -90,9 +95,9 @@ public void testFindElement() throws Exception {
9095
assertNotNull(table);
9196
//is number of cells/rows inside table correct
9297
List<MobileElement> rows = table.findElementsByClassName("UIATableCell");
93-
assertEquals(12, rows.size());
98+
assertEquals(18, rows.size());
9499
//is first one about buttons
95-
assertEquals("Buttons, Various uses of UIButton", rows.get(0).getAttribute("name"));
100+
assertEquals("Action Sheets", rows.get(0).getAttribute("name"));
96101
//navigationBar is not inside table
97102
WebElement nav_bar = null;
98103
try {
@@ -128,40 +133,17 @@ public void testScreenshot() {
128133
assertNotNull(file);
129134
}
130135

131-
@Test
132-
public void testTextFieldEdit() {
133-
//go to the text fields section
134-
openMenuPosition(2);
135-
WebElement text_field = driver.findElementsByClassName("UIATextField").get(0);
136-
//get default/empty text
137-
String default_val = text_field.getAttribute("value");
138-
//write some random text to element
139-
String rnd_string = RandomStringUtils.randomAlphanumeric(6);
140-
text_field.sendKeys(rnd_string);
141-
assertEquals(rnd_string, text_field.getAttribute("value"));
142-
//send some random keys
143-
String rnd_string2 = RandomStringUtils.randomAlphanumeric(6);
144-
Actions swipe = new Actions(driver).sendKeys(rnd_string2);
145-
swipe.perform();
146-
//check if text is there
147-
assertEquals(rnd_string + rnd_string2, text_field.getAttribute("value"));
148-
//clear
149-
text_field.clear();
150-
//check if is empty/has default text
151-
assertEquals(default_val, text_field.getAttribute("value"));
152-
}
153-
154136
@Test
155137
public void testAlertInteraction() {
156138
//go to the alerts section
157-
openMenuPosition(10);
139+
openMenuPosition(2);
158140

159141
//trigger modal alert with cancel & ok buttons
160-
List<IOSElement> triggerOkCancel = driver.findElementsByAccessibilityId("Show OK-Cancel");
161-
triggerOkCancel.get(1).click();
142+
List<IOSElement> triggerOkCancel = driver.findElementsByXPath("//UIATableCell[2]/UIAStaticText[1]");
143+
triggerOkCancel.get(0).click();
162144
Alert alert = driver.switchTo().alert();
163145
//check if title of alert is correct
164-
assertEquals("UIAlertView <Alert message>", alert.getText());
146+
assertEquals("A Short Title Is Best A message should be a short, complete sentence.", alert.getText());
165147
alert.accept();
166148
}
167149

@@ -183,12 +165,12 @@ public void testScroll() {
183165
@Test
184166
public void testSlider() {
185167
//go to controls
186-
openMenuPosition(1);
168+
openMenuPosition(10);
187169
//get the slider
188170
WebElement slider = driver.findElementByClassName("UIASlider");
189-
assertEquals("50%", slider.getAttribute("value"));
171+
assertEquals("42%", slider.getAttribute("value"));
190172
Point sliderLocation = getCenter(slider);
191-
driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()-100, sliderLocation.getY(), 1);
173+
driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()-sliderLocation.getX(), sliderLocation.getY(), 1);
192174
assertEquals("0%", slider.getAttribute("value"));
193175
}
194176

@@ -201,8 +183,11 @@ public void testSessions() throws Exception {
201183
HttpEntity entity = response.getEntity();
202184
JSONObject jsonObject = (JSONObject) new JSONParser().parse(EntityUtils.toString(entity));
203185

186+
JSONArray lang= (JSONArray) jsonObject.get("value");
187+
JSONObject innerObj = (JSONObject) lang.iterator().next();
188+
204189
String sessionId = driver.getSessionId().toString();
205-
assertEquals(jsonObject.get("sessionId"), sessionId);
190+
assertEquals(innerObj.get("id"), sessionId);
206191
}
207192

208193
@Test
@@ -218,13 +203,13 @@ public void testSource() {
218203
//get main view soruce
219204
String source_main = driver.getPageSource();
220205
assertTrue(source_main.contains("UIATableView"));
221-
assertTrue(source_main.contains("TextFields, Uses of UITextField"));
206+
assertTrue(source_main.contains("Text Fields"));
222207

223208
//got to text fields section
224-
openMenuPosition(2);
209+
openMenuPosition(13);
225210
String source_textfields = driver.getPageSource();
226-
assertTrue(source_textfields.contains("UIAStaticText"));
227-
assertTrue(source_textfields.contains("TextFields"));
211+
//assertTrue(source_textfields.contains("UIAStaticText"));
212+
assertTrue(source_textfields.contains("UIATextField"));
228213

229214
assertNotSame(source_main, source_textfields);
230215
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public void checkTheAbilityToStartADriverWithTheDefaultServerAndNode(){
6363
new AndroidDriver<MobileElement>(capabilities).quit();
6464
}
6565

66+
//Eclipse Workaround Refer To - https://github.com/appium/java-client/issues/416
6667
@Test
6768
public void checkTheAbilityToStartADriverWithTheDefaultServerAndNotDefaultNode(){
6869
System.setProperty(AppiumServiceBuilder.APPIUM_PATH, findCustomNode().getAbsolutePath());
@@ -91,8 +92,11 @@ public void checkTheAbilityToBuildServiceAndStartADriver(){
9192
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
9293
File app = new File(appDir, "ApiDemos-debug.apk");
9394

94-
AppiumServiceBuilder builder = new AppiumServiceBuilder().withAppiumJS(findCustomNode()).withArgument(GeneralServerFlag.APP,
95-
app.getAbsolutePath()).withArgument(GeneralServerFlag.LOG_LEVEL, "info").usingAnyFreePort() /*and so on*/;
95+
AppiumServiceBuilder builder = new AppiumServiceBuilder().
96+
withAppiumJS(findCustomNode()).
97+
//withArgument(GeneralServerFlag.APP,app.getAbsolutePath()).
98+
withArgument(GeneralServerFlag.LOG_LEVEL, "info").
99+
usingAnyFreePort() /*and so on*/;
96100

97101
DesiredCapabilities capabilities = new DesiredCapabilities();
98102
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
@@ -109,8 +113,11 @@ public void checkTheAbilityToUseServiceSeparatelyFromADriver(){
109113
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
110114
File app = new File(appDir, "ApiDemos-debug.apk");
111115

112-
AppiumServiceBuilder builder = new AppiumServiceBuilder().withAppiumJS(findCustomNode()).withArgument(GeneralServerFlag.APP,
113-
app.getAbsolutePath()).withArgument(GeneralServerFlag.LOG_LEVEL, "info").usingAnyFreePort() /*and so on*/;
116+
AppiumServiceBuilder builder = new AppiumServiceBuilder().
117+
withAppiumJS(findCustomNode()).
118+
//withArgument(GeneralServerFlag.APP,app.getAbsolutePath()).
119+
withArgument(GeneralServerFlag.LOG_LEVEL, "info").
120+
usingAnyFreePort() /*and so on*/;
114121
AppiumDriverLocalService service = builder.build();
115122

116123
DesiredCapabilities capabilities = new DesiredCapabilities();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
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
2+
path.to.custom.node.macos=/usr/local/lib/node_modules/appium/build/lib/main.js
33
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public void setUp() throws Exception {
4545
File app = new File(appDir, "TestApp.app");
4646
DesiredCapabilities capabilities = new DesiredCapabilities();
4747
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "");
48-
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1");
49-
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone Simulator");
48+
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9.3");
49+
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 6");
5050
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
5151

5252
uiTestApp = new TestAppScreenSimple();

0 commit comments

Comments
 (0)