playwright/tests/page/wheel.spec.ts

193 lines
5.8 KiB
TypeScript
Raw Normal View History

2021-09-14 22:22:52 +03:00
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2021-10-11 17:52:17 +03:00
import type { Page } from 'playwright-core';
2021-09-14 22:22:52 +03:00
import { test as it, expect } from './pageTest';
it.skip(({ isAndroid }) => {
return isAndroid;
2021-09-14 22:22:52 +03:00
});
let ignoreDelta = false;
it.beforeAll(async ({ browserMajorVersion, browserName, isElectron, platform }) => {
if (((browserName === 'chromium' && browserMajorVersion >= 102) || isElectron) && platform === 'darwin') {
// Chromium reports deltaX/deltaY scaled by host device scale factor.
// https://bugs.chromium.org/p/chromium/issues/detail?id=1324819
// https://github.com/microsoft/playwright/issues/7362
// Different bots have different scale factors (usually 1 or 2), so we just ignore the values
// instead of guessing the host scale factor.
ignoreDelta = true;
}
});
async function expectEvent(page: Page, expected: any) {
let received: any;
await expect.poll(async () => {
received = await page.evaluate('window.lastEvent') as any;
return received;
}).toBeTruthy();
if (ignoreDelta) {
delete received.deltaX;
delete received.deltaY;
delete expected.deltaX;
delete expected.deltaY;
}
expect(received).toEqual(expected);
}
it('should dispatch wheel events @smoke', async ({ page, server }) => {
2021-09-14 22:22:52 +03:00
await page.setContent(`<div style="width: 5000px; height: 5000px;"></div>`);
await page.mouse.move(50, 60);
await listenForWheelEvents(page, 'div');
await page.mouse.wheel(0, 100);
2021-11-02 23:56:12 +03:00
await page.waitForFunction('window.scrollY === 100');
await expectEvent(page, {
deltaX: 0,
deltaY: 100,
2021-09-14 22:22:52 +03:00
clientX: 50,
clientY: 60,
deltaMode: 0,
ctrlKey: false,
shiftKey: false,
altKey: false,
metaKey: false,
});
});
it('should dispatch wheel event on svg element', async ({ page, browserName, headless, isLinux }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/15566' });
await page.setContent(`
<body>
<svg class="scroll-box"></svg>
</body>
<style>
.scroll-box {
position: absolute;
top: 0px;
left: 0px;
background-color: brown;
width: 200px;
height: 200px;
}
</style>`);
await listenForWheelEvents(page, 'svg');
await page.mouse.move(100, 100);
await page.mouse.wheel(0, 100);
await page.waitForFunction('!!window.lastEvent');
await expectEvent(page, {
deltaX: 0,
deltaY: 100,
clientX: 100,
clientY: 100,
deltaMode: 0,
ctrlKey: false,
shiftKey: false,
altKey: false,
metaKey: false,
});
});
it('should scroll when nobody is listening', async ({ page, server }) => {
2021-09-14 22:22:52 +03:00
await page.goto(server.PREFIX + '/input/scrollable.html');
await page.mouse.move(50, 60);
await page.mouse.wheel(0, 100);
await page.waitForFunction('window.scrollY === 100');
});
it('should set the modifiers', async ({ page }) => {
2021-09-14 22:22:52 +03:00
await page.setContent(`<div style="width: 5000px; height: 5000px;"></div>`);
await page.mouse.move(50, 60);
await listenForWheelEvents(page, 'div');
await page.keyboard.down('Shift');
await page.mouse.wheel(0, 100);
await expectEvent(page, {
deltaX: 0,
deltaY: 100,
2021-09-14 22:22:52 +03:00
clientX: 50,
clientY: 60,
deltaMode: 0,
ctrlKey: false,
shiftKey: true,
altKey: false,
metaKey: false,
});
});
it('should scroll horizontally', async ({ page }) => {
2021-09-14 22:22:52 +03:00
await page.setContent(`<div style="width: 5000px; height: 5000px;"></div>`);
await page.mouse.move(50, 60);
await listenForWheelEvents(page, 'div');
await page.mouse.wheel(100, 0);
await expectEvent(page, {
deltaX: 100,
deltaY: 0,
2021-09-14 22:22:52 +03:00
clientX: 50,
clientY: 60,
deltaMode: 0,
ctrlKey: false,
shiftKey: false,
altKey: false,
metaKey: false,
});
await page.waitForFunction('window.scrollX === 100');
});
it('should work when the event is canceled', async ({ page }) => {
2021-09-14 22:22:52 +03:00
await page.setContent(`<div style="width: 5000px; height: 5000px;"></div>`);
await page.mouse.move(50, 60);
await listenForWheelEvents(page, 'div');
await page.evaluate(() => {
document.querySelector('div').addEventListener('wheel', e => e.preventDefault());
});
// Give wheel listener a chance to propagate through all the layers in Firefox.
for (let i = 0; i < 10; i++)
await page.evaluate(() => new Promise(x => requestAnimationFrame(() => requestAnimationFrame(x))));
2021-09-14 22:22:52 +03:00
await page.mouse.wheel(0, 100);
await expectEvent(page, {
deltaX: 0,
deltaY: 100,
2021-09-14 22:22:52 +03:00
clientX: 50,
clientY: 60,
deltaMode: 0,
ctrlKey: false,
shiftKey: false,
altKey: false,
metaKey: false,
});
// Give the page a chance to scroll.
2022-07-27 23:27:46 +03:00
await page.waitForFunction(`!!window['lastEvent']`);
// Ensure that it did not.
2021-09-14 22:22:52 +03:00
expect(await page.evaluate('window.scrollY')).toBe(0);
});
async function listenForWheelEvents(page: Page, selector: string) {
await page.evaluate(selector => {
document.querySelector(selector).addEventListener('wheel', (e: WheelEvent) => {
window['lastEvent'] = {
deltaX: e.deltaX,
deltaY: e.deltaY,
clientX: e.clientX,
clientY: e.clientY,
deltaMode: e.deltaMode,
ctrlKey: e.ctrlKey,
shiftKey: e.shiftKey,
altKey: e.altKey,
metaKey: e.metaKey,
};
}, { passive: false });
}, selector);
}