XCUIElementQuery+FBHelpers.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * Copyright (c) 2018-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 "XCUIElementQuery+FBHelpers.h"
  10. #import "FBXCodeCompatibility.h"
  11. #import "XCUIElementQuery.h"
  12. #import "FBXCElementSnapshot.h"
  13. @implementation XCUIElementQuery (FBHelpers)
  14. - (nullable id<FBXCElementSnapshot>)fb_cachedSnapshot
  15. {
  16. id<FBXCElementSnapshot> rootElementSnapshot = self.rootElementSnapshot;
  17. if (nil == rootElementSnapshot) {
  18. return nil;
  19. }
  20. XCUIElementQuery *inputQuery = self;
  21. NSMutableArray<id<XCTElementSetTransformer>> *transformersChain = [NSMutableArray array];
  22. while (nil != inputQuery && nil != inputQuery.transformer) {
  23. [transformersChain insertObject:inputQuery.transformer atIndex:0];
  24. inputQuery = inputQuery.inputQuery;
  25. }
  26. NSMutableArray *snapshots = [NSMutableArray arrayWithObject:rootElementSnapshot];
  27. [snapshots addObjectsFromArray:rootElementSnapshot._allDescendants];
  28. NSOrderedSet *matchingSnapshots = [NSOrderedSet orderedSetWithArray:snapshots];
  29. @try {
  30. for (id<XCTElementSetTransformer> transformer in transformersChain) {
  31. matchingSnapshots = (NSOrderedSet *)[transformer transform:matchingSnapshots
  32. relatedElements:nil];
  33. }
  34. return matchingSnapshots.count == 1 ? matchingSnapshots.firstObject : nil;
  35. } @catch (NSException *e) {
  36. [FBLogger logFmt:@"Got an unexpected error while retriveing the cached snapshot: %@", e.reason];
  37. }
  38. return nil;
  39. }
  40. @end