/** * 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 "FBDebugCommands.h" #import "FBApplication.h" #import "FBRouteRequest.h" #import "FBSession.h" #import "XCUIApplication+FBHelpers.h" #import "XCUIElement+FBUtilities.h" #import "FBXPath.h" @implementation FBDebugCommands #pragma mark - + (NSArray *)routes { return @[ [[FBRoute GET:@"/source"] respondWithTarget:self action:@selector(handleGetSourceCommand:)], [[FBRoute GET:@"/source"].withoutSession respondWithTarget:self action:@selector(handleGetSourceCommand:)], [[FBRoute GET:@"/wda/accessibleSource"] respondWithTarget:self action:@selector(handleGetAccessibleSourceCommand:)], [[FBRoute GET:@"/wda/accessibleSource"].withoutSession respondWithTarget:self action:@selector(handleGetAccessibleSourceCommand:)], ]; } #pragma mark - Commands static NSString *const SOURCE_FORMAT_XML = @"xml"; static NSString *const SOURCE_FORMAT_JSON = @"json"; static NSString *const SOURCE_FORMAT_DESCRIPTION = @"description"; + (id)handleGetSourceCommand:(FBRouteRequest *)request { FBApplication *application = request.session.application ?: [FBApplication fb_activeApplication]; NSString *sourceType = request.parameters[@"format"] ?: SOURCE_FORMAT_XML; id result; if ([sourceType caseInsensitiveCompare:SOURCE_FORMAT_XML] == NSOrderedSame) { [application fb_waitUntilSnapshotIsStable]; result = [FBXPath xmlStringWithSnapshot:application.fb_lastSnapshot]; } else if ([sourceType caseInsensitiveCompare:SOURCE_FORMAT_JSON] == NSOrderedSame) { result = application.fb_tree; } else if ([sourceType caseInsensitiveCompare:SOURCE_FORMAT_DESCRIPTION] == NSOrderedSame) { NSMutableArray *childrenDescriptions = [NSMutableArray array]; for (XCUIElement *child in [application childrenMatchingType:XCUIElementTypeAny].allElementsBoundByIndex) { [childrenDescriptions addObject:child.debugDescription]; } // debugDescription property of XCUIApplication instance shows descendants addresses in memory // instead of the actual information about them, however the representation works properly // for all descendant elements result = (0 == childrenDescriptions.count) ? application.debugDescription : [childrenDescriptions componentsJoinedByString:@"\n\n"]; } else { return FBResponseWithStatus( FBCommandStatusUnsupported, [NSString stringWithFormat:@"Unknown source format '%@'. Only %@ source formats are supported.", sourceType, @[SOURCE_FORMAT_XML, SOURCE_FORMAT_JSON, SOURCE_FORMAT_DESCRIPTION]] ); } if (nil == result) { return FBResponseWithErrorFormat(@"Cannot get '%@' source of the current application", sourceType); } return FBResponseWithObject(result); } + (id)handleGetAccessibleSourceCommand:(FBRouteRequest *)request { FBApplication *application = request.session.application ?: [FBApplication fb_activeApplication]; return FBResponseWithObject(application.fb_accessibilityTree ?: @{}); } @end