forked from appium/WebDriverAgent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFBXPath.m
More file actions
841 lines (678 loc) · 24.1 KB
/
FBXPath.m
File metadata and controls
841 lines (678 loc) · 24.1 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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "FBXPath.h"
#import "FBConfiguration.h"
#import "FBExceptions.h"
#import "FBElementUtils.h"
#import "FBLogger.h"
#import "FBMacros.h"
#import "FBXMLGenerationOptions.h"
#import "FBXCElementSnapshotWrapper+Helpers.h"
#import "NSString+FBXMLSafeString.h"
#import "XCUIElement.h"
#import "XCUIElement+FBCaching.h"
#import "XCUIElement+FBUtilities.h"
#import "XCUIElement+FBWebDriverAttributes.h"
#import "XCTestPrivateSymbols.h"
@interface FBElementAttribute : NSObject
@property (nonatomic, readonly) id<FBElement> element;
+ (nonnull NSString *)name;
+ (nullable NSString *)valueForElement:(id<FBElement>)element;
+ (int)recordWithWriter:(xmlTextWriterPtr)writer forElement:(id<FBElement>)element;
+ (NSArray<Class> *)supportedAttributes;
@end
@interface FBTypeAttribute : FBElementAttribute
@end
@interface FBValueAttribute : FBElementAttribute
@end
@interface FBNameAttribute : FBElementAttribute
@end
@interface FBLabelAttribute : FBElementAttribute
@end
@interface FBEnabledAttribute : FBElementAttribute
@end
@interface FBVisibleAttribute : FBElementAttribute
@end
@interface FBAccessibleAttribute : FBElementAttribute
@end
@interface FBDimensionAttribute : FBElementAttribute
@end
@interface FBXAttribute : FBDimensionAttribute
@end
@interface FBYAttribute : FBDimensionAttribute
@end
@interface FBWidthAttribute : FBDimensionAttribute
@end
@interface FBHeightAttribute : FBDimensionAttribute
@end
@interface FBIndexAttribute : FBElementAttribute
@end
@interface FBHittableAttribute : FBElementAttribute
@end
@interface FBInternalIndexAttribute : FBElementAttribute
@property (nonatomic, nonnull, readonly) NSString* indexValue;
+ (int)recordWithWriter:(xmlTextWriterPtr)writer forValue:(NSString *)value;
@end
@interface FBPlaceholderValueAttribute : FBElementAttribute
@end
@interface FBTraitsAttribute : FBElementAttribute
@end
#if TARGET_OS_TV
@interface FBFocusedAttribute : FBElementAttribute
@end
#endif
const static char *_UTF8Encoding = "UTF-8";
static NSString *const kXMLIndexPathKey = @"private_indexPath";
static NSString *const topNodeIndexPath = @"top";
@implementation FBXPath
+ (id)throwException:(NSString *)name forQuery:(NSString *)xpathQuery
{
NSString *reason = [NSString stringWithFormat:@"Cannot evaluate results for XPath expression \"%@\"", xpathQuery];
@throw [NSException exceptionWithName:name reason:reason userInfo:@{}];
return nil;
}
+ (nullable NSString *)xmlStringWithRootElement:(id<FBElement>)root
options:(nullable FBXMLGenerationOptions *)options
{
xmlDocPtr doc;
xmlTextWriterPtr writer = xmlNewTextWriterDoc(&doc, 0);
int rc = xmlTextWriterStartDocument(writer, NULL, _UTF8Encoding, NULL);
if (rc < 0) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterStartDocument. Error code: %d", rc];
} else {
BOOL hasScope = nil != options.scope && [options.scope length] > 0;
if (hasScope) {
rc = xmlTextWriterStartElement(writer,
(xmlChar *)[[self safeXmlStringWithString:options.scope] UTF8String]);
if (rc < 0) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterStartElement for the tag value '%@'. Error code: %d", options.scope, rc];
}
}
if (rc >= 0) {
[self waitUntilStableWithElement:root];
// If 'includeHittableInPageSource' setting is enabled, then use native snapshots
// to calculate a more accurate value for the 'hittable' attribute.
rc = [self xmlRepresentationWithRootElement:[self snapshotWithRoot:root
useNative:FBConfiguration.includeHittableInPageSource]
writer:writer
elementStore:nil
query:nil
excludingAttributes:options.excludedAttributes];
}
if (rc >= 0 && hasScope) {
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterEndElement. Error code: %d", rc];
}
}
if (rc >= 0) {
rc = xmlTextWriterEndDocument(writer);
if (rc < 0) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlXPathNewContext. Error code: %d", rc];
}
}
}
if (rc < 0) {
xmlFreeTextWriter(writer);
xmlFreeDoc(doc);
return nil;
}
int buffersize;
xmlChar *xmlbuff;
xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
xmlFreeTextWriter(writer);
xmlFreeDoc(doc);
NSString *result = [NSString stringWithCString:(const char *)xmlbuff encoding:NSUTF8StringEncoding];
xmlFree(xmlbuff);
return result;
}
+ (NSArray<id<FBXCElementSnapshot>> *)matchesWithRootElement:(id<FBElement>)root
forQuery:(NSString *)xpathQuery
{
xmlDocPtr doc;
xmlTextWriterPtr writer = xmlNewTextWriterDoc(&doc, 0);
if (NULL == writer) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlNewTextWriterDoc for XPath query \"%@\"", xpathQuery];
return [self throwException:FBXPathQueryEvaluationException forQuery:xpathQuery];
}
NSMutableDictionary *elementStore = [NSMutableDictionary dictionary];
int rc = xmlTextWriterStartDocument(writer, NULL, _UTF8Encoding, NULL);
id<FBXCElementSnapshot> lookupScopeSnapshot = nil;
id<FBXCElementSnapshot> contextRootSnapshot = nil;
BOOL useNativeSnapshot = nil == xpathQuery
? NO
: [[self.class elementAttributesWithXPathQuery:xpathQuery] containsObject:FBHittableAttribute.class];
if (rc < 0) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterStartDocument. Error code: %d", rc];
} else {
[self waitUntilStableWithElement:root];
if (FBConfiguration.limitXpathContextScope) {
lookupScopeSnapshot = [self snapshotWithRoot:root useNative:useNativeSnapshot];
} else {
if ([root isKindOfClass:XCUIElement.class]) {
lookupScopeSnapshot = [self snapshotWithRoot:[(XCUIElement *)root application]
useNative:useNativeSnapshot];
contextRootSnapshot = [root isKindOfClass:XCUIApplication.class]
? nil
: ([(XCUIElement *)root lastSnapshot] ?: [self snapshotWithRoot:(XCUIElement *)root
useNative:useNativeSnapshot]);
} else {
lookupScopeSnapshot = (id<FBXCElementSnapshot>)root;
contextRootSnapshot = nil == lookupScopeSnapshot.parent ? nil : (id<FBXCElementSnapshot>)root;
while (nil != lookupScopeSnapshot.parent) {
lookupScopeSnapshot = lookupScopeSnapshot.parent;
}
}
}
rc = [self xmlRepresentationWithRootElement:lookupScopeSnapshot
writer:writer
elementStore:elementStore
query:xpathQuery
excludingAttributes:nil];
if (rc >= 0) {
rc = xmlTextWriterEndDocument(writer);
if (rc < 0) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterEndDocument. Error code: %d", rc];
}
}
}
if (rc < 0) {
xmlFreeTextWriter(writer);
xmlFreeDoc(doc);
return [self throwException:FBXPathQueryEvaluationException forQuery:xpathQuery];
}
xmlXPathObjectPtr contextNodeQueryResult = [self matchNodeInDocument:doc
elementStore:elementStore.copy
forSnapshot:contextRootSnapshot];
xmlNodePtr contextNode = NULL;
if (NULL != contextNodeQueryResult) {
xmlNodeSetPtr nodeSet = contextNodeQueryResult->nodesetval;
if (!xmlXPathNodeSetIsEmpty(nodeSet)) {
contextNode = nodeSet->nodeTab[0];
}
}
xmlXPathObjectPtr queryResult = [self evaluate:xpathQuery
document:doc
contextNode:contextNode];
if (NULL != contextNodeQueryResult) {
xmlXPathFreeObject(contextNodeQueryResult);
}
if (NULL == queryResult) {
xmlFreeTextWriter(writer);
xmlFreeDoc(doc);
return [self throwException:FBInvalidXPathException forQuery:xpathQuery];
}
NSArray *matchingSnapshots = [self collectMatchingSnapshots:queryResult->nodesetval
elementStore:elementStore];
xmlXPathFreeObject(queryResult);
xmlFreeTextWriter(writer);
xmlFreeDoc(doc);
if (nil == matchingSnapshots) {
return [self throwException:FBXPathQueryEvaluationException forQuery:xpathQuery];
}
return matchingSnapshots;
}
+ (NSArray *)collectMatchingSnapshots:(xmlNodeSetPtr)nodeSet
elementStore:(NSMutableDictionary *)elementStore
{
if (xmlXPathNodeSetIsEmpty(nodeSet)) {
return @[];
}
NSMutableArray *matchingSnapshots = [NSMutableArray array];
const xmlChar *indexPathKeyName = (xmlChar *)[kXMLIndexPathKey UTF8String];
for (NSInteger i = 0; i < nodeSet->nodeNr; i++) {
xmlNodePtr currentNode = nodeSet->nodeTab[i];
xmlChar *attrValue = xmlGetProp(currentNode, indexPathKeyName);
if (NULL == attrValue) {
[FBLogger log:@"Failed to invoke libxml2>xmlGetProp"];
return nil;
}
id<FBXCElementSnapshot> element = [elementStore objectForKey:(id)[NSString stringWithCString:(const char *)attrValue encoding:NSUTF8StringEncoding]];
if (element) {
[matchingSnapshots addObject:element];
}
xmlFree(attrValue);
}
return matchingSnapshots.copy;
}
+ (nullable xmlXPathObjectPtr)matchNodeInDocument:(xmlDocPtr)doc
elementStore:(NSDictionary<NSString *, id<FBXCElementSnapshot>> *)elementStore
forSnapshot:(nullable id<FBXCElementSnapshot>)snapshot
{
if (nil == snapshot) {
return NULL;
}
NSString *contextRootUid = [FBElementUtils uidWithAccessibilityElement:[(id)snapshot accessibilityElement]];
if (nil == contextRootUid) {
return NULL;
}
for (NSString *key in elementStore) {
id<FBXCElementSnapshot> value = [elementStore objectForKey:key];
NSString *snapshotUid = [FBElementUtils uidWithAccessibilityElement:[value accessibilityElement]];
if (nil == snapshotUid || ![snapshotUid isEqualToString:contextRootUid]) {
continue;
}
NSString *indexQuery = [NSString stringWithFormat:@"//*[@%@=\"%@\"]", kXMLIndexPathKey, key];
xmlXPathObjectPtr queryResult = [self evaluate:indexQuery
document:doc
contextNode:NULL];
if (NULL != queryResult) {
return queryResult;
}
}
return NULL;
}
+ (NSSet<Class> *)elementAttributesWithXPathQuery:(NSString *)query
{
if ([query rangeOfString:@"[^\\w@]@\\*[^\\w]" options:NSRegularExpressionSearch].location != NSNotFound) {
// read all element attributes if 'star' attribute name pattern is used in xpath query
return [NSSet setWithArray:FBElementAttribute.supportedAttributes];
}
NSMutableSet<Class> *result = [NSMutableSet set];
for (Class attributeCls in FBElementAttribute.supportedAttributes) {
if ([query rangeOfString:[NSString stringWithFormat:@"[^\\w@]@%@[^\\w]", [attributeCls name]] options:NSRegularExpressionSearch].location != NSNotFound) {
[result addObject:attributeCls];
}
}
return result.copy;
}
+ (int)xmlRepresentationWithRootElement:(id<FBXCElementSnapshot>)root
writer:(xmlTextWriterPtr)writer
elementStore:(nullable NSMutableDictionary *)elementStore
query:(nullable NSString*)query
excludingAttributes:(nullable NSArray<NSString *> *)excludedAttributes
{
// Trying to be smart here and only including attributes, that were asked in the query, to the resulting document.
// This may speed up the lookup significantly in some cases
NSMutableSet<Class> *includedAttributes;
if (nil == query) {
includedAttributes = [NSMutableSet setWithArray:FBElementAttribute.supportedAttributes];
if (!FBConfiguration.includeHittableInPageSource) {
// The hittable attribute is expensive to calculate for each snapshot item
// thus we only include it when requested explicitly
[includedAttributes removeObject:FBHittableAttribute.class];
}
if (nil != excludedAttributes) {
for (NSString *excludedAttributeName in excludedAttributes) {
for (Class supportedAttribute in FBElementAttribute.supportedAttributes) {
if ([[supportedAttribute name] caseInsensitiveCompare:excludedAttributeName] == NSOrderedSame) {
[includedAttributes removeObject:supportedAttribute];
break;
}
}
}
}
} else {
includedAttributes = [self.class elementAttributesWithXPathQuery:query].mutableCopy;
}
[FBLogger logFmt:@"The following attributes were requested to be included into the XML: %@", includedAttributes];
int rc = [self writeXmlWithRootElement:root
indexPath:(elementStore != nil ? topNodeIndexPath : nil)
elementStore:elementStore
includedAttributes:includedAttributes.copy
writer:writer];
if (rc < 0) {
[FBLogger log:@"Failed to generate XML presentation of a screen element"];
return rc;
}
return 0;
}
+ (xmlXPathObjectPtr)evaluate:(NSString *)xpathQuery
document:(xmlDocPtr)doc
contextNode:(nullable xmlNodePtr)contextNode
{
xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
if (NULL == xpathCtx) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlXPathNewContext for XPath query \"%@\"", xpathQuery];
return NULL;
}
xpathCtx->node = NULL == contextNode ? doc->children : contextNode;
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression((const xmlChar *)[xpathQuery UTF8String], xpathCtx);
if (NULL == xpathObj) {
xmlXPathFreeContext(xpathCtx);
[FBLogger logFmt:@"Failed to invoke libxml2>xmlXPathEvalExpression for XPath query \"%@\"", xpathQuery];
return NULL;
}
xmlXPathFreeContext(xpathCtx);
return xpathObj;
}
+ (nullable NSString *)safeXmlStringWithString:(NSString *)str
{
return [str fb_xmlSafeStringWithReplacement:@""];
}
+ (int)recordElementAttributes:(xmlTextWriterPtr)writer
forElement:(id<FBXCElementSnapshot>)element
indexPath:(nullable NSString *)indexPath
includedAttributes:(nullable NSSet<Class> *)includedAttributes
{
for (Class attributeCls in FBElementAttribute.supportedAttributes) {
// include all supported attributes by default unless enumerated explicitly
if (includedAttributes && ![includedAttributes containsObject:attributeCls]) {
continue;
}
int rc = [attributeCls recordWithWriter:writer
forElement:[FBXCElementSnapshotWrapper ensureWrapped:element]];
if (rc < 0) {
return rc;
}
}
if (nil != indexPath) {
// index path is the special case
return [FBInternalIndexAttribute recordWithWriter:writer forValue:indexPath];
}
return 0;
}
+ (int)writeXmlWithRootElement:(id<FBXCElementSnapshot>)root
indexPath:(nullable NSString *)indexPath
elementStore:(nullable NSMutableDictionary *)elementStore
includedAttributes:(nullable NSSet<Class> *)includedAttributes
writer:(xmlTextWriterPtr)writer
{
NSAssert((indexPath == nil && elementStore == nil) || (indexPath != nil && elementStore != nil), @"Either both or none of indexPath and elementStore arguments should be equal to nil", nil);
NSArray<id<FBXCElementSnapshot>> *children = root.children;
if (elementStore != nil && indexPath != nil && [indexPath isEqualToString:topNodeIndexPath]) {
[elementStore setObject:root forKey:topNodeIndexPath];
}
FBXCElementSnapshotWrapper *wrappedSnapshot = [FBXCElementSnapshotWrapper ensureWrapped:root];
int rc = xmlTextWriterStartElement(writer, (xmlChar *)[wrappedSnapshot.wdType UTF8String]);
if (rc < 0) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterStartElement for the tag value '%@'. Error code: %d", wrappedSnapshot.wdType, rc];
return rc;
}
rc = [self recordElementAttributes:writer
forElement:root
indexPath:indexPath
includedAttributes:includedAttributes];
if (rc < 0) {
return rc;
}
for (NSUInteger i = 0; i < [children count]; i++) {
@autoreleasepool {
id<FBXCElementSnapshot> childSnapshot = [children objectAtIndex:i];
NSString *newIndexPath = (indexPath != nil) ? [indexPath stringByAppendingFormat:@",%lu", (unsigned long)i] : nil;
if (elementStore != nil && newIndexPath != nil) {
[elementStore setObject:childSnapshot forKey:(id)newIndexPath];
}
rc = [self writeXmlWithRootElement:[FBXCElementSnapshotWrapper ensureWrapped:childSnapshot]
indexPath:newIndexPath
elementStore:elementStore
includedAttributes:includedAttributes
writer:writer];
if (rc < 0) {
return rc;
}
}
}
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterEndElement. Error code: %d", rc];
return rc;
}
return 0;
}
+ (id<FBXCElementSnapshot>)snapshotWithRoot:(id<FBElement>)root
useNative:(BOOL)useNative
{
if (![root isKindOfClass:XCUIElement.class]) {
return (id<FBXCElementSnapshot>)root;
}
if (useNative) {
return [(XCUIElement *)root fb_nativeSnapshot];
}
return [root isKindOfClass:XCUIApplication.class]
? [(XCUIElement *)root fb_standardSnapshot]
: [(XCUIElement *)root fb_customSnapshot];
}
+ (void)waitUntilStableWithElement:(id<FBElement>)root
{
if ([root isKindOfClass:XCUIElement.class]) {
// If the app is not idle state while we retrieve the visiblity state
// then the snapshot retrieval operation might freeze and time out
[[(XCUIElement *)root application] fb_waitUntilStableWithTimeout:FBConfiguration.animationCoolOffTimeout];
}
}
@end
static NSString *const FBAbstractMethodInvocationException = @"AbstractMethodInvocationException";
@implementation FBElementAttribute
- (instancetype)initWithElement:(id<FBElement>)element
{
self = [super init];
if (self) {
_element = element;
}
return self;
}
+ (NSString *)name
{
NSString *errMsg = [NSString stringWithFormat:@"The abstract method +(NSString *)name is expected to be overriden by %@", NSStringFromClass(self.class)];
@throw [NSException exceptionWithName:FBAbstractMethodInvocationException reason:errMsg userInfo:nil];
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
NSString *errMsg = [NSString stringWithFormat:@"The abstract method -(NSString *)value is expected to be overriden by %@", NSStringFromClass(self.class)];
@throw [NSException exceptionWithName:FBAbstractMethodInvocationException reason:errMsg userInfo:nil];
}
+ (int)recordWithWriter:(xmlTextWriterPtr)writer forElement:(id<FBElement>)element
{
NSString *value = [self valueForElement:element];
if (nil == value) {
// Skip the attribute if the value equals to nil
return 0;
}
int rc = xmlTextWriterWriteAttribute(writer,
(xmlChar *)[[FBXPath safeXmlStringWithString:[self name]] UTF8String],
(xmlChar *)[[FBXPath safeXmlStringWithString:value] UTF8String]);
if (rc < 0) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterWriteAttribute(%@='%@'). Error code: %d", [self name], value, rc];
}
return rc;
}
+ (NSArray<Class> *)supportedAttributes
{
// The list of attributes to be written for each XML node
// The enumeration order does matter here
return @[FBTypeAttribute.class,
FBValueAttribute.class,
FBNameAttribute.class,
FBLabelAttribute.class,
FBEnabledAttribute.class,
FBVisibleAttribute.class,
FBAccessibleAttribute.class,
#if TARGET_OS_TV
FBFocusedAttribute.class,
#endif
FBXAttribute.class,
FBYAttribute.class,
FBWidthAttribute.class,
FBHeightAttribute.class,
FBIndexAttribute.class,
FBHittableAttribute.class,
FBPlaceholderValueAttribute.class,
FBTraitsAttribute.class,
];
}
@end
@implementation FBTypeAttribute
+ (NSString *)name
{
return @"type";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
return element.wdType;
}
@end
@implementation FBValueAttribute
+ (NSString *)name
{
return @"value";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
id idValue = element.wdValue;
if ([idValue isKindOfClass:[NSValue class]]) {
return [idValue stringValue];
} else if ([idValue isKindOfClass:[NSString class]]) {
return idValue;
}
return [idValue description];
}
@end
@implementation FBNameAttribute
+ (NSString *)name
{
return @"name";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
return element.wdName;
}
@end
@implementation FBLabelAttribute
+ (NSString *)name
{
return @"label";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
return element.wdLabel;
}
@end
@implementation FBEnabledAttribute
+ (NSString *)name
{
return @"enabled";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
return FBBoolToString(element.wdEnabled);
}
@end
@implementation FBVisibleAttribute
+ (NSString *)name
{
return @"visible";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
return FBBoolToString(element.wdVisible);
}
@end
@implementation FBAccessibleAttribute
+ (NSString *)name
{
return @"accessible";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
return FBBoolToString(element.wdAccessible);
}
@end
#if TARGET_OS_TV
@implementation FBFocusedAttribute
+ (NSString *)name
{
return @"focused";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
return FBBoolToString(element.wdFocused);
}
@end
#endif
@implementation FBDimensionAttribute
+ (NSString *)valueForElement:(id<FBElement>)element
{
return [NSString stringWithFormat:@"%@", [element.wdRect objectForKey:[self name]]];
}
@end
@implementation FBXAttribute
+ (NSString *)name
{
return @"x";
}
@end
@implementation FBYAttribute
+ (NSString *)name
{
return @"y";
}
@end
@implementation FBWidthAttribute
+ (NSString *)name
{
return @"width";
}
@end
@implementation FBHeightAttribute
+ (NSString *)name
{
return @"height";
}
@end
@implementation FBIndexAttribute
+ (NSString *)name
{
return @"index";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
return [NSString stringWithFormat:@"%lu", element.wdIndex];
}
@end
@implementation FBHittableAttribute
+ (NSString *)name
{
return @"hittable";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
return FBBoolToString(element.wdHittable);
}
@end
@implementation FBInternalIndexAttribute
+ (NSString *)name
{
return kXMLIndexPathKey;
}
+ (int)recordWithWriter:(xmlTextWriterPtr)writer forValue:(NSString *)value
{
if (nil == value) {
// Skip the attribute if the value equals to nil
return 0;
}
int rc = xmlTextWriterWriteAttribute(writer,
(xmlChar *)[[FBXPath safeXmlStringWithString:[self name]] UTF8String],
(xmlChar *)[[FBXPath safeXmlStringWithString:value] UTF8String]);
if (rc < 0) {
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterWriteAttribute(%@='%@'). Error code: %d", [self name], value, rc];
}
return rc;
}
@end
@implementation FBPlaceholderValueAttribute
+ (NSString *)name
{
return @"placeholderValue";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
return element.wdPlaceholderValue;
}
@end
@implementation FBTraitsAttribute
+ (NSString *)name
{
return @"traits";
}
+ (NSString *)valueForElement:(id<FBElement>)element
{
return element.wdTraits;
}
@end