这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 52 additions & 13 deletions WebDriverAgentLib/Utilities/FBXPath.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
#import "FBXMLGenerationOptions.h"
#import "FBXCElementSnapshotWrapper+Helpers.h"
#import "NSString+FBXMLSafeString.h"
#import "XCUIApplication.h"
#import "XCUIElement.h"
#import "XCUIElement+FBCaching.h"
#import "XCUIElement+FBUtilities.h"
#import "XCUIElement+FBWebDriverAttributes.h"
#import "XCTestPrivateSymbols.h"
#import "FBElementHelpers.h"
#import "FBXCAXClientProxy.h"
#import "FBXCAccessibilityElement.h"


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

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

+ (NSArray<Class> *)supportedAttributes;

Expand Down Expand Up @@ -98,7 +102,13 @@ @interface FBInternalIndexAttribute : FBElementAttribute

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

+ (int)recordWithWriter:(xmlTextWriterPtr)writer forValue:(NSString *)value;
@end

@interface FBApplicationBundleIdAttribute : FBElementAttribute

@end

@interface FBApplicationPidAttribute : FBElementAttribute

@end

Expand Down Expand Up @@ -472,6 +482,27 @@ + (int)recordElementAttributes:(xmlTextWriterPtr)writer
// index path is the special case
return [FBInternalIndexAttribute recordWithWriter:writer forValue:indexPath];
}
if (element.elementType == XCUIElementTypeApplication) {
// only record process identifier and bundle identifier for the application element
int pid = [element.accessibilityElement processIdentifier];
if (pid > 0) {
int rc = [FBApplicationPidAttribute recordWithWriter:writer
forValue:[NSString stringWithFormat:@"%d", pid]];
if (rc < 0) {
return rc;
}
XCUIApplication *app = [[FBXCAXClientProxy sharedClient]
monitoredApplicationWithProcessIdentifier:pid];
NSString *bundleID = [app bundleID];
if (nil != bundleID) {
rc = [FBApplicationBundleIdAttribute recordWithWriter:writer
forValue:bundleID];
if (rc < 0) {
return rc;
}
}
}
}
return 0;
}

Expand Down Expand Up @@ -585,6 +616,11 @@ + (NSString *)valueForElement:(id<FBElement>)element
+ (int)recordWithWriter:(xmlTextWriterPtr)writer forElement:(id<FBElement>)element
{
NSString *value = [self valueForElement:element];
return [self recordWithWriter:writer forValue:value];
}

+ (int)recordWithWriter:(xmlTextWriterPtr)writer forValue:(nullable NSString *)value
{
if (nil == value) {
// Skip the attribute if the value equals to nil
return 0;
Expand Down Expand Up @@ -830,22 +866,25 @@ + (NSString *)name
return kXMLIndexPathKey;
}

+ (int)recordWithWriter:(xmlTextWriterPtr)writer forValue:(NSString *)value
@end

@implementation FBApplicationBundleIdAttribute : FBElementAttribute

+ (NSString *)name
{
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;
return @"bundleId";
}

@end

@implementation FBApplicationPidAttribute : FBElementAttribute

+ (NSString *)name
{
return @"processId";
}

@end

@implementation FBPlaceholderValueAttribute

Expand Down
15 changes: 15 additions & 0 deletions WebDriverAgentTests/IntegrationTests/FBXPathIntegrationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
#import "FBMacros.h"
#import "FBTestMacros.h"
#import "FBXPath.h"
#import "FBXCAccessibilityElement.h"
#import "FBXCodeCompatibility.h"
#import "FBXCElementSnapshotWrapper+Helpers.h"
#import "FBXMLGenerationOptions.h"
#import "XCUIApplication.h"
#import "XCUIElement.h"
#import "XCUIElement+FBFind.h"
#import "XCUIElement+FBUtilities.h"
Expand Down Expand Up @@ -50,6 +52,19 @@ - (void)setUp
return snapshot;
}

- (void)testApplicationNodeXMLRepresentation
{
id<FBXCElementSnapshot> snapshot = [self.testedApplication fb_customSnapshot];
snapshot.children = @[];
FBXCElementSnapshotWrapper *wrappedSnapshot = [FBXCElementSnapshotWrapper ensureWrapped:snapshot];
NSString *xmlStr = [FBXPath xmlStringWithRootElement:wrappedSnapshot
options:nil];
int pid = [snapshot.accessibilityElement processIdentifier];
XCTAssertNotNil(xmlStr);
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]];
XCTAssertEqualObjects(xmlStr, expectedXml);
}

- (void)testSingleDescendantXMLRepresentation
{
id<FBXCElementSnapshot> snapshot = self.destinationSnapshot;
Expand Down
Loading