@@ -9,59 +9,181 @@ public enum SwipeElementDirection {
9
9
*/
10
10
UP {
11
11
@ 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" );
19
40
}
20
41
},
21
42
/**
22
43
* Down from the center of the upper
23
44
*/
24
45
DOWN {
46
+
25
47
@ 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" );
33
72
}
34
73
},
35
74
/**
36
75
* To the left from the center of the rightmost
37
76
*/
38
77
LEFT {
78
+
39
79
@ 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 ;
47
84
}
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
+ }
48
110
},
49
111
/**
50
112
* To the right from the center of the leftmost
51
113
*/
52
114
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
+
53
131
@ 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" );
61
141
}
62
142
};
63
143
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
+ }
67
189
}
0 commit comments