FBDebugCommands.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import "FBDebugCommands.h"
  10. #import "FBRouteRequest.h"
  11. #import "FBSession.h"
  12. #import "FBXMLGenerationOptions.h"
  13. #import "XCUIApplication+FBHelpers.h"
  14. #import "XCUIElement+FBUtilities.h"
  15. #import "FBXPath.h"
  16. @implementation FBDebugCommands
  17. #pragma mark - <FBCommandHandler>
  18. + (NSArray *)routes
  19. {
  20. return
  21. @[
  22. [[FBRoute GET:@"/source"] respondWithTarget:self action:@selector(handleGetSourceCommand:)],
  23. [[FBRoute GET:@"/source"].withoutSession respondWithTarget:self action:@selector(handleGetSourceCommand:)],
  24. [[FBRoute GET:@"/wda/accessibleSource"] respondWithTarget:self action:@selector(handleGetAccessibleSourceCommand:)],
  25. [[FBRoute GET:@"/wda/accessibleSource"].withoutSession respondWithTarget:self action:@selector(handleGetAccessibleSourceCommand:)],
  26. ];
  27. }
  28. #pragma mark - Commands
  29. static NSString *const SOURCE_FORMAT_XML = @"xml";
  30. static NSString *const SOURCE_FORMAT_JSON = @"json";
  31. static NSString *const SOURCE_FORMAT_DESCRIPTION = @"description";
  32. + (id<FBResponsePayload>)handleGetSourceCommand:(FBRouteRequest *)request
  33. {
  34. // This method might be called without session
  35. XCUIApplication *application = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
  36. NSString *sourceType = request.parameters[@"format"] ?: SOURCE_FORMAT_XML;
  37. NSString *sourceScope = request.parameters[@"scope"];
  38. id result;
  39. if ([sourceType caseInsensitiveCompare:SOURCE_FORMAT_XML] == NSOrderedSame) {
  40. NSArray<NSString *> *excludedAttributes = nil == request.parameters[@"excluded_attributes"]
  41. ? nil
  42. : [request.parameters[@"excluded_attributes"] componentsSeparatedByString:@","];
  43. result = [application fb_xmlRepresentationWithOptions:
  44. [[[FBXMLGenerationOptions new]
  45. withExcludedAttributes:excludedAttributes]
  46. withScope:sourceScope]];
  47. } else if ([sourceType caseInsensitiveCompare:SOURCE_FORMAT_JSON] == NSOrderedSame) {
  48. result = application.fb_tree;
  49. } else if ([sourceType caseInsensitiveCompare:SOURCE_FORMAT_DESCRIPTION] == NSOrderedSame) {
  50. result = application.fb_descriptionRepresentation;
  51. } else {
  52. return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:[NSString stringWithFormat:@"Unknown source format '%@'. Only %@ source formats are supported.",
  53. sourceType, @[SOURCE_FORMAT_XML, SOURCE_FORMAT_JSON, SOURCE_FORMAT_DESCRIPTION]] traceback:nil]);
  54. }
  55. if (nil == result) {
  56. return FBResponseWithUnknownErrorFormat(@"Cannot get '%@' source of the current application", sourceType);
  57. }
  58. return FBResponseWithObject(result);
  59. }
  60. + (id<FBResponsePayload>)handleGetAccessibleSourceCommand:(FBRouteRequest *)request
  61. {
  62. // This method might be called without session
  63. XCUIApplication *application = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
  64. return FBResponseWithObject(application.fb_accessibilityTree ?: @{});
  65. }
  66. @end