2018-11-21 03:43:07 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-06-23 05:18:46 +00:00
|
|
|
import os from 'os';
|
|
|
|
import expect from 'expect';
|
|
|
|
import {
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
2020-06-23 05:18:46 +00:00
|
|
|
itFailsFirefox,
|
2020-07-13 09:22:26 +00:00
|
|
|
} from './mocha-utils'; // eslint-disable-line import/extensions
|
2020-07-14 15:57:29 +00:00
|
|
|
import { KeyInput } from '../lib/cjs/puppeteer/common/USKeyboardLayout.js';
|
2018-11-21 03:43:07 +00:00
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
interface Dimensions {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dimensions(): Dimensions {
|
2018-11-21 03:43:07 +00:00
|
|
|
const rect = document.querySelector('textarea').getBoundingClientRect();
|
|
|
|
return {
|
|
|
|
x: rect.left,
|
|
|
|
y: rect.top,
|
|
|
|
width: rect.width,
|
2020-05-07 10:54:55 +00:00
|
|
|
height: rect.height,
|
2018-11-21 03:43:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Mouse', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should click the document', async () => {
|
|
|
|
const { page } = getTestState();
|
2018-11-21 03:43:07 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.evaluate(() => {
|
2020-06-23 05:18:46 +00:00
|
|
|
globalThis.clickPromise = new Promise((resolve) => {
|
2020-05-07 10:54:55 +00:00
|
|
|
document.addEventListener('click', (event) => {
|
2020-04-09 05:56:25 +00:00
|
|
|
resolve({
|
|
|
|
type: event.type,
|
|
|
|
detail: event.detail,
|
|
|
|
clientX: event.clientX,
|
|
|
|
clientY: event.clientY,
|
|
|
|
isTrusted: event.isTrusted,
|
2020-05-07 10:54:55 +00:00
|
|
|
button: event.button,
|
2019-02-03 01:49:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-11-21 03:43:07 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.mouse.click(50, 60);
|
2020-07-10 10:52:13 +00:00
|
|
|
const event = await page.evaluate<() => MouseEvent>(
|
2020-06-23 05:18:46 +00:00
|
|
|
() => globalThis.clickPromise
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(event.type).toBe('click');
|
|
|
|
expect(event.detail).toBe(1);
|
|
|
|
expect(event.clientX).toBe(50);
|
|
|
|
expect(event.clientY).toBe(60);
|
|
|
|
expect(event.isTrusted).toBe(true);
|
|
|
|
expect(event.button).toBe(0);
|
|
|
|
});
|
2021-11-19 07:50:31 +00:00
|
|
|
it('should resize the textarea', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2020-07-10 10:52:13 +00:00
|
|
|
const { x, y, width, height } = await page.evaluate<() => Dimensions>(
|
|
|
|
dimensions
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
const mouse = page.mouse;
|
|
|
|
await mouse.move(x + width - 4, y + height - 4);
|
|
|
|
await mouse.down();
|
|
|
|
await mouse.move(x + width + 100, y + height + 100);
|
|
|
|
await mouse.up();
|
2020-07-10 10:52:13 +00:00
|
|
|
const newDimensions = await page.evaluate<() => Dimensions>(dimensions);
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(newDimensions.width).toBe(Math.round(width + 104));
|
|
|
|
expect(newDimensions.height).toBe(Math.round(height + 104));
|
|
|
|
});
|
2021-11-19 07:50:31 +00:00
|
|
|
it('should select the text with mouse', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
|
|
|
await page.focus('textarea');
|
2020-05-07 10:54:55 +00:00
|
|
|
const text =
|
|
|
|
"This is the text that we are going to try to select. Let's see how it goes.";
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.type(text);
|
|
|
|
// Firefox needs an extra frame here after typing or it will fail to set the scrollTop
|
|
|
|
await page.evaluate(() => new Promise(requestAnimationFrame));
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.evaluate(
|
|
|
|
() => (document.querySelector('textarea').scrollTop = 0)
|
|
|
|
);
|
|
|
|
const { x, y } = await page.evaluate(dimensions);
|
|
|
|
await page.mouse.move(x + 2, y + 2);
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.mouse.down();
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.mouse.move(100, 100);
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.mouse.up();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
const textarea = document.querySelector('textarea');
|
|
|
|
return textarea.value.substring(
|
|
|
|
textarea.selectionStart,
|
|
|
|
textarea.selectionEnd
|
|
|
|
);
|
|
|
|
})
|
|
|
|
).toBe(text);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should trigger hover state', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/scrollable.html');
|
|
|
|
await page.hover('#button-6');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => document.querySelector('button:hover').id)
|
|
|
|
).toBe('button-6');
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.hover('#button-2');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => document.querySelector('button:hover').id)
|
|
|
|
).toBe('button-2');
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.hover('#button-91');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => document.querySelector('button:hover').id)
|
|
|
|
).toBe('button-91');
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox(
|
|
|
|
'should trigger hover state with removed window.Node',
|
|
|
|
async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/scrollable.html');
|
|
|
|
await page.evaluate(() => delete window.Node);
|
|
|
|
await page.hover('#button-6');
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => document.querySelector('button:hover').id)
|
|
|
|
).toBe('button-6');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
it('should set modifier keys on click', async () => {
|
|
|
|
const { page, server, isFirefox } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/scrollable.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.evaluate(() =>
|
|
|
|
document
|
|
|
|
.querySelector('#button-3')
|
2020-06-23 05:18:46 +00:00
|
|
|
.addEventListener('mousedown', (e) => (globalThis.lastEvent = e), true)
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2020-06-23 05:18:46 +00:00
|
|
|
const modifiers = new Map<KeyInput, string>([
|
|
|
|
['Shift', 'shiftKey'],
|
|
|
|
['Control', 'ctrlKey'],
|
|
|
|
['Alt', 'altKey'],
|
|
|
|
['Meta', 'metaKey'],
|
|
|
|
]);
|
2020-04-09 05:56:25 +00:00
|
|
|
// In Firefox, the Meta modifier only exists on Mac
|
2020-05-07 10:54:55 +00:00
|
|
|
if (isFirefox && os.platform() !== 'darwin') delete modifiers['Meta'];
|
2020-06-23 05:18:46 +00:00
|
|
|
for (const [modifier, key] of modifiers) {
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.down(modifier);
|
2018-11-21 03:43:07 +00:00
|
|
|
await page.click('#button-3');
|
2020-07-10 10:52:13 +00:00
|
|
|
if (
|
|
|
|
!(await page.evaluate((mod: string) => globalThis.lastEvent[mod], key))
|
|
|
|
)
|
2020-06-23 05:18:46 +00:00
|
|
|
throw new Error(key + ' should be true');
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.up(modifier);
|
|
|
|
}
|
|
|
|
await page.click('#button-3');
|
2020-06-23 05:18:46 +00:00
|
|
|
for (const [modifier, key] of modifiers) {
|
2020-07-10 10:52:13 +00:00
|
|
|
if (await page.evaluate((mod: string) => globalThis.lastEvent[mod], key))
|
2020-04-09 05:56:25 +00:00
|
|
|
throw new Error(modifiers[modifier] + ' should be false');
|
|
|
|
}
|
|
|
|
});
|
2020-07-06 07:27:17 +00:00
|
|
|
itFailsFirefox('should send mouse wheel events', async () => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/wheel.html');
|
|
|
|
const elem = await page.$('div');
|
|
|
|
const boundingBoxBefore = await elem.boundingBox();
|
|
|
|
expect(boundingBoxBefore).toMatchObject({
|
|
|
|
width: 115,
|
|
|
|
height: 115,
|
|
|
|
});
|
|
|
|
|
|
|
|
await page.mouse.move(
|
|
|
|
boundingBoxBefore.x + boundingBoxBefore.width / 2,
|
|
|
|
boundingBoxBefore.y + boundingBoxBefore.height / 2
|
|
|
|
);
|
|
|
|
|
|
|
|
await page.mouse.wheel({ deltaY: -100 });
|
|
|
|
const boundingBoxAfter = await elem.boundingBox();
|
|
|
|
expect(boundingBoxAfter).toMatchObject({
|
|
|
|
width: 230,
|
|
|
|
height: 230,
|
|
|
|
});
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should tween mouse movement', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.mouse.move(100, 100);
|
|
|
|
await page.evaluate(() => {
|
2020-06-23 05:18:46 +00:00
|
|
|
globalThis.result = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
document.addEventListener('mousemove', (event) => {
|
2020-06-23 05:18:46 +00:00
|
|
|
globalThis.result.push([event.clientX, event.clientY]);
|
2018-11-21 03:43:07 +00:00
|
|
|
});
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.mouse.move(200, 300, { steps: 5 });
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(await page.evaluate('result')).toEqual([
|
|
|
|
[120, 140],
|
|
|
|
[140, 180],
|
|
|
|
[160, 220],
|
|
|
|
[180, 260],
|
2020-05-07 10:54:55 +00:00
|
|
|
[200, 300],
|
2020-04-09 05:56:25 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
// @see https://crbug.com/929806
|
2021-11-19 07:50:31 +00:00
|
|
|
it('should work with mobile viewports and cross process navigations', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2021-11-19 07:50:31 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setViewport({ width: 360, height: 640, isMobile: true });
|
|
|
|
await page.goto(server.CROSS_PROCESS_PREFIX + '/mobile.html');
|
|
|
|
await page.evaluate(() => {
|
|
|
|
document.addEventListener('click', (event) => {
|
|
|
|
globalThis.result = { x: event.clientX, y: event.clientY };
|
2019-02-11 22:00:06 +00:00
|
|
|
});
|
2021-11-19 07:50:31 +00:00
|
|
|
});
|
2019-02-11 22:00:06 +00:00
|
|
|
|
2021-11-19 07:50:31 +00:00
|
|
|
await page.mouse.click(30, 40);
|
2019-02-11 22:00:06 +00:00
|
|
|
|
2021-11-19 07:50:31 +00:00
|
|
|
expect(await page.evaluate('result')).toEqual({ x: 30, y: 40 });
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|