Skip to content

Commit 554710e

Browse files
saikrishna321SrinivasanTarget
authored andcommitted
refactored android test to latest java-client dependency (appium-boneyard#113)
* refactored android test to latest java-client dependency * starting appium-server using LocalService * removed AndroidFindBySet * updated to beta6 and fixed reviews
1 parent 0c60d06 commit 554710e

9 files changed

Lines changed: 153 additions & 252 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
<dependency>
1212
<groupId>junit</groupId>
1313
<artifactId>junit</artifactId>
14-
<version>4.11</version>
14+
<version>4.12</version>
1515
<scope>test</scope>
1616
</dependency>
1717
<dependency>
1818
<groupId>io.appium</groupId>
1919
<artifactId>java-client</artifactId>
20-
<version>4.0.0</version>
21-
</dependency>
20+
<version>5.0.0-BETA6</version>
21+
</dependency>
2222
<dependency>
2323
<groupId>com.googlecode.json-simple</groupId>
2424
<artifactId>json-simple</artifactId>
@@ -35,7 +35,7 @@
3535
<dependency>
3636
<groupId>com.saucelabs</groupId>
3737
<artifactId>sauce_junit</artifactId>
38-
<version>2.1.3</version>
38+
<version>2.1.23</version>
3939
<scope>test</scope>
4040
</dependency>
4141
<dependency>

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

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,22 @@
11
package com.saucelabs.appium;
22

3-
import io.appium.java_client.AppiumDriver;
43
import io.appium.java_client.MobileElement;
54
import io.appium.java_client.TouchAction;
6-
import io.appium.java_client.android.AndroidDriver;
7-
import junit.framework.Assert;
8-
import org.junit.After;
9-
import org.junit.Before;
105
import org.junit.Test;
116
import org.openqa.selenium.WebElement;
12-
import org.openqa.selenium.remote.DesiredCapabilities;
137

14-
import java.io.File;
15-
import java.net.URL;
16-
17-
/**
18-
* Created by saikrisv on 26/04/16.
19-
*/
20-
public class AndroidDragAndDrop {
21-
private AppiumDriver<WebElement> driver;
22-
23-
@Before
24-
public void setUp() throws Exception {
25-
File classpathRoot = new File(System.getProperty("user.dir"));
26-
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
27-
File app = new File(appDir, "ApiDemos-debug.apk");
28-
DesiredCapabilities capabilities = new DesiredCapabilities();
29-
capabilities.setCapability("deviceName","Android Emulator");
30-
capabilities.setCapability("platformVersion", "4.4");
31-
capabilities.setCapability("app", app.getAbsolutePath());
32-
capabilities.setCapability("appPackage", "io.appium.android.apis");
33-
capabilities.setCapability("appActivity", ".ApiDemos");
34-
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
35-
}
36-
37-
@After
38-
public void tearDown() throws Exception {
39-
driver.quit();
40-
}
8+
import static org.junit.Assert.assertEquals;
419

10+
public class AndroidDragAndDrop extends BaseDriver {
4211
@Test
4312
public void testDragAndDrop() throws InterruptedException {
44-
driver.findElementByXPath(".//*[@text='Views']").click();
45-
driver.findElementByXPath(".//*[@text='Drag and Drop']").click();
13+
scrollTo("Views").click();
14+
scrollTo("Drag and Drop").click();
4615
MobileElement calc = (MobileElement) driver.findElementById("io.appium.android.apis:id/drag_dot_1");
4716
TouchAction touchAction = new TouchAction(driver);
48-
touchAction.press(calc).perform();
49-
try {
50-
Thread.sleep(5000);
51-
} catch (InterruptedException e) {
52-
e.printStackTrace();
53-
}
54-
touchAction.moveTo(driver.findElementById("io.appium.android.apis:id/drag_dot_2")).release().perform();
17+
WebElement destination = driver.findElementById("io.appium.android.apis:id/drag_dot_2");
18+
touchAction.longPress(calc).waitAction(6000).moveTo(destination).perform().release();
5519
Thread.sleep(5000);
56-
Assert.assertEquals(driver.findElementById("io.appium.android.apis:id/drag_result_text").getText(),"Dropped!");
20+
assertEquals(driver.findElementById("io.appium.android.apis:id/drag_result_text").getText(), "Dropped!");
5721
}
5822
}

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

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,22 @@
11
package com.saucelabs.appium;
22

3-
import io.appium.java_client.AppiumDriver;
4-
import io.appium.java_client.android.AndroidDriver;
5-
import org.junit.After;
6-
import org.junit.Before;
3+
import io.appium.java_client.TouchAction;
74
import org.junit.Test;
85
import org.openqa.selenium.Dimension;
96
import org.openqa.selenium.Point;
107
import org.openqa.selenium.WebElement;
11-
import org.openqa.selenium.remote.DesiredCapabilities;
128

13-
import java.io.File;
14-
import java.net.URL;
15-
16-
/**
17-
* Created by saikrisv on 26/04/16.
18-
*/
19-
public class AndroidSlideTest {
20-
private AppiumDriver<WebElement> driver;
21-
22-
@Before
23-
public void setUp() throws Exception {
24-
File classpathRoot = new File(System.getProperty("user.dir"));
25-
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
26-
File app = new File(appDir, "ApiDemos-debug.apk");
27-
DesiredCapabilities capabilities = new DesiredCapabilities();
28-
capabilities.setCapability("deviceName","Android Emulator");
29-
capabilities.setCapability("platformVersion", "4.4");
30-
capabilities.setCapability("app", app.getAbsolutePath());
31-
capabilities.setCapability("appPackage", "io.appium.android.apis");
32-
capabilities.setCapability("appActivity", ".ApiDemos");
33-
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
34-
}
35-
36-
@After
37-
public void tearDown() throws Exception {
38-
driver.quit();
39-
}
9+
public class AndroidSlideTest extends BaseDriver {
4010

4111
@Test
4212
public void testSlider(){
43-
driver.findElementByXPath(".//*[@text='Views']").click();
44-
driver.scrollTo("Seek Bar").click();
13+
scrollTo("Views").click();
14+
scrollTo("Seek Bar").click();
4515

4616
WebElement slider = driver.findElementById("io.appium.android.apis:id/seek");
4717
Point sliderLocation = getCenter(slider);
48-
driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()-100, sliderLocation.getY(), 7000);
18+
new TouchAction(driver).press(sliderLocation.getX(),sliderLocation.getY())
19+
.moveTo(sliderLocation.getX() / 2 , sliderLocation.getY()).perform().release();
4920

5021
}
5122

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

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
11
package com.saucelabs.appium;
22

3-
import io.appium.java_client.AppiumDriver;
4-
import io.appium.java_client.android.AndroidDriver;
5-
import org.junit.After;
6-
import org.junit.Before;
73
import org.junit.Test;
84
import org.openqa.selenium.By;
95
import org.openqa.selenium.WebElement;
10-
import org.openqa.selenium.remote.DesiredCapabilities;
116

12-
import java.io.File;
13-
import java.net.URL;
147
import java.util.List;
158

169
import static org.junit.Assert.assertEquals;
1710

18-
public class AndroidTest {
11+
public class AndroidTest extends BaseDriver{
1912

20-
private AppiumDriver<WebElement> driver;
21-
22-
@Before
23-
public void setUp() throws Exception {
24-
File classpathRoot = new File(System.getProperty("user.dir"));
25-
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
26-
File app = new File(appDir, "ApiDemos-debug.apk");
27-
DesiredCapabilities capabilities = new DesiredCapabilities();
28-
capabilities.setCapability("deviceName","Android Emulator");
29-
capabilities.setCapability("platformVersion", "4.4");
30-
capabilities.setCapability("app", app.getAbsolutePath());
31-
capabilities.setCapability("appPackage", "io.appium.android.apis");
32-
capabilities.setCapability("appActivity", ".ApiDemos");
33-
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
34-
}
35-
36-
@After
37-
public void tearDown() throws Exception {
38-
driver.quit();
39-
}
4013

4114
@Test
4215
public void apiDemo(){
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.saucelabs.appium;
2+
3+
import io.appium.java_client.AppiumDriver;
4+
import io.appium.java_client.MobileBy;
5+
import io.appium.java_client.MobileElement;
6+
import io.appium.java_client.android.AndroidDriver;
7+
import io.appium.java_client.service.local.AppiumDriverLocalService;
8+
import io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException;
9+
import org.junit.After;
10+
import org.junit.Before;
11+
import org.openqa.selenium.WebElement;
12+
import org.openqa.selenium.remote.DesiredCapabilities;
13+
14+
import java.io.File;
15+
import java.net.URL;
16+
17+
public class BaseDriver {
18+
public AppiumDriver<WebElement> driver;
19+
private static AppiumDriverLocalService service;
20+
21+
@Before
22+
public void setUp() throws Exception {
23+
service = AppiumDriverLocalService.buildDefaultService();
24+
service.start();
25+
26+
if (service == null || !service.isRunning()) {
27+
throw new AppiumServerHasNotBeenStartedLocallyException(
28+
"An appium server node is not started!");
29+
}
30+
File classpathRoot = new File(System.getProperty("user.dir"));
31+
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
32+
File app = new File(appDir.getCanonicalPath(), "ApiDemos-debug.apk");
33+
DesiredCapabilities capabilities = new DesiredCapabilities();
34+
capabilities.setCapability("deviceName","Android Emulator");
35+
capabilities.setCapability("app", app.getAbsolutePath());
36+
capabilities.setCapability("appPackage", "io.appium.android.apis");
37+
capabilities.setCapability("appActivity", ".ApiDemos");
38+
driver = new AndroidDriver<>(service.getUrl(), capabilities);
39+
}
40+
41+
@After
42+
public void tearDown() throws Exception {
43+
if (driver != null) {
44+
driver.quit();
45+
}
46+
if (service != null) {
47+
service.stop();
48+
}
49+
}
50+
51+
52+
public MobileElement scrollTo(String text){
53+
return (MobileElement) driver.findElement(MobileBy.
54+
AndroidUIAutomator("new UiScrollable(new UiSelector()"
55+
+ ".scrollable(true)).scrollIntoView(resourceId(\"android:id/list\")).scrollIntoView("
56+
+ "new UiSelector().text(\""+text+"\"))"));
57+
}
58+
59+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public class MobileFindJavaTest {
1717

1818
@Test
1919
public void apiDemo() throws Exception {
20-
driver.scrollTo("about phone");
21-
driver.scrollTo("bluetooth");
20+
//driver.scrollTo("about phone");
21+
//driver.scrollTo("bluetooth");
2222
}
2323

2424

sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/page_object/android/ApiDemosListViewScreenByAllPossible.java

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,46 @@
77
import io.appium.java_client.android.AndroidElement;
88
import io.appium.java_client.pagefactory.AndroidFindAll;
99
import io.appium.java_client.pagefactory.AndroidFindBy;
10+
import io.appium.java_client.pagefactory.HowToUseLocators;
11+
12+
import static io.appium.java_client.pagefactory.LocatorGroupStrategy.ALL_POSSIBLE;
1013

1114
/**
12-
*
1315
* Here is the common sample shows how to use
14-
* {@link AndroidFindAll} annotation to describe all possible
16+
* {@link AndroidFindBy} annotation to describe all possible
1517
* locators of a target element. This feature should help to
1618
* reduce the number of page PageObject-classes
17-
*
19+
* <p>
1820
* It demonstrates how to declare screen elements using Appium
1921
* page objects facilities.
20-
*
22+
* <p>
2123
* About Page Object design pattern read here:
2224
* https://code.google.com/p/selenium/wiki/PageObjects
23-
*
2425
*/
2526
public class ApiDemosListViewScreenByAllPossible {
26-
27-
/**
28-
* Page Object best practice is to describe interactions with target
29-
* elements by methods. This methods describe business logic of the page/screen.
30-
* Here lazy instantiated elements are public.
31-
* It was done so just for obviousness
32-
*/
33-
34-
@AndroidFindAll({
35-
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/Fakecontent\")"),
36-
@AndroidFindBy(id = "android:id/Faketext1"),
37-
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")"), //by this locator element is found
38-
@AndroidFindBy(id = "android:id/FakeId")
39-
}) //If here is a list then it will be filled by all elements
40-
//which are found using all declared locators
41-
public List<MobileElement> findAllElementViews;
42-
43-
@AndroidFindAll({
44-
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/Fakecontent\")"),
45-
@AndroidFindBy(id = "android:id/Faketext1"),
46-
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")"), //by this locator element is found
47-
@AndroidFindBy(id = "android:id/FakeId")
48-
}) //If here is a single element then it will find the first
49-
//element that is found by any of declared locators
50-
public AndroidElement findAllElementView;
27+
28+
/**
29+
* Page Object best practice is to describe interactions with target
30+
* elements by methods. This methods describe business logic of the page/screen.
31+
* Here lazy instantiated elements are public.
32+
* It was done so just for obviousness
33+
*/
34+
35+
@HowToUseLocators(androidAutomation = ALL_POSSIBLE)
36+
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/Fakecontent\")")
37+
@AndroidFindBy(id = "android:id/Faketext1")
38+
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")") //by this locator element is found
39+
@AndroidFindBy(id = "android:id/FakeId")//If here is a list then it will be filled by all elements
40+
//which are found using all declared locators
41+
public List<MobileElement> findAllElementViews;
42+
43+
@HowToUseLocators(androidAutomation = ALL_POSSIBLE)
44+
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/Fakecontent\")")
45+
@AndroidFindBy(id = "android:id/Faketext1")
46+
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")")
47+
//by this locator element is found
48+
@AndroidFindBy(id = "android:id/FakeId")
49+
//If here is a single element then it will find the first
50+
//element that is found by any of declared locators
51+
public AndroidElement findAllElementView;
5152
}

0 commit comments

Comments
 (0)