I'm trying to extend the new UI testing functionality in Xcode 7 by snapshotting the current screen elements (labels, images, buttons) and saving their accessibility information to json files.
The idea is that when running the UI tests later, a current screen snapshot can be taken and compared to the existing one, the test will fail if additional or missing elements are found.
Unfortunately the app resources don't seem available during UI testing, even with the correct target, so the json files can't be loaded for comparison. The following standard code fails to load a resource:
guard let resourcePath = NSBundle.mainBundle ().pathForResource ("StartScreenShapshot", ofType:"json") else {
XCTFail ("can't load resource StartScreenShapshot")
return
}
I can understand why Apple have taken this sandbox approach, as UI testing should be based on what's happening on the screen, and access to the workings of app shouldn't be needed, but not having access to the resource bundle is a pain.
So is there a way to load local resources from the app, or some other way locally, during Xcode 7 UI testing?
Saving the files locally (automatically) would also be a huge plus, would save creating them manually.
Thanks to @sage444
For unit testing the mainBundle() method doesn't work for retrieving a resource path, using a class does.
guard let resourcePath = NSBundle (forClass: self.dynamicType).pathForResource (contentName, ofType:"json") else {
XCTFail ("can't load resource \(contentName)")
return
}
Thanks @danfordham
Updated for Swift 3
1) Copy bundle resources
2) Reference new bundle this way,
guard let path = Bundle(for: type(of: self)).path(forResource: contentName, ofType: "json") else {
XCTFail ("can't load resource \(contentName)")
return
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With