Skip to content

Commit

Permalink
feat: Increase Xpath Lookup Performance (#666)
Browse files Browse the repository at this point in the history
* Increase Xpath lookup performance

* moved import

* addressing review comments
  • Loading branch information
Dan-Maor authored Feb 23, 2023
1 parent 2b6d6dd commit 1696f4b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 1 addition & 3 deletions WebDriverAgentLib/Categories/XCUIElement+FBResolve.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ - (XCUIElement *)fb_stableInstance
if (nil == uid) {
return self;
}
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id<FBXCElementSnapshot> snapshot, NSDictionary *bindings) {
return [[FBXCElementSnapshotWrapper wdUIDWithSnapshot:snapshot] isEqualToString:uid];
}];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@",FBStringify(FBXCElementSnapshotWrapper, fb_uid), uid];
return [query matchingPredicate:predicate].allElementsBoundByIndex.firstObject ?: self;
}

Expand Down
18 changes: 17 additions & 1 deletion WebDriverAgentLib/Categories/XCUIElement+FBUID.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

#import <objc/runtime.h>

#import "XCUIElement+FBUID.h"

#import "FBElementUtils.h"
Expand All @@ -33,6 +35,17 @@ - (NSString *)fb_uid

@implementation FBXCElementSnapshotWrapper (FBUID)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-load-method"
+ (void)load
{
Class XCElementSnapshotCls = objc_lookUpClass("XCElementSnapshot");
NSAssert(XCElementSnapshotCls != nil, @"Could not locate XCElementSnapshot class");
Method uidMethod = class_getInstanceMethod(self.class, @selector(fb_uid));
class_addMethod(XCElementSnapshotCls, @selector(fb_uid), method_getImplementation(uidMethod), method_getTypeEncoding(uidMethod));
}
#pragma diagnostic pop

- (unsigned long long)fb_accessibiltyId
{
return [FBElementUtils idWithAccessibilityElement:self.accessibilityElement];
Expand All @@ -45,7 +58,10 @@ + (nullable NSString *)wdUIDWithSnapshot:(id<FBXCElementSnapshot>)snapshot

- (NSString *)fb_uid
{
return [self.class wdUIDWithSnapshot:self.snapshot];
if ([self isKindOfClass:FBXCElementSnapshotWrapper.class]) {
return [self.class wdUIDWithSnapshot:self.snapshot];
}
return [FBElementUtils uidWithAccessibilityElement:[self accessibilityElement]];
}

@end
6 changes: 3 additions & 3 deletions WebDriverAgentLib/Categories/XCUIElement+FBUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ @implementation XCUIElement (FBUtilities)
XCUIElementQuery *query = onlyChildren
? [self.fb_query childrenMatchingType:type]
: [self.fb_query descendantsMatchingType:type];
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id<FBXCElementSnapshot> snapshot, NSDictionary *bindings) {
return [matchedIds containsObject:[FBXCElementSnapshotWrapper wdUIDWithSnapshot:snapshot] ?: @""];
}];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@",FBStringify(FBXCElementSnapshotWrapper, fb_uid), matchedIds];
[matchedElements addObjectsFromArray:[query matchingPredicate:predicate].allElementsBoundByIndex];

for (XCUIElement *el in matchedElements) {
el.fb_isResolvedNatively = @NO;
}
Expand Down

0 comments on commit 1696f4b

Please sign in to comment.