英文:
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()
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论