Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix snapshots for wkwebview to let data load #857

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

PriyankaAngotra
Copy link

@PriyankaAngotra PriyankaAngotra commented Jun 7, 2024

In this PR

Since ver1.15.2, snapshots have been broken for WKWebView since it no more lets the data load before taking snapshot. This has broken our(private company) tests since we want to verify that the data we pass wkwebview loads before taking a snapshot. As a result, we've been forced to stick to 1.15.0 and miss out on latest updates.
This PR reverts that change from 1.15.2. Happy to have a conversation regarding this particular fix which was done for a different issue.

@carlos-santos-anchor
Copy link

I think it was introduced here: #692

@kahseng-seek
Copy link

Our organization is also experiencing the same issue with the web view snapshot since version 1.15.2. Is there any plan to merge this? Are there any recommended workarounds we could use at the moment?

@mbrandonw
Copy link
Member

Hi @PriyankaAngotra and @kahseng-seek, according to #692 this was needed to fix to prevent a crash:

Without this patch snapshots crash, with this patch, only the background color of the webview is snapped.

And multiple people seemed to have corroborated that. Are you saying that is not true?

@PriyankaAngotra
Copy link
Author

Hi @mbrandonw, that's correct. From the description it seems like the patch was more of a workaround and will probably need a different solution with some debugging. With this PR, a snapshot is generated for wkwebview with the html data loaded as was my experience. The use case is very simple with terms and conditions being loaded in a wkwebview through a viewModel after which a screenshot is generated. With the previous patch fix (1.15.2), the screenshot generated is blank. The above mentioned version is what broke it as it displayed fine before.

@mbrandonw
Copy link
Member

Hi @priyankaA87, honestly it's tough to make a call on this. We have one PR saying this changed prevents crashes and worked for them, and now we have another saying it causes problems.

Is there a reason you can't vend your own custom strategy for snapshotting web views that behaves how you want? While the library does ship a few basic snapshot strategies, in practice this has been very difficult to maintain. Multiple people can want a single strategy to behave in completely different ways, making it basically impossible to appease everyone.

@PriyankaAngotra
Copy link
Author

I understand your position isn't easy here. I am not entirely clear on the original issue but if @teameh can provide a simple example of the original issue, that might be useful to make a judgement. As I understand, a WKWebView should be able to load a basic HTML at the very least for an acceptable screenshot to be taken which is what I tried to test.

@teameh
Copy link
Contributor

teameh commented Aug 29, 2024

Hi all and thanks for tagging me! Well the patch I added was indeed a workaround for a crashing WebView with XCode 14 and iOS 16. If this is not crashing any more without the patch, then my patch could be reverted or this PR could be applied.

However, I think it would also be good to take a look at the test case for snapping WebViews. https://github.com/pointfreeco/swift-snapshot-testing/blob/main/Tests/SnapshotTestingTests/SnapshotTestingTests.swift#L1130-L1146 This is currently not executed on the CI so nobody is seeing that is would be failing (same for me when submitting the patch).

If I now run the test locally with or without the patch, I get a transparant image as the result instead of the expected webpage:

image

@mbrandonw
Copy link
Member

Hi @teameh, FWIW we do all of our web view snapshotting on Mac when testing for the Point-Free website, and that works just fine. I'm not sure when this strategy stopped work for iOS, but this does bring me back to my previous point:

Is there a reason you can't vend your own custom strategy for snapshotting web views that behaves how you want? While the library does ship a few basic snapshot strategies, in practice this has been very difficult to maintain. Multiple people can want a single strategy to behave in completely different ways, making it basically impossible to appease everyone.

As we are seeing here, maintaining these strategies is a significant amount of work, and ultimately we want to get away from the library being the de facto source of strategies, and instead be the infrastructure that glues everything together.

And so I do want to ask again: for those having problems with a strategy provided by the library, can you try defining your own strategy that works for your use case?

@teameh
Copy link
Contributor

teameh commented Aug 29, 2024

Well there's is no specific strategy for WKWebView right? This is just using your default UI/NSView(Controller) strategy. Can we define our own strategies for webviews inside of view(controllers)?

As we are seeing here, maintaining these strategies is a significant amount of work

Yeah I completely understand. But I guess you probably included in the lib because it's quite common for apps to use some web content in their app and they want a snapshot to verify it.

I'm not sure when this strategy stopped work for iOS

Xcode 14 RC, and I haven't seen a successful iOS webview snapshot since any more, and the test in this repo is also failing. I was surprised by @PriyankaAngotra stating that it did for them.

So I think somebody should indeed dive into this and try to find out why things aren't working any more on iOS and at least proof by updating this 5y old screenshot that some webview strategy can still work on iOS.

But regardless of the strategy not working any more. I think you can apply this patch, because the crash in the test is not occurring any more as far as I can tell.

@PriyankaAngotra
Copy link
Author

@mbrandonw Happy to merge based on above comments?

@PriyankaAngotra
Copy link
Author

@mbrandonw Not sure you've seen my nudge above, but this will unblock us from updating to newer versions since 1.15. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants