Skip to content

Commit a68ad44

Browse files
appium#129 appium#130 The swiping by direction
1 parent 9d2c42f commit a68ad44

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

src/main/java/io/appium/java_client/MobileElement.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,10 @@ public void swipe(int duration) {
8686
location.x + size.getWidth(), location.y + size.getHeight(),
8787
duration);
8888
}
89+
90+
91+
@Override
92+
public void swipe(SwipeElementDirection direction, int duration) {
93+
direction.swipe((AppiumDriver) parent, this, duration);
94+
}
8995
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.appium.java_client;
2+
3+
import org.openqa.selenium.Dimension;
4+
import org.openqa.selenium.Point;
5+
6+
public enum SwipeElementDirection {
7+
/**
8+
* Up from the center of the lower
9+
*/
10+
UP{
11+
@Override
12+
void swipe(AppiumDriver driver, MobileElement element, int duration){
13+
Point p = element.getCenter();
14+
Point location = element.getLocation();
15+
Dimension size = element.getSize();
16+
driver.swipe(p.getX(), location.getY() + size.getHeight(), p.getX(), location.getY(), duration);
17+
}
18+
},
19+
/**
20+
* Down from the center of the upper
21+
*/
22+
DOWN{
23+
@Override
24+
void swipe(AppiumDriver driver, MobileElement element, int duration){
25+
Point p = element.getCenter();
26+
Point location = element.getLocation();
27+
Dimension size = element.getSize();
28+
driver.swipe(p.getX(), location.getY(), p.getX(), location.getY() + size.getHeight(), duration);
29+
}
30+
},
31+
/**
32+
* To the left from the center of the rightmost
33+
*/
34+
LEFT{
35+
@Override
36+
void swipe(AppiumDriver driver, MobileElement element, int duration){
37+
Point p = element.getCenter();
38+
Point location = element.getLocation();
39+
Dimension size = element.getSize();
40+
driver.swipe(location.getX(), p.getY(), location.getX() + size.getWidth(), p.getY(), duration);
41+
}
42+
},
43+
/**
44+
* To the right from the center of the leftmost
45+
*/
46+
RIGHT{
47+
@Override
48+
void swipe(AppiumDriver driver, MobileElement element, int duration){
49+
Point p = element.getCenter();
50+
Point location = element.getLocation();
51+
Dimension size = element.getSize();
52+
driver.swipe(location.getX() + size.getWidth(), p.getY(), location.getX(), p.getY(), duration);
53+
}
54+
};
55+
56+
void swipe(AppiumDriver driver, MobileElement element, int duration){}
57+
}

src/main/java/io/appium/java_client/TouchableElement.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,15 @@ public interface TouchableElement extends WebElement {
4747
* take
4848
*/
4949
public void swipe(int duration);
50+
51+
/**
52+
* Convenience method for swiping on the given element to the given direction
53+
*
54+
* @param direction UP, DOWN, LEFT, RIGHT
55+
*
56+
* @param duration amount of time in milliseconds for the entire swipe action to
57+
* take
58+
*/
59+
public void swipe(SwipeElementDirection direction, int duration);
5060

5161
}

src/test/java/io/appium/java_client/android/AndroidGestureTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import io.appium.java_client.MobileBy;
2323
import io.appium.java_client.MobileElement;
2424
import io.appium.java_client.MultiTouchAction;
25+
import io.appium.java_client.SwipeElementDirection;
2526
import io.appium.java_client.TouchAction;
2627
import io.appium.java_client.android.AndroidDriver;
2728
import io.appium.java_client.remote.MobileCapabilityType;
2829

2930
import java.io.File;
3031
import java.net.URL;
32+
import java.util.concurrent.TimeUnit;
3133

3234
import org.junit.After;
3335
import org.junit.Before;
@@ -98,11 +100,19 @@ public void TapSingleFingerTest() throws InterruptedException {
98100

99101
@Test
100102
public void elementGestureTest(){
103+
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
101104
MobileElement e = (MobileElement) driver.findElement(MobileBy.AccessibilityId("App"));
102105
e.tap(1, 1500);
103106
MobileElement e2 = (MobileElement) driver.findElement(MobileBy.AccessibilityId("Activity"));
104107
e2.zoom();
105108
e2.swipe(2000);
106-
e2.pinch();
109+
e2.swipe(SwipeElementDirection.UP,2000);
110+
e2.swipe(SwipeElementDirection.DOWN,2000);
111+
e2.swipe(SwipeElementDirection.LEFT,100);
112+
MobileElement e3 = (MobileElement) driver.findElement(MobileBy.AccessibilityId("Custom Title"));
113+
e3.swipe(SwipeElementDirection.LEFT,100);
114+
MobileElement e4 = (MobileElement) driver.findElement(MobileBy.AccessibilityId("Change Left"));
115+
e4.pinch();
116+
e4.swipe(SwipeElementDirection.RIGHT,100);
107117
}
108118
}

src/test/java/io/appium/java_client/ios/iOSGestureTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.appium.java_client.AppiumDriver;
2121
import io.appium.java_client.MobileElement;
2222
import io.appium.java_client.MultiTouchAction;
23+
import io.appium.java_client.SwipeElementDirection;
2324
import io.appium.java_client.TouchAction;
2425
import io.appium.java_client.ios.IOSDriver;
2526
import io.appium.java_client.remote.MobileCapabilityType;
@@ -130,12 +131,16 @@ public void TapSingleFingerTest() {
130131
driver.tap(1,100,200,1000);
131132
}
132133

133-
@Test
134+
@Test
134135
public void elementGestureTest(){
135-
MobileElement e = (MobileElement) driver.findElementByName("TextField1");
136+
MobileElement e = (MobileElement) driver.findElementByName("TextField1");
136137
e.tap(1, 1500);
137138
e.zoom();
138139
e.swipe(2000);
139140
e.pinch();
141+
e.swipe(SwipeElementDirection.UP,2000);
142+
e.swipe(SwipeElementDirection.DOWN,2000);
143+
e.swipe(SwipeElementDirection.LEFT,2000);
144+
e.swipe(SwipeElementDirection.RIGHT,2000);
140145
}
141146
}

0 commit comments

Comments
 (0)