playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature] add expect(locator).isInViewport() / isIntersectingViewport

Open cardinalX opened this issue 4 years ago • 14 comments

Now we have to use this method, the implementation of which is taken from puppeteer. It would be more convenient if it was in the playwright itself

  /**
   * @returns {!Promise<boolean>}
   * @summary Проверка, что нужный элемент находится в текущей области видимости(viewPort)
   */
  isIntersectingViewport(selector: string): Promise<boolean> {
    return this.page.$eval(selector, async element => {
      const visibleRatio: number = await new Promise(resolve => {
        const observer = new IntersectionObserver(entries => {
          resolve(entries[0].intersectionRatio);
          observer.disconnect();
        });
        observer.observe(element);
        // Firefox doesn't call IntersectionObserver callback unless
        // there are rafs.
        requestAnimationFrame(() => {});
      });
      return visibleRatio > 0;
    });
  }

Context: https://playwright.slack.com/archives/CSUHZPVLM/p1631008178339400

cardinalX avatar Sep 07 '21 10:09 cardinalX

What's your use case for the method? (Helps us prioritize)

pavelfeldman avatar Sep 07 '21 15:09 pavelfeldman

What's your use case for the method? (Helps us prioritize)

We have some card elements, on clicking on which another subcards appears, and this appearance should occur in the user's field of view(there may be bugs that the subcards appeared below and the user will not even notice that something appeared)

cardinalX avatar Sep 09 '21 13:09 cardinalX

I can, in principle, use the above method, which you actually suggested, thank you:) Therefore, I would not give this task a high priority if there are no new applicants

cardinalX avatar Sep 09 '21 13:09 cardinalX

I also would like this feature as we have an anchor tag and on mobile we want to check that after the click the page scrolls down to the anchor correctly.

federico-moretti avatar Mar 17 '22 13:03 federico-moretti

We have long list of vertical pages of a report. When we select a particular page ( e.g. 10) we want to verify if the scroll has been performed and the page is now in the viewport.

NikkTod avatar Mar 19 '22 19:03 NikkTod

Hi @pavelfeldman @dgozman @mxschmitt ,

Use case from our app (greetings from MSFT as well, well technically LinkedIn 😄 ) Our page is loaded to a specific section of the page (essentially your pretty scrolled down) User clicks the breadcrumb, and it scrolls them up to the first container of their page

I would like to assert the top container is in the viewport

https://user-images.githubusercontent.com/6743796/160018972-d69db8f3-4d31-4da0-8229-9d0c72d0a114.mp4

cliffordfajardo avatar Mar 24 '22 22:03 cliffordfajardo

My use-case from #13131: I have a sidebar in my app which, when collapsed, animates out of the viewport using transform: translateX(-100%) (see more in the linked issue). I want to write a test which asserts that when I press the collapse button, the sidebar animates out of the viewport and becomes "hidden".

martindzejky avatar Mar 29 '22 06:03 martindzejky

+1 on the use case of anchor tags that scroll to different sections of the page

arturozv avatar Jun 09 '22 19:06 arturozv

+1 need this

hkfsc avatar Jun 27 '22 23:06 hkfsc

What's your use case for the method? (Helps us prioritize)

Why not to add it directly into Playwright itself, when the method already exist? :D I don't think it would require a lot of work just to make this method available to everyone :D Or would it be? :D

PeterHevesi avatar Jun 28 '22 12:06 PeterHevesi

+1 need this as well

edgonzales avatar Jun 28 '22 15:06 edgonzales

Does anybody know, how can I create this method, to check if an element is in viewport in C#? Because this method is in Javascript (which I hate) and I am not really able to port it into C# :D Thanks for any help, I will greatly appreciate :)

PeterHevesi avatar Jun 29 '22 14:06 PeterHevesi

+1 In my case this assertion will be very useful. I don't have any possibility how to check if element is fully visible at this moment.

overlapping

topolanekmartin avatar Jul 26 '22 09:07 topolanekmartin

  • 1 I need this also

yurii-sav avatar Aug 04 '22 09:08 yurii-sav

+1 for testing if a navigation element scrolls to the correct section

edgeboy47 avatar Oct 25 '22 15:10 edgeboy47

I need this to determine if the radio button is on negative coordinates and the corresponding label must be clicked or not.

tokyo-watcher avatar Nov 02 '22 05:11 tokyo-watcher

+1 for this. We recently had a regression where scrolling stopped working in our side panel which opens to show detailed information about a row clicked in a table.

I would want to:

  1. Assert that the bottom-most element in the side panel is not in the viewport initially (there is always a delete button at the bottom of the side panel to delete the selected item from the table)
  2. Scroll the delete button element into view
  3. Assert that the delete button element is now in the viewport

There are various other parts of our UI that would benefit from this kind of regression testing including to check that long tables scroll correctly, navigation menu scrolls if the viewport is shorter than the menu height, etc. It seems this is a feature in Puppeteer and it has many potential benefits.

sarah-gelt avatar Nov 03 '22 14:11 sarah-gelt