XCUIElement+FBPickerWheel.m 2.3 KB

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