-
Notifications
You must be signed in to change notification settings - Fork 4
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
Fixed scrolling an element into view only if it is not within a view. #15
base: main
Are you sure you want to change the base?
Fixed scrolling an element into view only if it is not within a view. #15
Conversation
Thanks @AlexSkrypnyk for filing the PR. Can you tell me the situation where this is necessary. I borrowed the JS and approach from https://github.com/minkphp/webdriver-classic-driver/blob/9567c6e77c2b89dcdc5a5f567ce826c1a831de1f/src/WebdriverClassicDriver.php#L981 and https://github.com/php-webdriver/php-webdriver/blob/11923b4ba312fc9d137266ae67438be65d4d500f/lib/Remote/RemoteWebElement.php#L250 |
When there is a bottom fixed bar and the test already has scrolling to the element to offset that (via a custom code), calling a click will reset this adjusted position instead of respecting it. |
@AlexSkrypnyk thanks for the reply. I have further questions; and then what happens? Does the test fail for some reason? Is there a reason the test is so dependent on the position of the page? |
A manual step is added to scroll the viewport to the amount of the height of the bottom bar so that the element that should receive a click would be vertically above the bottom bar and would now receive a click. With a change introduced in #10, the offset that a manual step sets, being reset because the code just performs a scroll to the element without assessing if such a scroll is needed at all (it is not needed if the element is already within a viewport). This PR corrects the code in #10 to consider if the current element is currently within a viewport and only scrolls to it if it is not. |
@AlexSkrypnyk thanks for all the info. For me this means that we should change the scrollIntoView to make sure it scrolls till the element is visible. Then you wouldn't need your workaround and the system would be more robust. This is what Cypress does for example - see https://docs.cypress.io/guides/core-concepts/interacting-with-elements#Scrolling |
Determining if the element is visible is not the same as determining if the element will receive a click. Also, the page may have more than one fixed bar and it can be vertical as well. So allowing a test to control the scroll outside of the click step gives more control to the developer, easier to implement and goes inline with the paradigm than a test should replicate what a user would do (like scrolling to the element instead of auto-scrolling). I think my solution to only scroll to an element if it is not within a viewport is simple enough to handle majority of the cases without the need to update existing tests. Alternatively, you could move the auto-scroll added in #10 under a feature flag and let a developer decide if they want to use it. |
Creating heuristics to determine if an element is clickable will be extremely hard because you would need to recursively consider every element on the page that may or may not be in the way of a click, which means that you would need to calculate a visibility of every element, which in own turn is not that easy once you start considering what “visible” means. Relying solely on this algorithm without allowing to override it will still not allow to let a developer control the scroll when they need to. |
@AlexSkrypnyk okay I'm convinced. This is very similar to https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoViewIfNeeded I would be great to add some text coverage of this behaviour if you can. I think all we need is a test confirming that clicking on an element does not scroll the page if it is already in window. |
FWIW I noticed that this can have side effects when there are overlays (like the drupal admin toolbar). What about using |
Fixes an issue introduced in #10: the scrolling an element into the view should only take place if the element is not already within the view.