UI 在 Xcode 7 中测试时应用资源不可用
App resources not available when UI testing in Xcode 7
我正在尝试通过快照当前屏幕元素(标签、图像、按钮)并将其辅助功能信息保存到 json 来扩展 Xcode 7 中新的 UI 测试功能] 个文件。
想法是,当 运行 稍后 UI 测试时,可以拍摄当前屏幕快照并与现有屏幕快照进行比较,如果发现额外或缺失的元素,测试将失败。
不幸的是,在 UI 测试期间,即使目标正确,应用程序资源似乎也不可用,因此无法加载 json 文件进行比较。以下标准代码无法加载资源:
guard let resourcePath = NSBundle.mainBundle ().pathForResource ("StartScreenShapshot", ofType:"json") else {
XCTFail ("can't load resource StartScreenShapshot")
return
}
我能理解为什么 Apple 采用这种沙盒方法,因为 UI 测试应该基于屏幕上发生的事情,不需要访问应用程序的工作,但没有访问权限资源包很痛苦。
在 Xcode 7 UI 测试期间,有没有办法从应用程序或本地以其他方式加载本地资源?
在本地(自动)保存文件也是一个巨大的优势,可以节省手动创建文件的时间。
感谢@sage444
对于单元测试,mainBundle() 方法无法检索资源路径,但使用 class 可以。
guard let resourcePath = NSBundle (forClass: self.dynamicType).pathForResource (contentName, ofType:"json") else {
XCTFail ("can't load resource \(contentName)")
return
}
感谢@danfordham
更新 Swift 3
1) 复制包资源
2) 以这种方式引用新包,
guard let path = Bundle(for: type(of: self)).path(forResource: contentName, ofType: "json") else {
XCTFail ("can't load resource \(contentName)")
return
}
我正在尝试通过快照当前屏幕元素(标签、图像、按钮)并将其辅助功能信息保存到 json 来扩展 Xcode 7 中新的 UI 测试功能] 个文件。
想法是,当 运行 稍后 UI 测试时,可以拍摄当前屏幕快照并与现有屏幕快照进行比较,如果发现额外或缺失的元素,测试将失败。
不幸的是,在 UI 测试期间,即使目标正确,应用程序资源似乎也不可用,因此无法加载 json 文件进行比较。以下标准代码无法加载资源:
guard let resourcePath = NSBundle.mainBundle ().pathForResource ("StartScreenShapshot", ofType:"json") else {
XCTFail ("can't load resource StartScreenShapshot")
return
}
我能理解为什么 Apple 采用这种沙盒方法,因为 UI 测试应该基于屏幕上发生的事情,不需要访问应用程序的工作,但没有访问权限资源包很痛苦。
在 Xcode 7 UI 测试期间,有没有办法从应用程序或本地以其他方式加载本地资源?
在本地(自动)保存文件也是一个巨大的优势,可以节省手动创建文件的时间。
感谢@sage444
对于单元测试,mainBundle() 方法无法检索资源路径,但使用 class 可以。
guard let resourcePath = NSBundle (forClass: self.dynamicType).pathForResource (contentName, ofType:"json") else {
XCTFail ("can't load resource \(contentName)")
return
}
感谢@danfordham
更新 Swift 3
1) 复制包资源
2) 以这种方式引用新包,
guard let path = Bundle(for: type(of: self)).path(forResource: contentName, ofType: "json") else {
XCTFail ("can't load resource \(contentName)")
return
}