forked from Ericwang1104/WebDriver4D
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebdriver4D.pas
More file actions
1992 lines (1694 loc) · 48.5 KB
/
Webdriver4D.pas
File metadata and controls
1992 lines (1694 loc) · 48.5 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ 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 Webdriver4D;
interface
uses
Classes, SysUtils, Windows,
Vcl.Graphics,
System.Generics.Collections,
JsonDataObjects, WebClasses, WebDriverConst, WD_http ;
type
TWebDriver = class;
TElement = class(TObject)
private
FW3C: Boolean;
FElementData: string;
FElementID: string;
FWebDriver: TWebDriver;
function GetElementID: string;
public
constructor create(elementData: string; W3C: Boolean);
property ElementID: string read FElementID;
property W3C: Boolean read FW3C write FW3C;
property elementData: string read FElementData;
function GetElementName: string;
procedure setWebDriver(wd: TWebDriver);
function executeScript(const script: string; const params: string = '[]'): string;
procedure selectByText(const text: string);
function CssValue(const cssproperty: string):string;
function Attribute(const name: string): string;
function PropertyHtml(const name: string): string;
function text(): string;
function tagname(): string;
procedure Click();
function IsSelected(): Boolean;
function IsDisplayed(): Boolean;
procedure Clear();
function TableRows(): integer;
function SelectIsMultiple():Boolean;
function SelectedOptionText(): string;
function SelectOptionsCount(): Integer;
procedure SendKey(const keys: string);
end;
TAlert = class(TObject)
private
FText: string;
FWebDriver: TWebDriver;
public
constructor create(text: string; WebDriver: TWebDriver);
procedure Accept();
procedure Dismiss();
property text: string read FText;
end;
TExecCommandEvent = procedure(response: string) of object;
TWebDriverClass = class of TWebDriver;
TWebDriver = class(TComponent)
strict private
procedure CutImage(const FileName: string; Pic: string;
X, Y, Width, Height: integer);
private
FCmd: TDriverCommand;
FOnResponse: TExecCommandEvent;
capabArray: TDictionary<string,string>;
function GetDriverIsRunning: Boolean;
function GetHasError: Boolean;
function GetHost: string;
function GetTimeout: integer;
procedure SaveScreenToFileName(const FileName, Base64File: string);
procedure SetTimeout(const Value: integer);
procedure sendActions(const data: string);
procedure normalizeFindArgs(var usingName: string; var KeyName: string);
strict protected
FErrorMessage: string;
FProcessInfo: TProcessInformation;
FStartupInfo: TStartupInfo;
FPopup_Error: Boolean;
FAddress: string;
FDriverType: TDriverType;
FBrowserExe: string;
FBrowserPath: string;
FProfile: string;
FDriverName: string;
FDriverNameAlt: string;
FDriverDir: string;
FLogFile: string;
FPath: string;
FPort: integer;
FSessionID: string;
FW3C: Boolean;
function BuildParams: string; virtual;
function ExecuteCommand(const CommandType: TCommandType;
const Command: string; const Param: string = ''): string;
function ProcResponse(const Resp: string): string;
function AddCapabilities(): string;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure StartDriver(const ExeName: string = '';
const Args: string = ''); virtual;
procedure Assign(Source: TPersistent); override;
procedure CloseWindow(ParamSessionID: string = '');
function GetAllCookie: string;
function GetAllCookieJsonArray: string;
function GetCookieByName(cookieName: string): string;
function AddCookie(cookieName, cookieValue: string): string;
procedure DeleteAllCookie;
procedure DeleteCookie(const cookieName: string);
function FindElementByID(const ID: string): TElement;
function FindElementByTag(const TagName: string): TElement;
function FindElementByClassName(const ClasseNam: string): TElement;
function FindElementByName(const Name: string): TElement;
function FindElement(usingName, KeyName: string): TElement;
function FindElementByLinkText(const LinkText: string): TElement;
function FindElementByXPath(XPath: string): TElement;
function FindElementByCSS(css: string): TElement;
function FindElementInElement(const Element: TElement; usingName, KeyName: string): TElement;
function FindElements(const usingName, KeyName: string): TObjectList<TElement>;
function FindElementsByXPath(XPath: string): TObjectList<TElement>;
function FindElementsByTag(const TagName: string): TObjectList<TElement>;
function FindElementsByLinkText(const LinkText: string): TObjectList<TElement>;
function FindElementsByCSS(const css: string): TObjectList<TElement>;
function FindElementsByClassName(const ClasName: string): TObjectList<TElement>;
function GetElementActive: TElement;
function GetCurrentWindowHandle: string;
function GetElementAttribute(const Element: TElement; const attributename: string;
const propertyFlag: Boolean = false): string;
function GetElementCssValue(const Element: TElement; const propertyname: string): string;
function GetElementText(const Element: TElement): string;
function GetElementTagname(const Element: TElement): string;
function GetElementSelected(const Element: TElement): string;
function GetElementDisplayed(const Element: TElement): string;
function GetElementEnabled(const Element: TElement): string;
procedure GetURL(const URL: string);
function GetCurUrl: string;
function NewSession: string; virtual; abstract;
procedure DeleteSession(ParamSessionID: string = '');
function GetDocument: string;
function GetAllSessions: string; virtual;
procedure Save_screenshot(const FileName: string);
procedure Set_Window_Size(const Width, Height: integer;
WindowHandle: string = 'current');
procedure WindowMaximize();
procedure ElementClick(const Element: TElement);
procedure ElementClear(const Element: TElement);
function Element_Location(const Element: TElement): string;
procedure Element_ScreenShot(const Element: TElement; FileName: string);
function Element_Size(const Element: TElement): string;
function ExecuteScript(const Script: string; const Args: string = '[]')
: string; virtual;
procedure Implicitly_Wait(const waitTime: Double);
procedure PageLoadTimeout(const Timeout: integer);
procedure Quit;
procedure Refresh(ParamSessionID: string = '');
procedure SendKey(const Element: TElement; const Key: string);
procedure SwitchToFrame(const FrameEl: TElement); virtual;
procedure SwitchToDefaultContent();
function SwitchToAlert(): TAlert;
procedure AlertAccept();
procedure AlertDismiss();
procedure TerminateWebDriver;
procedure WaitForLoaded;
procedure SetBinary(const exename: string);
procedure SetProfile(const profilename: string);
procedure SetCapability(const capname, capvalue: string);
procedure Wait(const ms: Integer);
procedure ProcessActions(actList: TKeyboardActionContainer); overload;
procedure ProcessActions(actList: TMouseActionContainer); overload;
property driverType: TDriverType read FDriverType;
property ErrorMessage: string read FErrorMessage;
property HasError: Boolean read GetHasError;
property LogFile: string read FLogFile write FLogFile;
property Host: string read GetHost;
property Address: string read FAddress write FAddress;
property Cmd: TDriverCommand read FCmd write FCmd;
property DriverIsRunning: Boolean read GetDriverIsRunning;
property DriverName: string read FDriverName;
property DriverDir: string read FDriverDir write FDriverDir;
property Port: integer read FPort write FPort;
property Path: string read FPath write FPath;
property Popup_Error: Boolean read FPopup_Error write FPopup_Error;
property SessionID: string read FSessionID write FSessionID;
property W3C: Boolean read FW3C;
property browserexe: string read FBrowserExe;
published
procedure Clear;
property Timeout: integer read GetTimeout write SetTimeout;
property OnResponse: TExecCommandEvent read FOnResponse write FOnResponse;
end;
{**********************************************}
implementation
uses
//Delphi versione > XE2
{$IF CompilerVersion > 23.0}
System.NetEncoding,
{$else}
Soap.EncdDecd,
{$ifend}
Vcl.Imaging.pngimage;
{***********************************************************************}
constructor TWebDriver.Create(AOwner: TComponent);
begin
inherited;
FillChar(FProcessInfo, SizeOf(FProcessInfo), 0);
FAddress := '127.0.0.1';
FLogFile := '';
FPath := '';
FErrorMessage := '';
FW3C := False;
FPopup_Error := True;
FBrowserExe := '';
FBrowserPath := '';
FProfile := '';
capabArray := TDictionary<string,string>.Create;
FDriverType := btUnknown;
FDriverDir := '.\';
FDriverName:='';
FDriverNameAlt:='';
end;
destructor TWebDriver.Destroy;
begin
if FProcessInfo.hProcess <> 0 then
begin
Clear;
TerminateWebDriver;
end;
capabArray.Free;
inherited;
end;
procedure TWebDriver.Assign(Source: TPersistent);
var
WD: TWebDriver;
begin
inherited;
if Source is TWebDriver then
begin
WD := Source as TWebDriver;
self.Address := WD.Address;
self.Port := WD.Port;
self.Path := WD.Path;
self.Timeout := WD.Timeout;
end;
end;
function TWebDriver.BuildParams: string;
begin
end;
procedure TWebDriver.CloseWindow(ParamSessionID: string = '');
var
Command: string;
begin
if ParamSessionID <> '' then
begin
Command := Host + '/session/' + ParamSessionID + '/window';
end
else
Command := Host + '/session/' + FSessionID + '/window';
ExecuteCommand(cDelete, Command);
end;
procedure TWebDriver.CutImage(const FileName: string; Pic: string;
X, Y, Width, Height: integer);
var
png: TPngImage;
// Delphi version > XE2
{$IF CompilerVersion > 23.0}
Encd: TBase64Encoding;
{$ifend}
Stream: TMemoryStream;
Byts: TBytes;
bmp: TBitmap;
REctS, REctD: TRect;
begin
{$IF CompilerVersion > 23.0}
Encd := TBase64Encoding.Create;
{$ifend}
Stream := TMemoryStream.Create;
bmp := TBitmap.Create;
png := TPngImage.Create;
try
{$IF CompilerVersion > 23.0}
Byts := Encd.DecodeStringToBytes(Pic);
{$ELSE}
Byts := DecodeBase64(pic); //xe2
{$ifend}
Stream.Write(Byts[0], Length(Byts));
Stream.Position := 0;
png.LoadFromStream(Stream);
REctS.Left := X;
REctS.Top := Y;
REctS.Width := Width;
REctS.Height := Height;
REctD.Left := 0;
REctD.Top := 0;
bmp.Width := Width;
bmp.Height := Height;
REctD.Width := Width;
REctD.Height := Height;
bmp.Canvas.CopyRect(REctD, png.Canvas, REctS);
png.Assign(bmp);
png.SaveToFile(FileName);
finally
FreeAndNil(png);
FreeAndNil(bmp);
FreeAndNil(Stream);
{$IF CompilerVersion > 23.0}
FreeAndNil(Encd);
{$ifend}
end;
end;
procedure TWebDriver.DeleteSession(ParamSessionID: string = '');
var
Command: string;
begin
if ParamSessionID <> '' then
begin
Command := Host + '/session/' + ParamSessionID;
end
else
begin
Command := Host + '/session/' + FSessionID;
FSessionID := '';
end;
ExecuteCommand(cDelete, Command);
end;
procedure TWebDriver.ElementClick(const Element: TElement);
var
Command: string;
Data: string;
Resp: string;
Json:TJsonObject;
begin
System.Assert(Element <> nil, 'ElementClick: element not assigned');
Json :=TJsonObject.Create;
try
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + '/click';
Json.S['id'] := Element.ElementID;
Data := Json.ToJSON(False);
Resp := ExecuteCommand(cPost, Command, Data);
ProcResponse(Resp);
finally
FreeAndNil(Json);
end;
end;
procedure TWebDriver.ElementClear(const Element: TElement);
var
Command: string;
Data: string;
Resp: string;
Json:TJsonObject;
begin
System.Assert(Element <> nil, 'ElementClear: element not assigned');
Json :=TJsonObject.Create;
try
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + '/clear';
Json.S['id'] := Element.ElementID;
Data := Json.ToJSON(False);
Resp := ExecuteCommand(cPost, Command, Data);
ProcResponse(Resp);
finally
FreeAndNil(Json);
end;
end;
function TWebDriver.Element_Location(const Element: TElement): string;
var
Command: string;
Resp: string;
begin
if FW3C then
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + '/rect'
else
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID +
'/location';
Resp := ExecuteCommand(cGet, Command);
result := ProcResponse(Resp);
end;
function TWebDriver.Element_Size(const Element: TElement): string;
var
Command: string;
Resp: string;
begin
if FW3C then
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + '/rect'
else
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + '/size';
Resp := ExecuteCommand(cGet, Command);
result := ProcResponse(Resp);
end;
function TWebDriver.ExecuteScript(const Script: string;
const Args: string = '[]'): string;
var
Command: string;
Data: string;
Resp: string;
Json:TJsonObject;
begin
Json :=TJsonObject.Create;
try
Command := Host + '/session/' + FSessionID + '/execute/sync';
Json.S['script'] :=Script;
Json.A['args'].FromJSON(args);
Data :=Json.ToJSON();
Resp := ExecuteCommand(cPost, Command, Data);
result := ProcResponse(Resp);
Finally
FreeAndNil(Json);
end;
end;
function TWebDriver.FindElementByID(const ID: string): TElement;
begin
result := FindElement('id', ID);
end;
function TWebDriver.FindElementByTag(const TagName: string): TElement;
begin
result := FindElement('tag name', TagName);
end;
function TWebDriver.FindElementByClassName(const ClasseNam: string): TElement;
begin
result := FindElement('class name', ClasseNam);
end;
procedure TWebDriver.normalizeFindArgs(var usingName: string; var KeyName: string);
begin
if FW3C then
begin
if SameText(usingName, 'id') then
begin
usingName := 'css selector';
KeyName := format('[id="%s" ]', [KeyName]);
end
else if SameText(usingName, 'tag name') then
begin
usingName := 'css selector';
end
else if SameText(usingName, 'class name') then
begin
usingName := 'css selector';
KeyName := format('.%s', [KeyName]);
end
else if SameText(usingName, 'name') then
begin
usingName := 'css selector';
KeyName := format('[name="%s"]', [KeyName]);
end;
end;
end;
function TWebDriver.FindElement(usingName, KeyName: string): TElement;
var
Command: string;
Data: string;
Resp: string;
JsonData:string;
Json:TJsonObject;
ele: TElement;
begin
Command := Host + '/session/' + FSessionID + '/element';
normalizeFindArgs(usingName, KeyName);
Json :=TJsonObject.Create;
try
Json.S['using'] :=usingName;
Json.S['value'] :=KeyName;
Data := Json.ToJSON();
Resp := ExecuteCommand(cPost, Command, Data);
JsonData :=ProcResponse(Resp);
if not HasError then
begin
ele:=TElement.Create(JsonData, FW3C);
ele.setWebDriver(self);
result := ele;
end else
begin
Result :=nil;
end;
finally
FreeAndNil(Json);
end;
end;
function TWebDriver.FindElementInElement(const Element: TElement;
usingName: string; KeyName: string): TElement;
var
Command: string;
Data: string;
Resp: string;
JsonData:string;
Json:TJsonObject;
ele: TElement;
begin
System.Assert(Element<>nil, 'FindElementInElement: element null' );
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID
+ '/element';
normalizeFindArgs(usingName, KeyName);
Json :=TJsonObject.Create;
try
Json.S['using'] :=usingName;
Json.S['value'] :=KeyName;
Data := Json.ToJSON();
Resp := ExecuteCommand(cPost, Command, Data);
JsonData :=ProcResponse(Resp);
if not HasError then
begin
ele:=TElement.Create(JsonData, FW3C);
ele.setWebDriver(self);
result := ele;
end else
begin
Result :=nil;
end;
finally
FreeAndNil(Json);
end;
end;
function TWebDriver.FindElementByLinkText(const LinkText: string): TElement;
begin
result := FindElement('link text', LinkText);
end;
function TWebDriver.FindElementByXPath(XPath: string): TElement;
begin
result := FindElement('xpath', XPath);
end;
function TWebDriver.FindElementByCSS(css: string): TElement;
begin
result := FindElement('css selector', css);
end;
function TWebDriver.GetElementActive: TElement;
var
Command: string;
Data: string;
Resp: string;
JsonData:string;
Json:TJsonObject;
ele: TElement;
begin
Command := Host + '/session/' + FSessionID + '/element/active';
// Json :=TJsonObject.Create;
try
// Data := Json.ToJSON();
Resp := ExecuteCommand(cGet, Command);
JsonData :=ProcResponse(Resp);
if not HasError then
begin
ele:=TElement.Create(JsonData, FW3C);
ele.setWebDriver(self);
result := ele;
end else
begin
Result :=nil;
end;
finally
// FreeAndNil(Json);
end;
end;
function TWebDriver.GetAllSessions: string;
var
Command: string;
Resp: string;
begin
// command /sessions not existing in W3C protocol,
// that is in all drivers (as geckodriver)
try
Command := Host + '/sessions';
Resp := ExecuteCommand(cGet, Command);
if Resp <> '' then
begin
result := ProcResponse(Resp);
end
else
begin
result := '';
end;
except
on E: exception do
raise Exception.Create('Command /sessions error: ' + E.Message + ' - response <|' + Resp + '|>');
end;
end;
function TWebDriver.GetCurrentWindowHandle: string;
var
Command: string;
Resp: string;
begin
if FW3C then
Command := Host + '/session/' + FSessionID + '/window'
else
Command := Host + '/session/' + FSessionID + '/window_handle';
Resp := ExecuteCommand(cGet, Command);
result := ProcResponse(Resp);
end;
function TWebDriver.GetElementAttribute(const Element: TElement; const attributename
: string; const propertyFlag: Boolean = false): string;
var
Command: string;
Resp: string;
attribProp: string;
begin
System.Assert(Element<>nil, 'GetElementAttribute: element null' );
// if FW3C then
// begin
// js :=Format('return (%s).apply(null, arguments);',[ATTRIBUTE_JS]);
// args:='[{"ELEMENT":'+'"'+Element.ElementID+'",'+'"'+Element.GetElementName+'"'+
// ':'+'"'+Element.ElementID+'"}'+','+'"'+attributename+'"]';
// result :=Self.ExecuteScript(js,args);
// end else
begin
if propertyFlag then attribProp := '/property/' else attribProp := '/attribute/';
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + attribProp
+ attributename;
Resp := ExecuteCommand(cGet, Command);
result := ProcResponse(Resp);
end;
end;
function TWebDriver.GetElementCssValue(const Element: TElement; const propertyname: string): string;
var
Command: string;
Resp: string;
begin
System.Assert(Element<>nil, 'GetElementCssValue: element null' );
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + '/css/'
+ propertyname;
Resp := ExecuteCommand(cGet, Command);
result := ProcResponse(Resp);
end;
function TWebDriver.GetElementSelected(const Element: TElement): string;
var
Command: string;
Resp: string;
begin
System.Assert(Element<>nil, 'GetElementSelected: element null' );
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + '/selected';
Resp := ExecuteCommand(cGet, Command);
result := ProcResponse(Resp);
end;
function TWebDriver.GetElementDisplayed(const Element: TElement): string;
var
Command: string;
Resp: string;
begin
System.Assert(Element<>nil, 'GetElementDisplayed: element null' );
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + '/displayed';
Resp := ExecuteCommand(cGet, Command);
result := ProcResponse(Resp);
end;
function TWebDriver.GetElementEnabled(const Element: TElement): string;
var
Command: string;
Resp: string;
begin
System.Assert(Element<>nil, 'GetElementEnabled: element null' );
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + '/enabled';
Resp := ExecuteCommand(cGet, Command);
result := ProcResponse(Resp);
end;
function TWebDriver.GetElementTagname(const Element: TElement): string;
var
Command: string;
Resp: string;
begin
System.Assert(Element<>nil, 'GetElementTagname: element null' );
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + '/name';
Resp := ExecuteCommand(cGet, Command);
result := ProcResponse(Resp);
end;
function TWebDriver.GetElementText(const Element: TElement): string;
var
Command: string;
Resp: string;
oldTimeout: Integer;
begin
System.Assert(Element<>nil, 'GetElementText: element null' );
Command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID + '/text';
oldTimeout := Timeout;
Timeout := 60000;
Resp := ExecuteCommand(cGet, Command);
Timeout := oldTimeout;
result := ProcResponse(Resp);
end;
function TWebDriver.GetHost: string;
begin
result := format('http://%s:%d%s', [FAddress, FPort, FPath]);
end;
procedure TWebDriver.GetURL(const URL: string);
var
Command: string;
Data: string;
Resp: string;
Json:TJsonObject;
begin
Command := Host + '/session/' + FSessionID + '/url';
Json :=TJsonObject.Create;
try
Json.S['url'] := URL;
Json.S['sessionid'] := FSessionID;
Data := Json.ToJSON();
Resp := ExecuteCommand(cPost, Command, Data);
ProcResponse(Resp);
if not HasError then
WaitForLoaded;
finally
FreeAndNil(Json);
end;
end;
procedure TWebDriver.Quit;
begin
if FSessionID <> '' then
begin
DeleteSession(FSessionID);
FSessionID := '';
end;
end;
procedure TWebDriver.Refresh(ParamSessionID: string = '');
var
Command: string;
Data: string;
Resp: string;
Json:TJsonObject;
begin
Json :=TJsonObject.Create;
try
if ParamSessionID <> '' then
Command := Host + '/session/' + ParamSessionID + '/refresh'
else
Command := Host + '/session/' + FSessionID + '/refresh';
Json.S['sessionid'] := FSessionID;
Data := Json.ToJSON();
Resp := ExecuteCommand(cPost, Command, Data);
ProcResponse(Resp);
finally
FreeAndNil(Json);
end;
end;
procedure TWebDriver.SaveScreenToFileName(const FileName, Base64File: string);
var
{$IF CompilerVersion > 23.0}
Encode: TBase64Encoding;
{$ifend}
bytes: TBytes;
Fs: TFileStream;
begin
{$IF CompilerVersion > 23.0}
Encode := TBase64Encoding.Create;
{$ifend}
try
{$IF CompilerVersion > 23.0}
bytes := Encode.DecodeStringToBytes(Base64File);
{$ELSE}
bytes := DecodeBase64(Base64File); //xe2
{$ifend}
Fs := TFileStream.Create(FileName, fmCreate);
try
Fs.WriteBuffer(bytes[0], Length(bytes));
finally
FreeAndNil(Fs);
end;
finally
{$IF CompilerVersion > 23.0}
FreeAndNil(Encode);
{$ifend}
end;
end;
procedure TWebDriver.StartDriver(const ExeName: string = '';
const Args: string = '');
var
Command: string;
drivName: string;
begin
if (ExeName <>'') then
begin
if not FileExists(FDriverDir+ExeName) then
raise Exception.Create('D1-Driver file not exists. ' + ExeName);
FDriverName :=ExeName;
end;
if FProcessInfo.hProcess <> 0 then
Exit;
FillChar(FStartupInfo, SizeOf(FStartupInfo), 0);
FillChar(FProcessInfo, SizeOf(FProcessInfo), 0);
FStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
FStartupInfo.wShowWindow := SW_HIDE;
if Args = '' then
Command := self.BuildParams
else
Command := Args;
windows.OutputDebugString(PWideChar('>>>Driver>>> '+FDriverDir+FDriverName + ' ' + Command));
if FileExists(FDriverDir+FDriverName) then
drivName:=FDriverName
else
if (FDriverNameAlt<>'') and (FileExists(FDriverDir+FDriverNameAlt)) then
drivName:=FDriverNameAlt
else
raise Exception.Create('D2-Driver file not exists. ' + FDriverName + ' or ' + FDriverNameAlt);
if CreateProcess(nil, Pchar(FDriverDir+drivName + ' ' + Command), nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, nil, FStartupInfo, FProcessInfo) then
begin
// start ok
end else
begin
raise Exception.Create('Driver not started. Last OS error: ' + IntToStr(windows.GetLastError));
end;
end;
procedure TWebDriver.Save_screenshot(const FileName: string);
var
Command: string;
Resp: string;
Pic: string;
begin
Command := Host + '/session/' + FSessionID + '/screenshot';
Resp := ExecuteCommand(cGet, Command);
Pic := ProcResponse(Resp);
if not HasError then
begin
SaveScreenToFileName(FileName, Pic);
end;
end;
procedure TWebDriver.Element_ScreenShot(const Element: TElement; FileName: string);
var
Command: string;
Resp: string;
Pic: string;
Size: string;
Loc: string;
X, Y, Width, Height: integer;
Json :TJsonObject;
begin
System.Assert(Element <> nil, 'Element_Screenshot: element not assigned');
Json :=TJsonObject.Create;
if FW3C then
begin
command := Host + '/session/' + FSessionID + '/element/' + Element.ElementID +
'/screenshot';
end else
begin
Command := Host + '/session/' + FSessionID + '/screenshot';
end;
try
Resp := ExecuteCommand(cGet, Command);
Json.FromJSON(Resp);
if not HasError then
begin
Pic := Json.S['value'];
Loc := Element_Location(Element);
Json.FromJSON(Loc);
X := Json.I['x'];
Y := Json.I['y'];
Size := Element_Size(Element);
Json.FromJSON(Size);
Width := Json.I['width'];
Height := Json.I['height'];
CutImage(FileName, Pic, X, Y, Width, Height);
end;
finally
FreeAndNil(Json);
end;
end;
function TWebDriver.FindElementsByXPath(XPath: string): TObjectList<TElement>;
begin
result := FindElements('xpath', XPath);
end;