-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
AppiumScrollActions.cs
215 lines (170 loc) · 6.34 KB
/
AppiumScrollActions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.MultiTouch;
using UITest.Core;
namespace UITest.Appium
{
public enum ScrollStrategy
{
Auto,
Gesture,
Programmatically
}
public class AppiumScrollActions : ICommandExecutionGroup
{
const string ScrollLeftCommand = "scrollLeft";
const string ScrollDownCommand = "scrollDown";
const string ScrollRightCommand = "scrollRight";
const string ScrollUpCommand = "scrollUp";
readonly AppiumApp _appiumApp;
readonly List<string> _commands = new()
{
ScrollLeftCommand,
ScrollDownCommand,
ScrollRightCommand,
ScrollUpCommand,
};
public AppiumScrollActions(AppiumApp appiumApp)
{
_appiumApp = appiumApp;
}
public bool IsCommandSupported(string commandName)
{
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
}
public CommandResponse Execute(string commandName, IDictionary<string, object> parameters)
{
return commandName switch
{
ScrollLeftCommand => ScrollLeft(parameters),
ScrollDownCommand => ScrollDown(parameters),
ScrollRightCommand => ScrollRight(parameters),
ScrollUpCommand => ScrollUp(parameters),
_ => CommandResponse.FailedEmptyResponse,
};
}
CommandResponse ScrollLeft(IDictionary<string, object> parameters)
{
parameters.TryGetValue("element", out var value);
var element = GetAppiumElement(value);
if (element is null)
return CommandResponse.FailedEmptyResponse;
ScrollStrategy strategy = (ScrollStrategy)parameters["strategy"];
double swipePercentage = (double)parameters["swipePercentage"];
int swipeSpeed = (int)parameters["swipeSpeed"];
bool withInertia = (bool)parameters["withInertia"];
ScrollToLeft(_appiumApp.Driver, element, strategy, swipePercentage, swipeSpeed, withInertia);
return CommandResponse.SuccessEmptyResponse;
}
CommandResponse ScrollDown(IDictionary<string, object> parameters)
{
parameters.TryGetValue("element", out var value);
var element = GetAppiumElement(value);
if (element is null)
return CommandResponse.FailedEmptyResponse;
ScrollStrategy strategy = (ScrollStrategy)parameters["strategy"];
double swipePercentage = (double)parameters["swipePercentage"];
int swipeSpeed = (int)parameters["swipeSpeed"];
bool withInertia = (bool)parameters["withInertia"];
ScrollToDown(_appiumApp.Driver, element, strategy, swipePercentage, swipeSpeed, withInertia);
return CommandResponse.SuccessEmptyResponse;
}
CommandResponse ScrollRight(IDictionary<string, object> parameters)
{
parameters.TryGetValue("element", out var value);
var element = GetAppiumElement(value);
if (element is null)
return CommandResponse.FailedEmptyResponse;
ScrollStrategy strategy = (ScrollStrategy)parameters["strategy"];
double swipePercentage = (double)parameters["swipePercentage"];
int swipeSpeed = (int)parameters["swipeSpeed"];
bool withInertia = (bool)parameters["withInertia"];
ScrollToRight(_appiumApp.Driver, element, strategy, swipePercentage, swipeSpeed, withInertia);
return CommandResponse.SuccessEmptyResponse;
}
CommandResponse ScrollUp(IDictionary<string, object> parameters)
{
parameters.TryGetValue("element", out var value);
var element = GetAppiumElement(value);
if (element is null)
return CommandResponse.FailedEmptyResponse;
ScrollStrategy strategy = (ScrollStrategy)parameters["strategy"];
double swipePercentage = (double)parameters["swipePercentage"];
int swipeSpeed = (int)parameters["swipeSpeed"];
bool withInertia = (bool)parameters["withInertia"];
ScrollToUp(_appiumApp.Driver, element, strategy, swipePercentage, swipeSpeed, withInertia);
return CommandResponse.SuccessEmptyResponse;
}
static AppiumElement? GetAppiumElement(object? element)
{
if (element is AppiumElement appiumElement)
{
return appiumElement;
}
else if (element is AppiumDriverElement driverElement)
{
return driverElement.AppiumElement;
}
return null;
}
static void ScrollToLeft(AppiumDriver driver, AppiumElement element, ScrollStrategy strategy, double swipePercentage, int swipeSpeed, bool withInertia = true)
{
var position = element.Location;
var size = element.Size;
int startX = (int)(position.X + (size.Width * 0.05));
int startY = position.Y + size.Height / 2;
int endX = (int)(position.X + (size.Width * swipePercentage));
int endY = startY;
new TouchAction(driver)
.Press(startX, startY)
.Wait(strategy != ScrollStrategy.Programmatically ? swipeSpeed : 0)
.MoveTo(endX, endY)
.Release()
.Perform();
}
static void ScrollToDown(AppiumDriver driver, AppiumElement element, ScrollStrategy strategy, double swipePercentage, int swipeSpeed, bool withInertia = true)
{
var position = element.Location;
var size = element.Size;
int startX = position.X + size.Width / 2;
int startY = (int)(position.Y + (size.Height * swipePercentage));
int endX = startX;
int endY = (int)(position.Y + (size.Height * 0.05));
new TouchAction(driver)
.Press(startX, startY)
.Wait(strategy != ScrollStrategy.Programmatically ? swipeSpeed : 0)
.MoveTo(endX, endY)
.Release()
.Perform();
}
static void ScrollToRight(AppiumDriver driver, AppiumElement element, ScrollStrategy strategy, double swipePercentage, int swipeSpeed, bool withInertia = true)
{
var position = element.Location;
var size = element.Size;
int startX = (int)(position.X + (size.Width * swipePercentage));
int startY = position.Y + size.Height / 2;
int endX = (int)(position.X + (size.Width * 0.05));
int endY = startY;
new TouchAction(driver)
.Press(startX, startY)
.Wait(strategy != ScrollStrategy.Programmatically ? swipeSpeed : 0)
.MoveTo(endX, endY)
.Release()
.Perform();
}
static void ScrollToUp(AppiumDriver driver, AppiumElement element, ScrollStrategy strategy, double swipePercentage, int swipeSpeed, bool withInertia = true)
{
var position = element.Location;
var size = element.Size;
int startX = position.X + size.Width / 2;
int startY = (int)(position.Y + (size.Height * 0.05));
int endX = startX;
int endY = (int)(position.Y + (size.Height * swipePercentage));
new TouchAction(driver)
.Press(startX, startY)
.Wait(strategy != ScrollStrategy.Programmatically ? swipeSpeed : 0)
.MoveTo(endX, endY)
.Release()
.Perform();
}
}
}