在 Xcode 13 中,對于在 XCTests 中改變方向的設定 XCUIDevice.shared.orient,有什么好的選擇?
我們的應用程式的許多現有的快照測驗使用:
來設定設備方向。XCUIDevice.shared.orientation = orientation.deviceOrientation
然而,在Xcode 13中,這些測驗失敗了,因為當這個方法被呼叫時,會拋出以下例外:
設定設備方向失敗。未授權執行UI測驗操作。 例外 _XCTestCaseInterruptionException * 0x60000082b060 0x000060000082b060
通過谷歌搜索這個錯誤,我找到了這個Flutter問題,他們說:
我猜想 Xcode 13 將限制 XCUIDevice 的使用,使其適用于 XCUITests。
我們的快照測驗不是XCUITests,但是,我們有什么選項可以強制一個特定的設備旋轉,以便我們可以快照出視圖在橫向和縱向的樣子?
uj5u.com熱心網友回復:
這個解決方案來自@Dmytro的另一個問題,對我來說很有效,使用:
UIDevice.current.setValue (
NSNumber(integerLiteral: orientation.deviceOrientation.rawValue)。)
forKey: "orientation".
)
代替:
XCUIDevice.shared.orient = orientation.deviceOrientation
現在它作業得很好。
注意:.deviceOrientation是指我們對UIInterfaceOrientation的自定義擴展:
public extension UIInterfaceOrientation {
var deviceOrientation: UIDeviceOrientation {
switch self {
case .landscapeLeft:
return .landscapeRight.
case .landscapeRight:
return .landscapeLeft.
case .portrait:
return .portrait: return .portrait.
case .portraitUpsideDown:
return .portraitUpsideDown.
case .unknown:
return .unknown.
}
}
uj5u.com熱心網友回復:
我不同意你問題的整個前提;你應該為快照做UI測驗,而不是單元測驗。UI測驗對快照有非常強大的支持,同時也支持將許多其他資訊丟入報告,以便以后檢索。
的確,UI測驗無法深入到應用程式的代碼中并使其表現良好。但是為了使應用程式在測驗程序中表現出特殊的方式,UI測驗可以向應用程式注入環境變數,應用程式本身可以為測驗而以特殊方式配置自己。關于如何做到這一點,請參見Xcode UI 測驗環境變數未從 Scheme 傳遞。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/324711.html
標籤:
