XCUIElement+FBPickerWheel.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "XCUIElement+FBPickerWheel.h"
  10. #import "FBRunLoopSpinner.h"
  11. #import "FBXCElementSnapshot.h"
  12. #import "FBXCodeCompatibility.h"
  13. #import "XCUICoordinate.h"
  14. #import "XCUIElement+FBCaching.h"
  15. #import "XCUIElement+FBResolve.h"
  16. #if !TARGET_OS_TV
  17. @implementation XCUIElement (FBPickerWheel)
  18. static const NSTimeInterval VALUE_CHANGE_TIMEOUT = 2;
  19. - (BOOL)fb_scrollWithOffset:(CGFloat)relativeHeightOffset error:(NSError **)error
  20. {
  21. id<FBXCElementSnapshot> snapshot = self.fb_isResolvedFromCache.boolValue
  22. ? self.lastSnapshot
  23. : self.fb_takeSnapshot;
  24. NSString *previousValue = snapshot.value;
  25. XCUICoordinate *startCoord = [self coordinateWithNormalizedOffset:CGVectorMake(0.5, 0.5)];
  26. XCUICoordinate *endCoord = [startCoord coordinateWithOffset:CGVectorMake(0.0, relativeHeightOffset * snapshot.frame.size.height)];
  27. // If picker value is reflected in its accessiblity id
  28. // then fetching of the next snapshot may fail with StaleElementReferenceError
  29. // because we bound elements by their accessbility ids by default.
  30. // Fetching stable instance of an element allows it to be bounded to the
  31. // unique element identifier (UID), so it could be found next time even if its
  32. // id is different from the initial one. See https://github.com/appium/appium/issues/17569
  33. XCUIElement *stableInstance = self.fb_stableInstance;
  34. [endCoord tap];
  35. return [[[[FBRunLoopSpinner new]
  36. timeout:VALUE_CHANGE_TIMEOUT]
  37. timeoutErrorMessage:[NSString stringWithFormat:@"Picker wheel value has not been changed after %@ seconds timeout", @(VALUE_CHANGE_TIMEOUT)]]
  38. spinUntilTrue:^BOOL{
  39. return ![stableInstance.value isEqualToString:previousValue];
  40. }
  41. error:error];
  42. }
  43. - (BOOL)fb_selectNextOptionWithOffset:(CGFloat)offset error:(NSError **)error
  44. {
  45. return [self fb_scrollWithOffset:offset error:error];
  46. }
  47. - (BOOL)fb_selectPreviousOptionWithOffset:(CGFloat)offset error:(NSError **)error
  48. {
  49. return [self fb_scrollWithOffset:-offset error:error];
  50. }
  51. @end
  52. #endif