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 in_view function #3885

Merged
merged 1 commit into from
Feb 13, 2022
Merged

fix in_view function #3885

merged 1 commit into from
Feb 13, 2022

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Feb 13, 2022

@PH4NTOMiki's #3873 (comment) reminded me of #3741 (comment). The in_view function now isn't correct and reports true if the element is anywhere on the page, not stricly in the viewport as expected.

With this PR, in_view only returns true if the element is actually in the viewport.

Notes

According to playwright's docs:

This method returns the bounding box of the element, or null if the element is not visible

Playwright's term for "is not visible" is confusing:

Element is considered visible when it has non-empty bounding box and does not have visibility:hidden computed style. Note that elements of zero size or with display:none are not considered visible.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Feb 13, 2022

⚠️ No Changeset found

Latest commit: 2f75426

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@netlify
Copy link

netlify bot commented Feb 13, 2022

✔️ Deploy Preview for kit-demo canceled.

🔨 Explore the source changes: 2f75426

🔍 Inspect the deploy log: https://app.netlify.com/sites/kit-demo/deploys/6208b699a1a38e00087b2e6a

@benmccann benmccann merged commit 6ca5c2a into master Feb 13, 2022
@benmccann benmccann deleted the fix-in-view branch February 13, 2022 16:48
@PH4NTOMiki
Copy link
Contributor

@bluwy @benmccann the tests using in_view are being flaky again.

@bluwy
Copy link
Member Author

bluwy commented Feb 14, 2022

Yeah, I don't think the tests were de-flaked in the first place.

@PH4NTOMiki
Copy link
Contributor

I'm trying this now https://stackoverflow.com/a/68848306

@PH4NTOMiki
Copy link
Contributor

@bluwy what do you think of that, it seems to pass consistently.

@PH4NTOMiki
Copy link
Contributor

@bluwy what do you think of that, it seems to pass consistently.

Everywhere except Windows

@PH4NTOMiki
Copy link
Contributor

@PH4NTOMiki
Copy link
Contributor

@bluwy @benmccann This works consistently even on Windows, it's the old deprecated way, but it works:

in_view: async ({ page }, use) => {
		/** @param {string} selector */
		async function in_view(selector) {
			return await page.$eval(selector, async (/** @type {Element} */ element) => {
				// @ts-ignore
				let top = element.offsetTop;
				// @ts-ignore
				let left = element.offsetLeft;
				// @ts-ignore
				const width = element.offsetWidth;
				// @ts-ignore
				const height = element.offsetHeight;

				// @ts-ignore
				while (element.offsetParent) {
					// @ts-ignore
					element = element.offsetParent;
					// @ts-ignore
					top += element.offsetTop;
					// @ts-ignore
					left += element.offsetLeft;
				}

				return top < window.pageYOffset + window.innerHeight &&
					left < window.pageXOffset + window.innerWidth &&
					top + height > window.pageYOffset &&
					left + width > window.pageXOffset
					? true
					: { top, left, width, height };
			});
		}

		use(in_view);
	}

Maybe you or I can do a PR for it and merge it.

@bluwy
Copy link
Member Author

bluwy commented Feb 14, 2022

Thanks for digging into this @PH4NTOMiki. My first encounter with the scrolling shenanigans was also through that stackoverflow post, and the function had evolved over time. I'm not sure if using ResizeObservers would help though if it would still sometimes fail in windows.

Re the old deprecated way, do you know why that works more reliably? I'm not 100% sure if the function is safe too.

Also maybe related, perhaps we can setup the CI to emit the screenshots for failed tests. That might show us if the element is actually inside the view, or it's an error with in_view incorrectly detecting stuff.

@benmccann
Copy link
Member

It's already outputting screenshots and failure traces on the CI:

screenshot: 'only-on-failure',

I'm not sure how to retrieve them though. I guess we'd have to setup something to upload them somewhere?

@benmccann
Copy link
Member

There were a couple tests I couldn't get to fail on my machine. A user in Discord provided me with these traces awhile back to help us debug. Unfortunately, I haven't gotten to investigate yet. But maybe someone else will be able to 😁

playwright-test-results.zip

@bluwy
Copy link
Member Author

bluwy commented Feb 14, 2022

I'm not sure how to retrieve them though. I guess we'd have to setup something to upload them somewhere?

I've done something like this before using upload-artifact. Will send a PR.

@bluwy bluwy mentioned this pull request Feb 14, 2022
5 tasks
@PH4NTOMiki
Copy link
Contributor

Re the old deprecated way, do you know why that works more reliably? I'm not 100% sure if the function is safe too.

Well it's more reliable than current ones, but still not 100% reliable, but it's the best we currently have.

@PH4NTOMiki
Copy link
Contributor

@bluwy I think it works better because it doesn't use boundingClientRect(boundingBox in playwright is similar). It uses element static properties.

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.

3 participants