【问题标题】:In Xcode UI Testing is it possible to get the orientation of the view在 Xcode UI 测试中是否可以获取视图的方向
【发布时间】:2016-06-24 07:14:15
【问题描述】:

对于我的应用程序,我正在尝试运行一个基本的 UI 测试,以了解当实际设备旋转时视图是否保持固定方向。我知道如何模拟物理旋转(使用 UIDeviceOrientation),但是有没有办法获得实际视图的方向?

【问题讨论】:

  • 由于视图并没有真正的方向概念,所以根本不清楚您要问什么。
  • 我想这个问题可以改写。当设备方向更改为 .Landscape 时,您可以实际测试您的应用程序不旋转吗?如果是,如何?顺便说一句,我自己也在调查。

标签: ios user-interface xcode-ui-testing


【解决方案1】:

您可以使用这条线获得方向

XCUIDevice.sharedDevice().orientation

【讨论】:

  • 我认为问题是你能得到视图的方向,这得到了设备的模拟方向。即,用户想要做的是确保视图方向(一个没有意义的概念,但是......)无论设备方向如何都保持不变。
  • 他说他可以模拟旋转,所以他要测试当前方向,模拟旋转并测试方向是否改变
  • 抱歉,我是 iOS 开发新手。你是对的,大卫,这就是我要问的。你认为 UI 测试允许这种测试吗?
  • 这个测试有效,但是你看不到旋转发生XCUIDevice.sharedDevice().orientation = .LandscapeLeft XCTAssertTrue(XCUIDevice.sharedDevice().orientation == .LandscapeLeft) XCUIDevice.sharedDevice().orientation = .Portrait XCTAssertTrue(XCUIDevice.sharedDevice().orientation == .Portrait)
  • 答案无关紧要且具有误导性。问题是针对应用程序、视图而不是设备
【解决方案2】:

我找到了解决方法。这不是很好,但它正在完成这项工作。如果有的话,我会尝试找到一种更优雅的方式。

您需要做的是找到一个接口并检查它的方向。在我的情况下,我想测试当我的设备转向 .Landscape 时 UI 是否保持在 .Portrait 方向。

func testSPSCLaunchScreenLandscape() {
    // Use recording to get started writing UI tests.
    // Use XCTAssert and related functions to verify your tests produce the correct results.
    XCUIDevice.sharedDevice().orientation = .LandscapeLeft
    XCTAssert(UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation))
}

【讨论】:

  • 这对我不起作用。我仍然得到 .LandscapeLeft
【解决方案3】:

sharedDevice 已重命名。现在您可以像这样获取和设置值。

XCUIDevice.shared.orientation = .portrait

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多