You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift UI测试调用XCUIDevice报错:Cannot find 'XCUIDevice' in scope

Swift UI测试中XCUIDevice找不到的问题解决

在Swift UI测试中尝试调用XCUIDevice.sharedDevice().orientation = .Portrait时,遇到错误提示:Cannot find 'XCUIDevice' in scope。

我的测试代码如下:

import XCTest

final class FastlaneScreenshotsLaunchTests: XCTestCase {
    override class var runsForEachTargetApplicationUIConfiguration: Bool {
        true
    }

    override func setUpWithError() throws {
        continueAfterFailure = false
    }
            
    override func setUp() {
        super.setUp()
        
        let app = XCUIApplication()
        setupSnapshot(app)
        app.launch()
    }

    func testLaunch() throws {
        snapshot("0Launch")
        let app = XCUIApplication()
        XCUIDevice.sharedDevice().orientation = .Portrait // Cannot find 'XCUIDevice' in scope
    }

}

我原本想实现旋转模拟器的功能,根据搜索结果,原本认为XCUIDevice应该处于作用域内。

问题解决:
测试目标中包含了Mac Catalyst目标,而该目标不支持XCUIDevice。移除Mac Catalyst目标后,错误就解决了。

内容的提问来源于stack exchange,提问作者Todd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 14:30:51