-
Notifications
You must be signed in to change notification settings - Fork 37
/
gwda.go
1168 lines (999 loc) · 37.8 KB
/
gwda.go
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
package gwda
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
// HTTPClient The default client to use to communicate with the WebDriver server.
var HTTPClient = http.DefaultClient
var (
DefaultWaitTimeout = 60 * time.Second
DefaultWaitInterval = 400 * time.Millisecond
DefaultKeepAliveInterval = 30 * time.Second
)
func newRequest(method string, url string, rawBody []byte) (request *http.Request, err error) {
var header = map[string]string{
"Content-Type": "application/json;charset=UTF-8",
"Accept": "application/json",
}
if request, err = http.NewRequest(method, url, bytes.NewBuffer(rawBody)); err != nil {
return nil, err
}
for k, v := range header {
request.Header.Set(k, v)
}
return
}
func executeHTTP(method string, rawURL string, rawBody []byte, httpCli *http.Client) (rawResp rawResponse, err error) {
debugLog(fmt.Sprintf("--> %s %s\n%s", method, rawURL, rawBody))
var req *http.Request
if req, err = newRequest(method, rawURL, rawBody); err != nil {
return
}
tmpCli := HTTPClient
if httpCli != nil {
tmpCli = httpCli
}
tmpCli.Timeout = 0
start := time.Now()
var resp *http.Response
if resp, err = tmpCli.Do(req); err != nil {
return nil, err
}
defer func() {
// https://github.com/etcd-io/etcd/blob/v3.3.25/pkg/httputil/httputil.go#L16-L22
_, _ = io.Copy(ioutil.Discard, resp.Body)
_ = resp.Body.Close()
}()
rawResp, err = ioutil.ReadAll(resp.Body)
debugLog(fmt.Sprintf("<-- %s %s %d %s\n%s\n", method, rawURL, resp.StatusCode, time.Since(start), rawResp))
if err != nil {
return nil, err
}
if err = rawResp.checkErr(); err != nil {
if resp.StatusCode == http.StatusOK {
return rawResp, nil
}
return nil, err
}
return
}
var debugFlag = false
// SetDebug sets debug mode
func SetDebug(debug bool) {
debugFlag = debug
}
func debugLog(msg string) {
if !debugFlag {
return
}
log.Println("[GWDA-DEBUG] " + msg)
}
func convertToHTTPClient(_conn net.Conn) *http.Client {
return &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return _conn, nil
},
},
Timeout: 0,
}
}
type rawResponse []byte
func (r rawResponse) checkErr() (err error) {
var reply = new(struct {
Value struct {
Err string `json:"error"`
Message string `json:"message"`
Traceback string `json:"traceback"`
}
})
if err = json.Unmarshal(r, reply); err != nil {
return err
}
if reply.Value.Err != "" {
errText := reply.Value.Message
re := regexp.MustCompile(`{.+?=(.+?)}`)
if re.MatchString(reply.Value.Message) {
subMatch := re.FindStringSubmatch(reply.Value.Message)
errText = subMatch[len(subMatch)-1]
}
return fmt.Errorf("%s: %s", reply.Value.Err, errText)
}
return
}
func (r rawResponse) valueConvertToString() (s string, err error) {
var reply = new(struct{ Value string })
if err = json.Unmarshal(r, reply); err != nil {
return "", err
}
s = reply.Value
return
}
func (r rawResponse) valueConvertToBool() (b bool, err error) {
var reply = new(struct{ Value bool })
if err = json.Unmarshal(r, reply); err != nil {
return false, err
}
b = reply.Value
return
}
func (r rawResponse) valueConvertToSessionInfo() (sessionInfo SessionInfo, err error) {
var reply = new(struct{ Value struct{ SessionInfo } })
if err = json.Unmarshal(r, reply); err != nil {
return SessionInfo{}, err
}
sessionInfo = reply.Value.SessionInfo
return
}
func (r rawResponse) valueConvertToJsonRawMessage() (raw json.RawMessage, err error) {
var reply = new(struct{ Value json.RawMessage })
if err = json.Unmarshal(r, reply); err != nil {
return nil, err
}
raw = reply.Value
return
}
func (r rawResponse) valueDecodeAsBase64() (raw *bytes.Buffer, err error) {
var str string
if str, err = r.valueConvertToString(); err != nil {
return nil, err
}
var decodeString []byte
if decodeString, err = base64.StdEncoding.DecodeString(str); err != nil {
return nil, err
}
raw = bytes.NewBuffer(decodeString)
return
}
var errNoSuchElement = errors.New("no such element")
func (r rawResponse) valueConvertToElementID() (id string, err error) {
var reply = new(struct{ Value map[string]string })
if err = json.Unmarshal(r, reply); err != nil {
return "", err
}
if len(reply.Value) == 0 {
return "", errNoSuchElement
}
if id = elementIDFromValue(reply.Value); id == "" {
return "", fmt.Errorf("invalid element returned: %+v", reply)
}
return
}
func (r rawResponse) valueConvertToElementIDs() (IDs []string, err error) {
var reply = new(struct{ Value []map[string]string })
if err = json.Unmarshal(r, reply); err != nil {
return nil, err
}
if len(reply.Value) == 0 {
return nil, errNoSuchElement
}
IDs = make([]string, len(reply.Value))
for i, elem := range reply.Value {
var id string
if id = elementIDFromValue(elem); id == "" {
return nil, fmt.Errorf("invalid element returned: %+v", reply)
}
IDs[i] = id
}
return
}
type AlertAction string
const (
AlertActionAccept AlertAction = "accept"
AlertActionDismiss AlertAction = "dismiss"
)
type Capabilities map[string]interface{}
func NewCapabilities() Capabilities {
return make(Capabilities)
}
func (caps Capabilities) WithAppLaunchOption(launchOpt AppLaunchOption) Capabilities {
for k, v := range launchOpt {
caps[k] = v
}
return caps
}
// WithDefaultAlertAction
func (caps Capabilities) WithDefaultAlertAction(alertAction AlertAction) Capabilities {
caps["defaultAlertAction"] = alertAction
return caps
}
// WithMaxTypingFrequency
// Defaults to `60`.
func (caps Capabilities) WithMaxTypingFrequency(n int) Capabilities {
if n <= 0 {
n = 60
}
caps["maxTypingFrequency"] = n
return caps
}
// WithWaitForIdleTimeout
// Defaults to `10`
func (caps Capabilities) WithWaitForIdleTimeout(second float64) Capabilities {
caps["waitForIdleTimeout"] = second
return caps
}
// WithShouldUseTestManagerForVisibilityDetection If set to YES will ask TestManagerDaemon for element visibility
// Defaults to `false`
func (caps Capabilities) WithShouldUseTestManagerForVisibilityDetection(b bool) Capabilities {
caps["shouldUseTestManagerForVisibilityDetection"] = b
return caps
}
// WithShouldUseCompactResponses If set to YES will use compact (standards-compliant) & faster responses
// Defaults to `true`
func (caps Capabilities) WithShouldUseCompactResponses(b bool) Capabilities {
caps["shouldUseCompactResponses"] = b
return caps
}
// WithElementResponseAttributes If shouldUseCompactResponses == NO,
// is the comma-separated list of fields to return with each element.
// Defaults to `type,label`.
func (caps Capabilities) WithElementResponseAttributes(s string) Capabilities {
caps["elementResponseAttributes"] = s
return caps
}
// WithShouldUseSingletonTestManager
// Defaults to `true`
func (caps Capabilities) WithShouldUseSingletonTestManager(b bool) Capabilities {
caps["shouldUseSingletonTestManager"] = b
return caps
}
// WithDisableAutomaticScreenshots
// Defaults to `true`
func (caps Capabilities) WithDisableAutomaticScreenshots(b bool) Capabilities {
caps["disableAutomaticScreenshots"] = b
return caps
}
// WithShouldTerminateApp
// Defaults to `true`
func (caps Capabilities) WithShouldTerminateApp(b bool) Capabilities {
caps["shouldTerminateApp"] = b
return caps
}
// WithEventloopIdleDelaySec
// Delays the invocation of '-[XCUIApplicationProcess setEventLoopHasIdled:]' by the timer interval passed.
// which is skipped on setting it to zero.
func (caps Capabilities) WithEventloopIdleDelaySec(second float64) Capabilities {
caps["eventloopIdleDelaySec"] = second
return caps
}
type SessionInfo struct {
SessionId string `json:"sessionId"`
Capabilities struct {
Device string `json:"device"`
BrowserName string `json:"browserName"`
SdkVersion string `json:"sdkVersion"`
CFBundleIdentifier string `json:"CFBundleIdentifier"`
} `json:"capabilities"`
}
type DeviceStatus struct {
Message string `json:"message"`
State string `json:"state"`
OS struct {
TestmanagerdVersion int `json:"testmanagerdVersion"`
Name string `json:"name"`
SdkVersion string `json:"sdkVersion"`
Version string `json:"version"`
} `json:"os"`
IOS struct {
IP string `json:"ip"`
SimulatorVersion string `json:"simulatorVersion"`
} `json:"ios"`
Ready bool `json:"ready"`
Build struct {
Time string `json:"time"`
ProductBundleIdentifier string `json:"productBundleIdentifier"`
} `json:"build"`
}
type DeviceInfo struct {
TimeZone string `json:"timeZone"`
CurrentLocale string `json:"currentLocale"`
Model string `json:"model"`
UUID string `json:"uuid"`
UserInterfaceIdiom int `json:"userInterfaceIdiom"`
UserInterfaceStyle string `json:"userInterfaceStyle"`
Name string `json:"name"`
IsSimulator bool `json:"isSimulator"`
ThermalState int `json:"thermalState"`
}
type Location struct {
AuthorizationStatus int `json:"authorizationStatus"`
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
Altitude float64 `json:"altitude"`
}
type BatteryInfo struct {
// Battery level in range [0.0, 1.0], where 1.0 means 100% charge.
Level float64 `json:"level"`
// Battery state ( 1: on battery, discharging; 2: plugged in, less than 100%, 3: plugged in, at 100% )
State BatteryState `json:"state"`
}
type BatteryState int
const (
_ = iota
BatteryStateUnplugged BatteryState = iota // on battery, discharging
BatteryStateCharging // plugged in, less than 100%
BatteryStateFull // plugged in, at 100%
)
func (v BatteryState) String() string {
switch v {
case BatteryStateUnplugged:
return "On battery, discharging"
case BatteryStateCharging:
return "Plugged in, less than 100%"
case BatteryStateFull:
return "Plugged in, at 100%"
default:
return "UNKNOWN"
}
}
type Size struct {
Width int `json:"width"`
Height int `json:"height"`
}
type Screen struct {
StatusBarSize Size `json:"statusBarSize"`
Scale float64 `json:"scale"`
}
type AppInfo struct {
ProcessArguments struct {
Env interface{} `json:"env"`
Args []interface{} `json:"args"`
} `json:"processArguments"`
Name string `json:"name"`
AppBaseInfo
}
type AppBaseInfo struct {
Pid int `json:"pid"`
BundleId string `json:"bundleId"`
}
type AppState int
const (
AppStateNotRunning AppState = 1 << iota
AppStateRunningBack
AppStateRunningFront
)
func (v AppState) String() string {
switch v {
case AppStateNotRunning:
return "Not Running"
case AppStateRunningBack:
return "Running (Back)"
case AppStateRunningFront:
return "Running (Front)"
default:
return "UNKNOWN"
}
}
// AppLaunchOption Configure app launch parameters
type AppLaunchOption map[string]interface{}
func NewAppLaunchOption() AppLaunchOption {
return make(AppLaunchOption)
}
func (opt AppLaunchOption) WithBundleId(bundleId string) AppLaunchOption {
opt["bundleId"] = bundleId
return opt
}
// WithShouldWaitForQuiescence whether to wait for quiescence on application startup
// Defaults to `true`
func (opt AppLaunchOption) WithShouldWaitForQuiescence(b bool) AppLaunchOption {
opt["shouldWaitForQuiescence"] = b
return opt
}
// WithArguments The optional array of application command line arguments.
// The arguments are going to be applied if the application was not running before.
func (opt AppLaunchOption) WithArguments(args []string) AppLaunchOption {
opt["arguments"] = args
return opt
}
// WithEnvironment The optional dictionary of environment variables for the application, which is going to be executed.
// The environment variables are going to be applied if the application was not running before.
func (opt AppLaunchOption) WithEnvironment(env map[string]string) AppLaunchOption {
opt["environment"] = env
return opt
}
// PasteboardType The type of the item on the pasteboard.
type PasteboardType string
const (
PasteboardTypePlaintext PasteboardType = "plaintext"
PasteboardTypeImage PasteboardType = "image"
PasteboardTypeUrl PasteboardType = "url"
)
const (
TextBackspace string = "\u0008"
TextDelete string = "\u007F"
)
// type KeyboardKeyLabel string
//
// const (
// KeyboardKeyReturn = "return"
// )
// DeviceButton A physical button on an iOS device.
type DeviceButton string
const (
DeviceButtonHome DeviceButton = "home"
DeviceButtonVolumeUp DeviceButton = "volumeUp"
DeviceButtonVolumeDown DeviceButton = "volumeDown"
)
type NotificationType string
const (
NotificationTypePlain NotificationType = "plain"
NotificationTypeDarwin NotificationType = "darwin"
)
// EventPageID The event page identifier
type EventPageID int
const EventPageIDConsumer EventPageID = 0x0C
// EventUsageID The event usage identifier (usages are defined per-page)
type EventUsageID int
const (
EventUsageIDCsmrVolumeUp EventUsageID = 0xE9
EventUsageIDCsmrVolumeDown EventUsageID = 0xEA
EventUsageIDCsmrHome EventUsageID = 0x40
EventUsageIDCsmrPower EventUsageID = 0x30
EventUsageIDCsmrSnapshot EventUsageID = 0x65 // Power + Home
)
type Orientation string
const (
// OrientationPortrait Device oriented vertically, home button on the bottom
OrientationPortrait Orientation = "PORTRAIT"
// OrientationPortraitUpsideDown Device oriented vertically, home button on the top
OrientationPortraitUpsideDown Orientation = "UIA_DEVICE_ORIENTATION_PORTRAIT_UPSIDEDOWN"
// OrientationLandscapeLeft Device oriented horizontally, home button on the right
OrientationLandscapeLeft Orientation = "LANDSCAPE"
// OrientationLandscapeRight Device oriented horizontally, home button on the left
OrientationLandscapeRight Orientation = "UIA_DEVICE_ORIENTATION_LANDSCAPERIGHT"
)
type Rotation struct {
X int `json:"x"`
Y int `json:"y"`
Z int `json:"z"`
}
// SourceOption Configure the format or attribute of the Source
type SourceOption map[string]interface{}
func NewSourceOption() SourceOption {
return make(SourceOption)
}
// WithFormatAsJson Application elements tree in form of json string
func (opt SourceOption) WithFormatAsJson() SourceOption {
opt["format"] = "json"
return opt
}
// WithFormatAsXml Application elements tree in form of xml string
func (opt SourceOption) WithFormatAsXml() SourceOption {
opt["format"] = "xml"
return opt
}
// WithFormatAsDescription Application elements tree in form of internal XCTest debugDescription string
func (opt SourceOption) WithFormatAsDescription() SourceOption {
opt["format"] = "description"
return opt
}
// WithScope Allows to provide XML scope.
// only `xml` is supported.
func (opt SourceOption) WithScope(scope string) SourceOption {
if vFormat, ok := opt["format"]; ok && vFormat != "xml" {
return opt
}
opt["scope"] = scope
return opt
}
// WithExcludedAttributes Excludes the given attribute names.
// only `xml` is supported.
func (opt SourceOption) WithExcludedAttributes(attributes []string) SourceOption {
if vFormat, ok := opt["format"]; ok && vFormat != "xml" {
return opt
}
opt["excluded_attributes"] = strings.Join(attributes, ",")
return opt
}
const (
// legacyWebElementIdentifier is the string constant used in the old
// WebDriver JSON protocol that is the key for the map that contains an
// unique element identifier.
legacyWebElementIdentifier = "ELEMENT"
// webElementIdentifier is the string constant defined by the W3C
// specification that is the key for the map that contains a unique element identifier.
webElementIdentifier = "element-6066-11e4-a52e-4f735466cecf"
)
func elementIDFromValue(val map[string]string) string {
for _, key := range []string{webElementIdentifier, legacyWebElementIdentifier} {
if v, ok := val[key]; ok && v != "" {
return v
}
}
return ""
}
type BySelector struct {
ClassName ElementType `json:"class name"`
// isSearchByIdentifier
Name string `json:"name"`
Id string `json:"id"`
AccessibilityId string `json:"accessibility id"`
// isSearchByIdentifier
// partialSearch
LinkText ElementAttribute `json:"link text"`
PartialLinkText ElementAttribute `json:"partial link text"`
// partialSearch
Predicate string `json:"predicate string"`
ClassChain string `json:"class chain"`
XPath string `json:"xpath"`
}
func (wl BySelector) getUsingAndValue() (using, value string) {
vBy := reflect.ValueOf(wl)
tBy := reflect.TypeOf(wl)
for i := 0; i < vBy.NumField(); i++ {
vi := vBy.Field(i).Interface()
switch vi := vi.(type) {
case ElementType:
value = vi.String()
case string:
value = vi
case ElementAttribute:
value = vi.String()
}
if value != "" && value != "UNKNOWN" {
using = tBy.Field(i).Tag.Get("json")
return
}
}
return
}
type ElementAttribute map[string]interface{}
func (ea ElementAttribute) String() string {
for k, v := range ea {
switch v := v.(type) {
case bool:
return k + "=" + strconv.FormatBool(v)
case string:
return k + "=" + v
default:
return k + "=" + fmt.Sprintf("%v", v)
}
}
return "UNKNOWN"
}
func (ea ElementAttribute) getAttributeName() string {
for k := range ea {
return k
}
return "UNKNOWN"
}
func NewElementAttribute() ElementAttribute {
return make(ElementAttribute)
}
// WithUID Element's unique identifier
func (ea ElementAttribute) WithUID(uid string) ElementAttribute {
ea["UID"] = uid
return ea
}
// WithAccessibilityContainer Whether element is an accessibility container
// (contains children of any depth that are accessible)
func (ea ElementAttribute) WithAccessibilityContainer(b bool) ElementAttribute {
ea["accessibilityContainer"] = b
return ea
}
// WithAccessible Whether element is accessible
func (ea ElementAttribute) WithAccessible(b bool) ElementAttribute {
ea["accessible"] = b
return ea
}
// WithEnabled Whether element is enabled
func (ea ElementAttribute) WithEnabled(b bool) ElementAttribute {
ea["enabled"] = b
return ea
}
// WithLabel Element's label
func (ea ElementAttribute) WithLabel(s string) ElementAttribute {
ea["label"] = s
return ea
}
// WithName Element's name
func (ea ElementAttribute) WithName(s string) ElementAttribute {
ea["name"] = s
return ea
}
// WithSelected Element's selected state
func (ea ElementAttribute) WithSelected(b bool) ElementAttribute {
ea["selected"] = b
return ea
}
// WithType Element's type
func (ea ElementAttribute) WithType(elemType ElementType) ElementAttribute {
ea["type"] = elemType
return ea
}
// WithValue Element's value
func (ea ElementAttribute) WithValue(s string) ElementAttribute {
ea["value"] = s
return ea
}
// WithVisible
//
// Whether element is visible
func (ea ElementAttribute) WithVisible(b bool) ElementAttribute {
ea["visible"] = b
return ea
}
func (et ElementType) String() string {
vBy := reflect.ValueOf(et)
tBy := reflect.TypeOf(et)
for i := 0; i < vBy.NumField(); i++ {
if vBy.Field(i).Bool() {
return tBy.Field(i).Tag.Get("json")
}
}
return "UNKNOWN"
}
// ElementType
// !!! This mapping should be updated if there are changes after each new XCTest release"`
type ElementType struct {
Any bool `json:"XCUIElementTypeAny"`
Other bool `json:"XCUIElementTypeOther"`
Application bool `json:"XCUIElementTypeApplication"`
Group bool `json:"XCUIElementTypeGroup"`
Window bool `json:"XCUIElementTypeWindow"`
Sheet bool `json:"XCUIElementTypeSheet"`
Drawer bool `json:"XCUIElementTypeDrawer"`
Alert bool `json:"XCUIElementTypeAlert"`
Dialog bool `json:"XCUIElementTypeDialog"`
Button bool `json:"XCUIElementTypeButton"`
RadioButton bool `json:"XCUIElementTypeRadioButton"`
RadioGroup bool `json:"XCUIElementTypeRadioGroup"`
CheckBox bool `json:"XCUIElementTypeCheckBox"`
DisclosureTriangle bool `json:"XCUIElementTypeDisclosureTriangle"`
PopUpButton bool `json:"XCUIElementTypePopUpButton"`
ComboBox bool `json:"XCUIElementTypeComboBox"`
MenuButton bool `json:"XCUIElementTypeMenuButton"`
ToolbarButton bool `json:"XCUIElementTypeToolbarButton"`
Popover bool `json:"XCUIElementTypePopover"`
Keyboard bool `json:"XCUIElementTypeKeyboard"`
Key bool `json:"XCUIElementTypeKey"`
NavigationBar bool `json:"XCUIElementTypeNavigationBar"`
TabBar bool `json:"XCUIElementTypeTabBar"`
TabGroup bool `json:"XCUIElementTypeTabGroup"`
Toolbar bool `json:"XCUIElementTypeToolbar"`
StatusBar bool `json:"XCUIElementTypeStatusBar"`
Table bool `json:"XCUIElementTypeTable"`
TableRow bool `json:"XCUIElementTypeTableRow"`
TableColumn bool `json:"XCUIElementTypeTableColumn"`
Outline bool `json:"XCUIElementTypeOutline"`
OutlineRow bool `json:"XCUIElementTypeOutlineRow"`
Browser bool `json:"XCUIElementTypeBrowser"`
CollectionView bool `json:"XCUIElementTypeCollectionView"`
Slider bool `json:"XCUIElementTypeSlider"`
PageIndicator bool `json:"XCUIElementTypePageIndicator"`
ProgressIndicator bool `json:"XCUIElementTypeProgressIndicator"`
ActivityIndicator bool `json:"XCUIElementTypeActivityIndicator"`
SegmentedControl bool `json:"XCUIElementTypeSegmentedControl"`
Picker bool `json:"XCUIElementTypePicker"`
PickerWheel bool `json:"XCUIElementTypePickerWheel"`
Switch bool `json:"XCUIElementTypeSwitch"`
Toggle bool `json:"XCUIElementTypeToggle"`
Link bool `json:"XCUIElementTypeLink"`
Image bool `json:"XCUIElementTypeImage"`
Icon bool `json:"XCUIElementTypeIcon"`
SearchField bool `json:"XCUIElementTypeSearchField"`
ScrollView bool `json:"XCUIElementTypeScrollView"`
ScrollBar bool `json:"XCUIElementTypeScrollBar"`
StaticText bool `json:"XCUIElementTypeStaticText"`
TextField bool `json:"XCUIElementTypeTextField"`
SecureTextField bool `json:"XCUIElementTypeSecureTextField"`
DatePicker bool `json:"XCUIElementTypeDatePicker"`
TextView bool `json:"XCUIElementTypeTextView"`
Menu bool `json:"XCUIElementTypeMenu"`
MenuItem bool `json:"XCUIElementTypeMenuItem"`
MenuBar bool `json:"XCUIElementTypeMenuBar"`
MenuBarItem bool `json:"XCUIElementTypeMenuBarItem"`
Map bool `json:"XCUIElementTypeMap"`
WebView bool `json:"XCUIElementTypeWebView"`
IncrementArrow bool `json:"XCUIElementTypeIncrementArrow"`
DecrementArrow bool `json:"XCUIElementTypeDecrementArrow"`
Timeline bool `json:"XCUIElementTypeTimeline"`
RatingIndicator bool `json:"XCUIElementTypeRatingIndicator"`
ValueIndicator bool `json:"XCUIElementTypeValueIndicator"`
SplitGroup bool `json:"XCUIElementTypeSplitGroup"`
Splitter bool `json:"XCUIElementTypeSplitter"`
RelevanceIndicator bool `json:"XCUIElementTypeRelevanceIndicator"`
ColorWell bool `json:"XCUIElementTypeColorWell"`
HelpTag bool `json:"XCUIElementTypeHelpTag"`
Matte bool `json:"XCUIElementTypeMatte"`
DockItem bool `json:"XCUIElementTypeDockItem"`
Ruler bool `json:"XCUIElementTypeRuler"`
RulerMarker bool `json:"XCUIElementTypeRulerMarker"`
Grid bool `json:"XCUIElementTypeGrid"`
LevelIndicator bool `json:"XCUIElementTypeLevelIndicator"`
Cell bool `json:"XCUIElementTypeCell"`
LayoutArea bool `json:"XCUIElementTypeLayoutArea"`
LayoutItem bool `json:"XCUIElementTypeLayoutItem"`
Handle bool `json:"XCUIElementTypeHandle"`
Stepper bool `json:"XCUIElementTypeStepper"`
Tab bool `json:"XCUIElementTypeTab"`
TouchBar bool `json:"XCUIElementTypeTouchBar"`
StatusItem bool `json:"XCUIElementTypeStatusItem"`
}
// ProtectedResource A system resource that requires user authorization to access.
type ProtectedResource int
// https://developer.apple.com/documentation/xctest/xcuiprotectedresource?language=objc
const (
ProtectedResourceContacts ProtectedResource = 1
ProtectedResourceCalendar ProtectedResource = 2
ProtectedResourceReminders ProtectedResource = 3
ProtectedResourcePhotos ProtectedResource = 4
ProtectedResourceMicrophone ProtectedResource = 5
ProtectedResourceCamera ProtectedResource = 6
ProtectedResourceMediaLibrary ProtectedResource = 7
ProtectedResourceHomeKit ProtectedResource = 8
ProtectedResourceSystemRootDirectory ProtectedResource = 0x40000000
ProtectedResourceUserDesktopDirectory ProtectedResource = 0x40000001
ProtectedResourceUserDownloadsDirectory ProtectedResource = 0x40000002
ProtectedResourceUserDocumentsDirectory ProtectedResource = 0x40000003
ProtectedResourceBluetooth ProtectedResource = -0x40000000
ProtectedResourceKeyboardNetwork ProtectedResource = -0x40000001
ProtectedResourceLocation ProtectedResource = -0x40000002
ProtectedResourceHealth ProtectedResource = -0x40000003
)
type Condition func(wd WebDriver) (bool, error)
type Direction string
const (
DirectionUp Direction = "up"
DirectionDown Direction = "down"
DirectionLeft Direction = "left"
DirectionRight Direction = "right"
)
type PickerWheelOrder string
const (
PickerWheelOrderNext PickerWheelOrder = "next"
PickerWheelOrderPrevious PickerWheelOrder = "previous"
)
type Point struct {
X int `json:"x"`
Y int `json:"y"`
}
type Rect struct {
Point
Size
}
// WebDriver defines methods supported by WebDriver drivers.
type WebDriver interface {
// NewSession starts a new session and returns the SessionInfo.
NewSession(capabilities Capabilities) (SessionInfo, error)
ActiveSession() (SessionInfo, error)
// DeleteSession Kills application associated with that session and removes session
// 1) alertsMonitor disable
// 2) testedApplicationBundleId terminate
DeleteSession() error
Status() (DeviceStatus, error)
DeviceInfo() (DeviceInfo, error)
// Location Returns device location data.
//
// It requires to configure location access permission by manual.
// The response of 'latitude', 'longitude' and 'altitude' are always zero (0) without authorization.
// 'authorizationStatus' indicates current authorization status. '3' is 'Always'.
// https://developer.apple.com/documentation/corelocation/clauthorizationstatus
//
// Settings -> Privacy -> Location Service -> WebDriverAgent-Runner -> Always
//
// The return value could be zero even if the permission is set to 'Always'
// since the location service needs some time to update the location data.
Location() (Location, error)
BatteryInfo() (BatteryInfo, error)
WindowSize() (Size, error)
Screen() (Screen, error)
Scale() (float64, error)
ActiveAppInfo() (AppInfo, error)
// ActiveAppsList Retrieves the information about the currently active apps
ActiveAppsList() ([]AppBaseInfo, error)
// AppState Get the state of the particular application in scope of the current session.
// !This method is only returning reliable results since Xcode9 SDK
AppState(bundleId string) (AppState, error)
// IsLocked Checks if the screen is locked or not.
IsLocked() (bool, error)
// Unlock Forces the device under test to unlock.
// An immediate return will happen if the device is already unlocked
// and an error is going to be thrown if the screen has not been unlocked after the timeout.
Unlock() error
// Lock Forces the device under test to switch to the lock screen.
// An immediate return will happen if the device is already locked
// and an error is going to be thrown if the screen has not been locked after the timeout.
Lock() error
// Homescreen Forces the device under test to switch to the home screen
Homescreen() error
// AlertText Returns alert's title and description separated by new lines
AlertText() (string, error)
// AlertButtons Gets the labels of the buttons visible in the alert
AlertButtons() ([]string, error)
// AlertAccept Accepts alert, if present
AlertAccept(label ...string) error
// AlertDismiss Dismisses alert, if present
AlertDismiss(label ...string) error
// AlertSendKeys Types a text into an input inside the alert container, if it is present
AlertSendKeys(text string) error
// AppLaunch Launch an application with given bundle identifier in scope of current session.
// !This method is only available since Xcode9 SDK
AppLaunch(bundleId string, launchOpt ...AppLaunchOption) error
// AppLaunchUnattached Launch the app with the specified bundle ID.
AppLaunchUnattached(bundleId string) error
// AppTerminate Terminate an application with the given bundle id.
// Either `true` if the app has been successfully terminated or `false` if it was not running
AppTerminate(bundleId string) (bool, error)
// AppActivate Activate an application with given bundle identifier in scope of current session.
// !This method is only available since Xcode9 SDK
AppActivate(bundleId string) error
// AppDeactivate Deactivates application for given time and then activate it again
// The minimum application switch wait is 3 seconds
AppDeactivate(second float64) error
// AppAuthReset Resets the authorization status for a protected resource. Available since Xcode 11.4
AppAuthReset(ProtectedResource) error
// Tap Sends a tap event at the coordinate.
Tap(x, y int) error
TapFloat(x, y float64) error
// DoubleTap Sends a double tap event at the coordinate.
DoubleTap(x, y int) error
DoubleTapFloat(x, y float64) error
// TouchAndHold Initiates a long-press gesture at the coordinate, holding for the specified duration.
// second: The default value is 1
TouchAndHold(x, y int, second ...float64) error
TouchAndHoldFloat(x, y float64, second ...float64) error
// Drag Initiates a press-and-hold gesture at the coordinate, then drags to another coordinate.
// pressForDuration: The default value is 1 second.
Drag(fromX, fromY, toX, toY int, pressForDuration ...float64) error
DragFloat(fromX, fromY, toX, toY float64, pressForDuration ...float64) error
// Swipe works like Drag, but `pressForDuration` value is 0
Swipe(fromX, fromY, toX, toY int) error
SwipeFloat(fromX, fromY, toX, toY float64) error
ForceTouch(x, y int, pressure float64, second ...float64) error
ForceTouchFloat(x, y, pressure float64, second ...float64) error
// PerformW3CActions Perform complex touch action in scope of the current application.
PerformW3CActions(actions *W3CActions) error
PerformAppiumTouchActions(touchActs *TouchActions) error