Skip to content

Commit

Permalink
fix: Support non-strict XPath queries (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Oct 28, 2024
1 parent e120420 commit f596a61
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion WebDriverAgentMac/IntegrationTests/AMFindElementTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ - (void)testMultipleDescendantsWithPredicate

- (void)testSingleDescendantWithXPath
{
NSString *query = @"*//XCUIElementTypeButton[starts-with(@identifier, \"_XCUI:\")]";
NSString *query = @"//XCUIElementTypeButton[starts-with(@identifier, \"_XCUI:\")]";
NSArray<XCUIElement *> *matches = [self.testedApplication fb_descendantsMatchingXPathQuery:query
shouldReturnAfterFirstMatch:YES];
XCTAssertEqual(matches.count, 1);
Expand Down
28 changes: 27 additions & 1 deletion WebDriverAgentMac/WebDriverAgentLib/Utilities/FBXPath.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ + (void)recordWithNode:(NSXMLElement *)node forValue:(NSString *)value;

@end

@interface NSString (FBXPathFixes)

/**
https://discuss.appium.io/t/cannot-find-element-with-mac2/44959
*/
- (NSString *)fb_toFixedXPathQuery;

@end

@implementation NSString (FBXPathFixes)

- (NSString *)fb_toFixedXPathQuery
{
NSString *replacePattern = @"^([(]*)(/)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:replacePattern
options:NSRegularExpressionCaseInsensitive
error:nil];
return [regex stringByReplacingMatchesInString:self
options:0
range:NSMakeRange(0, [self length])
withTemplate:@"$1.$2"];
}

@end

static NSString *const kXMLIndexPathKey = @"private_indexPath";


Expand Down Expand Up @@ -132,7 +157,8 @@ + (nullable NSString *)xmlStringWithRootElement:(XCUIElement *)root

NSXMLElement *rootElement = [self makeXmlWithRootSnapshot:snapshot
indexPath:[AMSnapshotUtils hashWithSnapshot:snapshot]];
NSArray<__kindof NSXMLNode *> *matches = [rootElement nodesForXPath:xpathQuery error:&error];
NSArray<__kindof NSXMLNode *> *matches = [rootElement nodesForXPath:[xpathQuery fb_toFixedXPathQuery]
error:&error];
if (nil == matches) {
@throw [NSException exceptionWithName:FBInvalidXPathException
reason:error.description
Expand Down

0 comments on commit f596a61

Please sign in to comment.