简体   繁体   English

XCUITest 如何关闭日期选择器弹出框

[英]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.在 iOS 15 中,我的应用程序中有一个使用.compact样式的日期选择器,因此它将日期和时间选择 UI 显示为弹出框。

I can't find a way to dismiss the popover in a UI test, having tried suggestions for popover dismissal such as:我在 UI 测试中找不到关闭弹出框的方法,尝试了弹出框关闭的建议,例如:

  • app.otherElements["PopoverDismissRegion"] (didn't exist) app.otherElements["PopoverDismissRegion"] (不存在)
  • app.otherElements["dismiss popup"].tap() (didn't exist) app.otherElements["dismiss popup"].tap() (不存在)
  • .windows.element(boundBy: 0).tap() (just hits the centre of the screen, which is still the popover so does nothing of use) .windows.element(boundBy: 0).tap() (只是点击屏幕的中心,这仍然是弹出框,所以没有任何用处)

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.这在 iPhone 模拟器上对我有用,根据答案,它也适用于横向 iPad,但不适用于纵向。

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).我通过“强制”点击另一个我知道在屏幕上的元素(header 文本)解决了这个问题。 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()
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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