简体   繁体   中英

XCUITest How to dismiss date picker popover

In iOS 15 I've got a date picker in my app that uses the .compact style so it presents the date and time selection UI as popovers.

I can't find a way to dismiss the popover in a UI test, having tried suggestions for popover dismissal such as:

  • app.otherElements["PopoverDismissRegion"] (didn't exist)
  • app.otherElements["dismiss popup"].tap() (didn't exist)
  • .windows.element(boundBy: 0).tap() (just hits the centre of the screen, which is still the popover so does nothing of use)

It took a bit of time searching around, looking for non-datepicker related answers that might work but I found this answer , which suggested that this would work:

app.swipeDown(velocity: .fast)

That did the trick for me on an iPhone simulator and according to the answer it works for landscape iPads too, though not on portrait.

Duplicating the answer as the original doesn't reference date picker so is harder to find!

It's incredible that tapping the "supplied" dismissal button doesn't work, eh?

I've solved this by "force" tapping another element I know to be on the screen (a header text). This obviously won't work if the date picker is covering that element, but in my case it never has.

extension XCUIElement {

  func forceTap() {
    self.coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0)).tap()
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM