Comments (7)
Ah cool good to know! If it's only frames, then using the device sizing+traits we do for image snapshots probably would fix this! We could look into beefing up recursiveDescription
to take the same options that .image
does.
Another approach is for you and your team to settle on a single canonical simulator to use for recording screenshots. This is what we did at Kickstarter and other places, and seems to be a common approach.
Thanks for the info!
from swift-snapshot-testing.
Hey @kaqu! We haven't run into that yet cause we typically run our snapshot tests from a framework target instead of an app target. If you can do the same, then that would fix it. Otherwise it sounds like we need to encode some simulator properties into the file names of the snapshots for people that want to run tests on multiple simulators. We can look into that soon!
from swift-snapshot-testing.
Oh, also, that .iPhoneX
stuff won't be too helpful here because that only overrides traits in the view/controller. I think you will still see some difference.
If you are willing to share, could you show us some of the differences you are getting on different simulators? I'm curious what it looks like. Thanks!
from swift-snapshot-testing.
Thanks for quick response π I always run tests on the same device, but my colleague found that when doing PR and running tests on a different device (currently selected in Xcode), so It would be nice to keep that in sync between developers without keeping in mind to test outside of Xcode or with certain settings.
Here is a diff I've got from one of my views:
@@ β1,11 +1,11 @@
-<UIView; frame = (0 0; 375 812); autoresize = W+H; layer = <CALayer>>
- | <NavigationBar; frame = (0 0; 375 109); layer = <CALayer>>
- | | <UIStackView; frame = (20 44; 335 60); layer = <CATransformLayer>>
+<UIView; frame = (0 0; 414 896); autoresize = W+H; layer = <CALayer>>
+ | <NavigationBar; frame = (0 0; 414 109); layer = <CALayer>>
+ | | <UIStackView; frame = (20 44; 374 60); layer = <CATransformLayer>>
β | | | <UIStackView; frame = (0 30; 0 0); layer = <CATransformLayer>>
- | | | <Label; baseClass = UILabel; frame = (102.667 19.3333; 129.667 21.6667); text = 'TEXT'; userInteractionEnabled = NO; layer = <_UILabelLayer>>
- | | | <UIStackView; frame = (335 30; 0 0); layer = <CATransformLayer>>
- | | <UIImageView; frame = (0 104; 375 5); opaque = NO; userInteractionEnabled = NO; layer = <CALayer>>
- | <UIView; frame = (0 109; 375 703); autoresize = W+H; layer = <CALayer>>
- | | <Label; baseClass = UILabel; frame = (0 20; 375 0); hidden = YES; userInteractionEnabled = NO; layer = <_UILabelLayer>>
- | | <ImageView; baseClass = UIImageView; frame = (140.667 214.667; 94 94); opaque = NO; userInteractionEnabled = NO; layer = <CALayer>>
- | | <Label; baseClass = UILabel; frame = (0 328.667; 375 19.3333); text = 'TEXT'; userInteractionEnabled = NO; layer = <_UILabelLayer>>
+ | | | <Label; baseClass = UILabel; frame = (122.333 19.3333; 129.667 21.6667); text = 'TEXT'; userInteractionEnabled = NO; layer = <_UILabelLayer>>
+ | | | <UIStackView; frame = (374 30; 0 0); layer = <CATransformLayer>>
+ | | <UIImageView; frame = (0 104; 414 5); opaque = NO; userInteractionEnabled = NO; layer = <CALayer>>
+ | <UIView; frame = (0 109; 414 787); autoresize = W+H; layer = <CALayer>>
+ | | <Label; baseClass = UILabel; frame = (0 20; 414 0); hidden = YES; userInteractionEnabled = NO; layer = <_UILabelLayer>>
+ | | <ImageView; baseClass = UIImageView; frame = (160 256.667; 94 94); opaque = NO; userInteractionEnabled = NO; layer = <CALayer>>
+ | | <Label; baseClass = UILabel; frame = (0 370.667; 414 19.3333); text = 'TEXT'; userInteractionEnabled = NO; layer = <_UILabelLayer>>
Recorded on default iPhoneX and failed on default iPhone Xs Max simulator. Looks like only frames changed.
from swift-snapshot-testing.
It will be nice to have tests independent from developer environment. You can achieve this using specialized image snapshots, it will produce always the same results. Making that possible for recursive description might increase its usage (it is awesome!) π I am wondering if this is the case for any other snapshot type, but it might be only view specific (I don't know if that is the case for NSView since I have used it only on iOS).
from swift-snapshot-testing.
I have noticed something that maybe related to this issue, I still have limited understanding of when the layout constraints update took place, and when the view is being snapshot, which could affect the result of the snapshot output.
I noticed that if I declare a constraint that's dependent on a view.bounds
property at runtime, this will cause the snapshot output to be dependent on which Simulator with the corresponding screen size that we're running on.
For example, the following code will only produce a snapshot output that took the value of view.bounds.width
in reference to the simulator it's running on, instead of the ViewImageConfig
of the snapshot image. As a result the width is smaller when it's displayed on a larger view configuration.
override func viewDidLoad() {
super.viewDidLoad()
let offset: CGFloat = 20.0
textField.widthAnchor.constraint(equalTo: view.bounds.width - 2*offset).isActive = true
// This text field is off screen initially and it doesn't have a right anchor,
// but instead constrained by its width anchor
textField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: view.bounds.width + offset).isActive = true
}
I realized this is an unconventional layout declaration, so let me visualize it:
- The initial layout
offscreen | visible | offscreen |
---|---|---|
[View] | [TextField] |
- On user action, text field will be animated to the left, and then layout will be updated
offscreen | visible | offscreen |
---|---|---|
[View] | [TextField] |
I'm not sure if I should have find a more stable declaration of width anchor of the text field, or whether should I dig deeper into the Snapshot testing process, to resolve this.
Also, I have tried to improve the layout constraints declaration, to rely on the widthAnchor
of the view
of a view controller. Which surprisingly works at runtime. I thought I have to set a widthAnchor
explicitly to a value before using it. But, even in this case, snapshot still produced unexpected output if I update the initial layout to a runtime value of a view.bounds
, and then take snapshot on different ViewImageConfig
.
from swift-snapshot-testing.
Also, I have tried to improve the layout constraints declaration, to rely on the
widthAnchor
of theview
of a view controller. Which surprisingly works at runtime. I thought I have to set awidthAnchor
explicitly to a value before using it. But, even in this case, snapshot still produced unexpected output if I update the initial layout to a runtime value of aview.bounds
, and then take snapshot on differentViewImageConfig
.
I discovered that to maintain consistency on various image snapshot of layout update on taking snapshot (updating means to modify the constant
value of NSLayoutConstraint
), a different instance of each view controller is needed.
For example, instead of this:
assertSnapshot(matching: vc, as: .image(on: .iPhoneSe))
assertSnapshot(matching: vc, as: .image(on: .iPhone8))
assertSnapshot(matching: vc, as: .image(on: .iPhoneX))
A separate instance need to be used:
func viewController() -> UIViewController {
return UIViewController()
}
assertSnapshot(matching: viewController(), as: .image(on: .iPhoneSe))
assertSnapshot(matching: viewController(), as: .image(on: .iPhone8))
assertSnapshot(matching: viewController(), as: .image(on: .iPhoneX))
from swift-snapshot-testing.
Related Issues (20)
- Timeout Error During Snapshot Testing with WKWebView
- Snapshots tests fail when Json returns in different order, HOT 1
- macCatalyst: Snapshots generated differ a lot between local computer and a MacStadium M1 mac mini HOT 1
- Run success on local and xcode on CI Runner but test failed when using fastlane run_tests HOT 5
- Text-based snapshots break across platforms due to line ending differences HOT 2
- Undefined symbols Linker command failed with exit code 1. HOT 1
- Snapshot adds imported module's name that fails match with reference shapshot
- Deadlock in inline snapshot testing HOT 4
- What's the way to use `assertSnapshot` in async test? HOT 3
- How to snapshot on special iOS version? HOT 1
- Optimizing Image Handling in Swift Snapshot Testing
- Snapshot of table view with combine receiver happens too late HOT 1
- swift-syntax 510.0.0 update not possible HOT 1
- How to generate iPhone 15 Snapshots with Xcode 15, as view.swift doesn't contain those dimensions HOT 1
- I get "Process completed with exit code 65" when I use "assertSnapshot(of: vc, as: .image)" function on GitHub Actions.
- `diffTool` have a Swift 6 warning on Xcode 15.3 with Strict Concurrency Checking set to Complete HOT 3
- Privacy manifest for AppStore releases HOT 1
- "The file β...β couldnβt be opened because you donβt have permission to view it" after switching branch
- Privacy manifest file / Privacy Rules HOT 1
- Blank diff snapshot
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift-snapshot-testing.