Skip to content

Commit 088cff2

Browse files
feat: Add process and bundle identifiers to the application node in the XML source (#1055)
1 parent 8cef7bc commit 088cff2

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

WebDriverAgentLib/Utilities/FBXPath.m

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
#import "FBXMLGenerationOptions.h"
1818
#import "FBXCElementSnapshotWrapper+Helpers.h"
1919
#import "NSString+FBXMLSafeString.h"
20+
#import "XCUIApplication.h"
2021
#import "XCUIElement.h"
2122
#import "XCUIElement+FBCaching.h"
2223
#import "XCUIElement+FBUtilities.h"
2324
#import "XCUIElement+FBWebDriverAttributes.h"
2425
#import "XCTestPrivateSymbols.h"
2526
#import "FBElementHelpers.h"
27+
#import "FBXCAXClientProxy.h"
28+
#import "FBXCAccessibilityElement.h"
2629

2730

2831
@interface FBElementAttribute : NSObject
@@ -33,6 +36,7 @@ + (nonnull NSString *)name;
3336
+ (nullable NSString *)valueForElement:(id<FBElement>)element;
3437

3538
+ (int)recordWithWriter:(xmlTextWriterPtr)writer forElement:(id<FBElement>)element;
39+
+ (int)recordWithWriter:(xmlTextWriterPtr)writer forValue:(nullable NSString *)value;
3640

3741
+ (NSArray<Class> *)supportedAttributes;
3842

@@ -98,7 +102,13 @@ @interface FBInternalIndexAttribute : FBElementAttribute
98102

99103
@property (nonatomic, nonnull, readonly) NSString* indexValue;
100104

101-
+ (int)recordWithWriter:(xmlTextWriterPtr)writer forValue:(NSString *)value;
105+
@end
106+
107+
@interface FBApplicationBundleIdAttribute : FBElementAttribute
108+
109+
@end
110+
111+
@interface FBApplicationPidAttribute : FBElementAttribute
102112

103113
@end
104114

@@ -472,6 +482,27 @@ + (int)recordElementAttributes:(xmlTextWriterPtr)writer
472482
// index path is the special case
473483
return [FBInternalIndexAttribute recordWithWriter:writer forValue:indexPath];
474484
}
485+
if (element.elementType == XCUIElementTypeApplication) {
486+
// only record process identifier and bundle identifier for the application element
487+
int pid = [element.accessibilityElement processIdentifier];
488+
if (pid > 0) {
489+
int rc = [FBApplicationPidAttribute recordWithWriter:writer
490+
forValue:[NSString stringWithFormat:@"%d", pid]];
491+
if (rc < 0) {
492+
return rc;
493+
}
494+
XCUIApplication *app = [[FBXCAXClientProxy sharedClient]
495+
monitoredApplicationWithProcessIdentifier:pid];
496+
NSString *bundleID = [app bundleID];
497+
if (nil != bundleID) {
498+
rc = [FBApplicationBundleIdAttribute recordWithWriter:writer
499+
forValue:bundleID];
500+
if (rc < 0) {
501+
return rc;
502+
}
503+
}
504+
}
505+
}
475506
return 0;
476507
}
477508

@@ -585,6 +616,11 @@ + (NSString *)valueForElement:(id<FBElement>)element
585616
+ (int)recordWithWriter:(xmlTextWriterPtr)writer forElement:(id<FBElement>)element
586617
{
587618
NSString *value = [self valueForElement:element];
619+
return [self recordWithWriter:writer forValue:value];
620+
}
621+
622+
+ (int)recordWithWriter:(xmlTextWriterPtr)writer forValue:(nullable NSString *)value
623+
{
588624
if (nil == value) {
589625
// Skip the attribute if the value equals to nil
590626
return 0;
@@ -830,22 +866,25 @@ + (NSString *)name
830866
return kXMLIndexPathKey;
831867
}
832868

833-
+ (int)recordWithWriter:(xmlTextWriterPtr)writer forValue:(NSString *)value
869+
@end
870+
871+
@implementation FBApplicationBundleIdAttribute : FBElementAttribute
872+
873+
+ (NSString *)name
834874
{
835-
if (nil == value) {
836-
// Skip the attribute if the value equals to nil
837-
return 0;
838-
}
839-
int rc = xmlTextWriterWriteAttribute(writer,
840-
(xmlChar *)[[FBXPath safeXmlStringWithString:[self name]] UTF8String],
841-
(xmlChar *)[[FBXPath safeXmlStringWithString:value] UTF8String]);
842-
if (rc < 0) {
843-
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterWriteAttribute(%@='%@'). Error code: %d", [self name], value, rc];
844-
}
845-
return rc;
875+
return @"bundleId";
846876
}
877+
847878
@end
848879

880+
@implementation FBApplicationPidAttribute : FBElementAttribute
881+
882+
+ (NSString *)name
883+
{
884+
return @"processId";
885+
}
886+
887+
@end
849888

850889
@implementation FBPlaceholderValueAttribute
851890

WebDriverAgentTests/IntegrationTests/FBXPathIntegrationTests.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
#import "FBMacros.h"
1414
#import "FBTestMacros.h"
1515
#import "FBXPath.h"
16+
#import "FBXCAccessibilityElement.h"
1617
#import "FBXCodeCompatibility.h"
1718
#import "FBXCElementSnapshotWrapper+Helpers.h"
1819
#import "FBXMLGenerationOptions.h"
20+
#import "XCUIApplication.h"
1921
#import "XCUIElement.h"
2022
#import "XCUIElement+FBFind.h"
2123
#import "XCUIElement+FBUtilities.h"
@@ -50,6 +52,19 @@ - (void)setUp
5052
return snapshot;
5153
}
5254

55+
- (void)testApplicationNodeXMLRepresentation
56+
{
57+
id<FBXCElementSnapshot> snapshot = [self.testedApplication fb_customSnapshot];
58+
snapshot.children = @[];
59+
FBXCElementSnapshotWrapper *wrappedSnapshot = [FBXCElementSnapshotWrapper ensureWrapped:snapshot];
60+
NSString *xmlStr = [FBXPath xmlStringWithRootElement:wrappedSnapshot
61+
options:nil];
62+
int pid = [snapshot.accessibilityElement processIdentifier];
63+
XCTAssertNotNil(xmlStr);
64+
NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ type=\"%@\" name=\"%@\" label=\"%@\" enabled=\"%@\" visible=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\" index=\"%lu\" traits=\"%@\" processId=\"%d\" bundleId=\"%@\"/>\n", wrappedSnapshot.wdType, wrappedSnapshot.wdType, wrappedSnapshot.wdName, wrappedSnapshot.wdLabel, FBBoolToString(wrappedSnapshot.wdEnabled), FBBoolToString(wrappedSnapshot.wdVisible), FBBoolToString(wrappedSnapshot.wdAccessible), [wrappedSnapshot.wdRect[@"x"] stringValue], [wrappedSnapshot.wdRect[@"y"] stringValue], [wrappedSnapshot.wdRect[@"width"] stringValue], [wrappedSnapshot.wdRect[@"height"] stringValue], wrappedSnapshot.wdIndex, wrappedSnapshot.wdTraits, pid, [self.testedApplication bundleID]];
65+
XCTAssertEqualObjects(xmlStr, expectedXml);
66+
}
67+
5368
- (void)testSingleDescendantXMLRepresentation
5469
{
5570
id<FBXCElementSnapshot> snapshot = self.destinationSnapshot;

0 commit comments

Comments
 (0)