Skip to content

Commit 166a0cb

Browse files
#160 is fixed
1 parent 0dc816c commit 166a0cb

File tree

5 files changed

+523
-346
lines changed

5 files changed

+523
-346
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public void zoom() {
7070

7171
@Override
7272
public void swipe(SwipeElementDirection direction, int duration) {
73-
direction.swipe((AppiumDriver) parent, this, 0, 0, 0, 0, duration);
73+
direction.swipe((AppiumDriver) parent, this, 0, 0, duration);
74+
}
75+
76+
@Override
77+
public void swipe(SwipeElementDirection direction, int offset1,
78+
int offset2, int duration) throws IllegalCoordinatesException {
79+
direction.swipe((AppiumDriver) parent, this, offset1, offset2, duration);
7480
}
7581
}

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

+153-31
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,181 @@ public enum SwipeElementDirection {
99
*/
1010
UP{
1111
@Override
12-
void swipe(AppiumDriver driver, MobileElement element,
13-
int xOffsetStart, int xOffsetEnd, int yOffsetStart,
14-
int yOffsetEnd, int duration) throws IllegalCoordinatesException {
15-
Point p = element.getCenter();
16-
Point location = element.getLocation();
17-
Dimension size = element.getSize();
18-
driver.swipe(p.getX(), location.getY() + size.getHeight(), p.getX(), location.getY(), duration);
12+
int getStartX(Point center, Point location, Dimension size, int ignored) {
13+
return center.getX();
14+
}
15+
16+
@Override
17+
int getStartY(Point center, Point location, Dimension size, int offSet) {
18+
int result = location.getY() + size.getHeight() - offSet;
19+
checkYCoordinate(result, location, size, offSet);
20+
return result;
21+
}
22+
23+
@Override
24+
int getEndX(Point center, Point location, Dimension size, int ignored) {
25+
return center.getX();
26+
}
27+
28+
@Override
29+
int getEndY(Point center, Point location, Dimension size, int offSet) {
30+
int result = location.getY() + offSet;
31+
checkYCoordinate(result, location, size, offSet);
32+
return result;
33+
}
34+
35+
@Override
36+
void checkDirection(int x1, int y1, int x2, int y2) {
37+
if (y1 < y2)
38+
throw new IllegalCoordinatesException("Y1 " + y1 + " and Y2 " + y2 + " are inconsistent. It looks like you are "
39+
+ "trying to perform the swiping down");
1940
}
2041
},
2142
/**
2243
* Down from the center of the upper
2344
*/
2445
DOWN{
46+
2547
@Override
26-
void swipe(AppiumDriver driver, MobileElement element,
27-
int xOffsetStart, int xOffsetEnd, int yOffsetStart,
28-
int yOffsetEnd, int duration) throws IllegalCoordinatesException {
29-
Point p = element.getCenter();
30-
Point location = element.getLocation();
31-
Dimension size = element.getSize();
32-
driver.swipe(p.getX(), location.getY(), p.getX(), location.getY() + size.getHeight(), duration);
48+
int getStartX(Point center, Point location, Dimension size, int offSet) {
49+
return center.getX();
50+
}
51+
52+
@Override
53+
int getStartY(Point center, Point location, Dimension size, int offSet) {
54+
return UP.getEndY(center, location, size, offSet);
55+
}
56+
57+
@Override
58+
int getEndX(Point center, Point location, Dimension size, int offSet) {
59+
return center.getX();
60+
}
61+
62+
@Override
63+
int getEndY(Point center, Point location, Dimension size, int offSet) {
64+
return UP.getStartY(center, location, size, offSet);
65+
}
66+
67+
@Override
68+
void checkDirection(int x1, int y1, int x2, int y2) {
69+
if (y1 > y2)
70+
throw new IllegalCoordinatesException("Y1 " + y1 + " and Y2 " + y2 + " are inconsistent. It looks like you are "
71+
+ "trying to perform the swiping up");
3372
}
3473
},
3574
/**
3675
* To the left from the center of the rightmost
3776
*/
3877
LEFT{
78+
3979
@Override
40-
void swipe(AppiumDriver driver, MobileElement element,
41-
int xOffsetStart, int xOffsetEnd, int yOffsetStart,
42-
int yOffsetEnd, int duration) throws IllegalCoordinatesException {
43-
Point p = element.getCenter();
44-
Point location = element.getLocation();
45-
Dimension size = element.getSize();
46-
driver.swipe(location.getX() + size.getWidth(), p.getY(), location.getX(), p.getY(), duration);
80+
int getStartX(Point center, Point location, Dimension size, int offSet) {
81+
int result = location.getX() + size.getWidth() - offSet;
82+
checkXCoordinate(result, location, size, offSet);
83+
return result;
4784
}
85+
86+
@Override
87+
int getStartY(Point center, Point location, Dimension size, int offSet) {
88+
return center.getY();
89+
}
90+
91+
@Override
92+
int getEndX(Point center, Point location, Dimension size, int offSet) {
93+
int result = location.getX() + offSet;
94+
checkXCoordinate(result, location, size, offSet);
95+
return result;
96+
}
97+
98+
@Override
99+
int getEndY(Point center, Point location, Dimension size, int offSet) {
100+
return center.getY();
101+
}
102+
103+
@Override
104+
void checkDirection(int x1, int y1, int x2, int y2) {
105+
if (x1 < x2)
106+
throw new IllegalCoordinatesException("X1 " + x1 + " and X2 " + x2 + " are inconsistent. It looks like you are "
107+
+ "trying to perform the swiping right");
108+
109+
}
48110
},
49111
/**
50112
* To the right from the center of the leftmost
51113
*/
52114
RIGHT{
115+
116+
@Override
117+
int getStartX(Point center, Point location, Dimension size, int offSet) {
118+
return LEFT.getEndX(center, location, size, offSet);
119+
}
120+
121+
@Override
122+
int getStartY(Point center, Point location, Dimension size, int offSet) {
123+
return center.getY();
124+
}
125+
126+
@Override
127+
int getEndX(Point center, Point location, Dimension size, int offSet) {
128+
return LEFT.getStartX(center, location, size, offSet);
129+
}
130+
53131
@Override
54-
void swipe(AppiumDriver driver, MobileElement element,
55-
int xOffsetStart, int xOffsetEnd, int yOffsetStart,
56-
int yOffsetEnd, int duration) throws IllegalCoordinatesException {
57-
Point p = element.getCenter();
58-
Point location = element.getLocation();
59-
Dimension size = element.getSize();
60-
driver.swipe(location.getX(), p.getY(), location.getX()+ size.getWidth(), p.getY(), duration);
132+
int getEndY(Point center, Point location, Dimension size, int offSet) {
133+
return center.getY();
134+
}
135+
136+
@Override
137+
void checkDirection(int x1, int y1, int x2, int y2) {
138+
if (x1 > x2)
139+
throw new IllegalCoordinatesException("X1 " + x1 + " and X2 " + x2 + " are inconsistent. It looks like you are "
140+
+ "trying to perform the swiping left");
61141
}
62142
};
63143

64-
abstract void swipe(AppiumDriver driver, MobileElement element,
65-
int xOffsetStart, int xOffsetEnd, int yOffsetStart,
66-
int yOffsetEnd, int duration) throws IllegalCoordinatesException;
144+
abstract int getStartX(Point center, Point location, Dimension size, int offSet);
145+
abstract int getStartY(Point center, Point location, Dimension size, int offSet);
146+
abstract int getEndX(Point center, Point location, Dimension size, int offSet);
147+
abstract int getEndY(Point center, Point location, Dimension size, int offSet);
148+
abstract void checkDirection(int x1, int y1, int x2, int y2);
149+
150+
void swipe(AppiumDriver driver, MobileElement element,
151+
int offset1, int offset2, int duration) throws IllegalCoordinatesException{
152+
Point p = element.getCenter();
153+
Point location = element.getLocation();
154+
Dimension size = element.getSize();
155+
int startX = getStartX(p, location, size, offset1);
156+
int startY = getStartY(p, location, size, offset1);
157+
int endX = getEndX(p, location, size, offset2);
158+
int endY = getEndY(p, location, size, offset2);
159+
checkDirection(startX, startY, endX, endY);
160+
161+
driver.swipe(startX, startY, endX, endY, duration);
162+
}
163+
164+
static void checkYCoordinate(int y, Point location, Dimension size, int offSet)
165+
throws IllegalCoordinatesException {
166+
int bottom = location.getY() + size.getHeight();
167+
int top = location.getY();
168+
if (y > bottom)
169+
throw new IllegalCoordinatesException("The result Y " + y + " is lower than target element bottom "
170+
+ bottom);
171+
if (y < top)
172+
throw new IllegalCoordinatesException("The result Y " + y + " is higher than target element top "
173+
+ top);
174+
175+
}
176+
177+
static void checkXCoordinate(int x, Point location, Dimension size, int offSet)
178+
throws IllegalCoordinatesException {
179+
int right = location.getX() + size.getWidth();
180+
int left = location.getX();
181+
if (x > right)
182+
throw new IllegalCoordinatesException("The result X " + x + " is righter than target element right border "
183+
+ right);
184+
if (x < left)
185+
throw new IllegalCoordinatesException("The result X " + x + " is lefter than target element left border "
186+
+ left);
187+
188+
}
67189
}
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,76 @@
1-
package io.appium.java_client;
2-
3-
import org.openqa.selenium.WebElement;
4-
5-
/**
6-
* It supposed that mobile elements could be tappable, swipeable, zoomable and so on.
7-
* This interface extends {@link WebElement} and describes this behavior.
8-
*/
9-
public interface TouchableElement extends WebElement {
10-
11-
/**
12-
* Convenience method for pinching the given element.
13-
* "pinching" refers to the action of two appendages pressing the screen and sliding towards each other.
14-
* NOTE:
15-
* This convenience method places the initial touches around the element, if this would happen to place one of them
16-
* off the screen, appium with return an outOfBounds error. In this case, revert to using the MultiTouchAction api
17-
* instead of this method.
18-
*
19-
*/
20-
public void pinch();
21-
22-
/**
23-
* Convenience method for tapping the center of the given element
24-
*
25-
* @param fingers
26-
* number of fingers/appendages to tap with
27-
* @param duration
28-
* how long between pressing down, and lifting fingers/appendages
29-
*/
30-
public void tap(int fingers, int duration);
31-
32-
/**
33-
* Convenience method for "zooming in" on the given element.
34-
* "zooming in" refers to the action of two appendages pressing the screen and sliding away from each other.
35-
* NOTE:
36-
* This convenience method slides touches away from the element, if this would happen to place one of them
37-
* off the screen, appium will return an outOfBounds error. In this case, revert to using the MultiTouchAction api
38-
* instead of this method.
39-
*/
40-
public void zoom();
41-
42-
/**
43-
* Convenience method for swiping on the given element to the given direction
44-
*
45-
* @param direction UP, DOWN, LEFT, RIGHT
46-
*
47-
* @param duration amount of time in milliseconds for the entire swipe action to
48-
* take
49-
*/
50-
public void swipe(SwipeElementDirection direction, int duration);
51-
52-
}
1+
package io.appium.java_client;
2+
3+
import org.openqa.selenium.WebElement;
4+
5+
/**
6+
* It supposed that mobile elements could be tappable, swipeable, zoomable and so on.
7+
* This interface extends {@link WebElement} and describes this behavior.
8+
*/
9+
public interface TouchableElement extends WebElement {
10+
11+
/**
12+
* Convenience method for pinching the given element.
13+
* "pinching" refers to the action of two appendages pressing the screen and sliding towards each other.
14+
* NOTE:
15+
* This convenience method places the initial touches around the element, if this would happen to place one of them
16+
* off the screen, appium with return an outOfBounds error. In this case, revert to using the MultiTouchAction api
17+
* instead of this method.
18+
*
19+
*/
20+
public void pinch();
21+
22+
/**
23+
* Convenience method for tapping the center of the given element
24+
*
25+
* @param fingers
26+
* number of fingers/appendages to tap with
27+
* @param duration
28+
* how long between pressing down, and lifting fingers/appendages
29+
*/
30+
public void tap(int fingers, int duration);
31+
32+
/**
33+
* Convenience method for "zooming in" on the given element.
34+
* "zooming in" refers to the action of two appendages pressing the screen and sliding away from each other.
35+
* NOTE:
36+
* This convenience method slides touches away from the element, if this would happen to place one of them
37+
* off the screen, appium will return an outOfBounds error. In this case, revert to using the MultiTouchAction api
38+
* instead of this method.
39+
*/
40+
public void zoom();
41+
42+
/**
43+
* Convenience method for swiping on the given element to the given direction
44+
*
45+
* @param direction UP, DOWN, LEFT, RIGHT
46+
*
47+
* @param duration amount of time in milliseconds for the entire swipe action to
48+
* take
49+
*/
50+
public void swipe(SwipeElementDirection direction, int duration);
51+
52+
53+
/**
54+
* Convenience method for swiping on the given element to the given direction
55+
*
56+
* @param direction direction UP, DOWN, LEFT, RIGHT
57+
*
58+
* @param offset1 is the offset from the border of the element. If direction is UP then
59+
* this is offset from the bottom of the element. If direction is DOWN then
60+
* this is offset from the top of the element. If direction is RIGHT then
61+
* this is offset from the left border of the element. If direction is LEFT then
62+
* this is offset from the right border of the element.
63+
*
64+
* @param offset2 is the offset from the border of the element. If direction is UP then
65+
* this is offset from the top of the element. If direction is DOWN then
66+
* this is offset from the bottom of the element. If direction is RIGHT then
67+
* this is offset from the right border of the element. If direction is LEFT then
68+
* this is offset from the left border of the element.
69+
*
70+
* @param duration amount of time in milliseconds for the entire swipe action to
71+
* take
72+
* @throws IllegalCoordinatesException when resulted coordinates are out of the element borders
73+
* or disagree with the given direction
74+
*/
75+
public void swipe(SwipeElementDirection direction, int offset1, int offset2, int duration) throws IllegalCoordinatesException;
76+
}

0 commit comments

Comments
 (0)