forked from Ericwang1104/WebDriver4D
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebClasses.pas
More file actions
299 lines (231 loc) · 6.8 KB
/
WebClasses.pas
File metadata and controls
299 lines (231 loc) · 6.8 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
{ W3C Webdriver library for Delphi
Copyright (C) 2021 Giandomenico De Sanctis gidesay@yahoo.com
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
unit WebClasses;
interface
uses
Classes,
SysUtils,
Windows,
System.Generics.Collections,
JsonDataObjects, webdriverConst;
type
TBrowserAction = class(TObject)
private
FType: TBrowserActionType;
public
constructor create();
function toJson(): string; virtual;
function toJsonObject(): TJsonObject; virtual;
end;
TMouseAction = class(TBrowserAction)
private
FAction: TActionPointer;
FButton: Integer;
FX: Integer;
FY: Integer;
FDuration: Integer;
FOrigin: string;
public
constructor create(const action: TActionPointer);
function toJsonObject(): TJsonObject; override;
property x: Integer read FX write FX;
property y: Integer read FY write FY;
property button: integer read FButton write FButton;
property origin: string read FOrigin write FOrigin;
property duration: Integer read FDuration write FDuration;
end;
TKeyboardAction = class(TBrowserAction)
private
FAction: TActionKey;
FValue: Char;
public
constructor create(const action: TActionKey; const value: Char);
function toJsonObject(): TJsonObject; override;
property value: Char read FValue;
end;
TMouseActionContainer = class(TObjectList<TMouseAction>)
private
Fid: string;
FType: TBrowserActionType;
FPointerType: TActionPointerType;
public
constructor create(const id: string; const pointerType: TActionPointerType);
function toJson(): string;
property pointerType: TActionPointerType read fpointerType;
property id: string read FId ;
end;
TKeyboardActionContainer = class(TObjectList<TKeyboardAction>)
private
Fid: string;
FType: TBrowserActionType;
public
constructor create(const id: string);
function toJson(): string;
property id: string read FId ;
end;
{*************************************************************************}
implementation
constructor TBrowserAction.create();
begin
inherited create;
FType := batNone;
end;
function TBrowserAction.toJson(): string;
var
jsonOb: TJsonObject;
begin
jsonOb := toJsonObject();
result := jsonOb.ToJSON();
jsonOb.Free;
end;
function TBrowserAction.toJsonObject;
begin
result := nil;
end;
constructor TMouseAction.create(const action: TActionPointer);
begin
inherited create();
FAction := action;
FOrigin := '';
FButton := 0;
FX := 0;
FY := 0;
end;
function TMouseAction.toJsonObject(): TJsonObject;
var
pActionAr: array[0..10] of string;
jsonOb, jsonOrig: TJsonObject;
// TActionPointer = (apPause, apPointerUp, apPointerDown, apPointerMove, apPointerCancel);
begin
pActionAr[0]:= 'pause';
pActionAr[1]:= 'pointerUp';
pActionAr[2]:= 'pointerDown';
pActionAr[3]:= 'pointerMove';
pActionAr[4]:= 'pointerCancel';
JsonOb :=TJsonObject.Create;
jsonOrig:=TJsonObject.Create;
jsonOb.S['type']:= pActionAr[Ord(FAction)];
JsonOb.I['duration'] := FDuration;
if FOrigin<>'' then
begin
jsonOrig.S[WEB_ELEMENT_CONST_W3C]:=FOrigin;
jsonOb.O['origin']:= jsonOrig;
end;
jsonOb.I['x']:=FX;
jsonOb.I['y']:=FY;
jsonOb.I['button']:=FButton;
result := JsonOb;
end;
{***************************************************************************}
constructor TKeyboardAction.create(const action: TActionKey; const value: Char);
begin
inherited create();
FAction := action;
FValue := value;
end;
function TKeyboardAction.toJsonObject: TJsonObject;
var
jsonOb: TJsonObject;
pActionAr: array[0..10] of string;
// TActionKey = (akPause, akKeyUp, akKeyDown);
begin
pActionAr[0]:= 'pause';
pActionAr[1]:= 'keyUp';
pActionAr[2]:= 'keyDown';
JsonOb :=TJsonObject.Create;
jsonOb.S['type']:= pActionAr[Ord(FAction)];
jsonOb.S['value']:=FValue;
result := JsonOb;
end;
{***************************************************************************}
constructor TMouseActionContainer.create(const id: string; const pointerType: TActionPointerType);
begin
inherited create;
System.Assert(id<>'', 'TMouseActionContainer.Create: id is empty string');
OwnsObjects := True;
Fid := id;
FType := batPointer;
FPointerType := pointerType;
end;
function TMouseActionContainer.toJson(): string;
var
act: TMouseAction;
pTypeAr: array[0..10] of string;
jsonAr, jsonArActions: TJsonArray;
jsonOb, jsonOb1, jsonCont: TJsonObject;
begin
// TActionPointerType = (aptMouse, aptPen, aptTouch);
pTypeAr[0] := 'mouse';
pTypeAr[1] := 'pen';
pTypeAr[2] := 'touch';
result := '';
jsonCont := TJsonObject.Create;
jsonAr := TJsonArray.Create;
jsonOb := TJsonObject.Create;
jsonOb1 := TJsonObject.Create;
jsonArActions := TJsonArray.Create;
try
jsonOb.S['id'] :=Fid;
jsonOb.S['type'] :='pointer';
jsonOb1.S['pointerType'] := pTypeAr[Ord(FPointerType)];
jsonOb.O['parameters'] :=jsonOb1;
for act in Self do
begin
jsonArActions.Add(act.toJsonObject);
end;
jsonOb.A['actions']:= jsonArActions;
jsonAr.Add(jsonOb);
jsonCont.A['actions']:=jsonAr;
Result := jsonCont.ToJSON();
finally
jsonCont.Free;
end;
end;
{***************************************************************************}
constructor TKeyboardActionContainer.create(const id: string);
begin
inherited create;
System.Assert(id<>'', 'TMouseActionContainer.Create: id is empty string');
OwnsObjects := True;
Fid := id;
FType := batKey;
end;
function TKeyboardActionContainer.toJson(): string;
var
act: TKeyboardAction;
jsonAr, jsonArActions: TJsonArray;
jsonOb, jsonCont: TJsonObject;
begin
result := '';
jsonCont := TJsonObject.Create;
jsonAr := TJsonArray.Create;
jsonOb := TJsonObject.Create;
jsonArActions := TJsonArray.Create;
try
jsonOb.S['id'] :=Fid;
jsonOb.S['type'] :='key';
for act in Self do
begin
jsonArActions.Add(act.toJsonObject);
end;
jsonOb.A['actions']:= jsonArActions;
jsonAr.Add(jsonOb);
jsonCont.A['actions']:=jsonAr;
Result := jsonCont.ToJSON();
finally
jsonCont.Free;
end;
end;
end.