如何验证页面已向上滚动以及如何验证文本在 Playwright 屏幕上可见?

huangapple go评论114阅读模式
英文:

How verify that page is scrolled up and how verify text is visible on screen in playwright?

问题

需要一个断言来检查页面是否已滚动到顶部,以及一个断言来检查屏幕上是否可见一个元素。如何在Playwright和TypeScript中实现?

const isScrolledUp = await page.evaluate(() => {
    return window.pageYOffset === 0;
});
expect(isScrolledUp).toBe(true);

const visibleScreen = page.getByRole('heading', { name: 'Some Name' });
await expect(visibleScreen).toBeFocused();
英文:

I need an assertion to check if a page has scrolled to the top and an assertion to check if an element is visible on screen. How to do it in playwright with TypeScript?

const isScrolledUp = await page.evaluate(() => {
        return window.pageYOffset === 0;
      });      
      expect(isScrolledUp).toBe(true);

const visibleScreen = page.getByRole('heading', { name: 'Some Name' });
await expect(visibleScreen).toBeFocused();

答案1

得分: 0

import { test, expect } from '@playwright/test';

test('测试 @00', async ({page}) => {
    
    // 使用鼠标滚轮向上滚动到页面顶部
    await page.mouse.wheel(0, -100000000)
    const isScrolledUp = await page.evaluate(() => {
        return window.scrollY === 0;
    });
    expect(isScrolledUp).toBe(true);

    // toBeInViewport 只检查当前视图
    await expect(page.getByRole('heading', {name: 'Some name'})).toBeInViewport()
})
英文:

You could scroll with the mouse to a very low "sure-to-scroll-to-the-top" y-value and then assure with toBeInViewport()

import { test, expect } from '@playwright/test';

test('Test @00', async ({page}) => {
    
    // Scrolls to the top of the page
    await page.mouse.wheel(0, -100000000)
    const isScrolledUp = await page.evaluate(() => {
        return window.scrollY === 0;
    });
    expect(isScrolledUp).toBe(true);

    // toBeInViewport checks only the current view
    await expect(page.getByRole('heading', {name: 'Some name'})).toBeInViewport()
})

huangapple
  • 本文由 发表于 2023年6月12日 02:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定