FBDebugCommands.m 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. NSString *excludedAttributesString = request.parameters[@"excluded_attributes"];
  49. NSSet<NSString *> *excludedAttributes = (excludedAttributesString == nil)
  50. ? nil
  51. : [NSSet setWithArray:[excludedAttributesString componentsSeparatedByString:@","]];
  52. result = [application fb_tree:excludedAttributes];
  53. } else if ([sourceType caseInsensitiveCompare:SOURCE_FORMAT_DESCRIPTION] == NSOrderedSame) {
  54. result = application.fb_descriptionRepresentation;
  55. } else {
  56. return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:[NSString stringWithFormat:@"Unknown source format '%@'. Only %@ source formats are supported.",
  57. sourceType, @[SOURCE_FORMAT_XML, SOURCE_FORMAT_JSON, SOURCE_FORMAT_DESCRIPTION]] traceback:nil]);
  58. }
  59. if (nil == result) {
  60. return FBResponseWithUnknownErrorFormat(@"Cannot get '%@' source of the current application", sourceType);
  61. }
  62. return FBResponseWithObject(result);
  63. }
  64. + (id<FBResponsePayload>)handleGetAccessibleSourceCommand:(FBRouteRequest *)request
  65. {
  66. // This method might be called without session
  67. XCUIApplication *application = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
  68. return FBResponseWithObject(application.fb_accessibilityTree ?: @{});
  69. }
  70. @end