playwright/tests/emulation-focus.spec.ts

197 строки
6.7 KiB
TypeScript
Исходник Обычный вид История

2020-08-03 23:41:48 +03:00
/**
* Copyright 2018 Google Inc. All rights reserved.
* Modifications 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.
*/
2020-08-19 09:00:45 +03:00
import { test as it, expect } from './config/pageTest';
import { browserTest } from './config/browserTest';
import { attachFrame } from './config/utils';
2020-08-03 23:41:48 +03:00
it('should think that it is focused by default', async ({page}) => {
2020-08-03 23:41:48 +03:00
expect(await page.evaluate('document.hasFocus()')).toBe(true);
});
it('should think that all pages are focused', async ({page, isElectron}) => {
it.fixme(isElectron, 'BrowserContext.newPage does not work in Electron');
2020-08-03 23:41:48 +03:00
const page2 = await page.context().newPage();
expect(await page.evaluate('document.hasFocus()')).toBe(true);
expect(await page2.evaluate('document.hasFocus()')).toBe(true);
await page2.close();
});
it('should focus popups by default', async ({page, server}) => {
2020-08-03 23:41:48 +03:00
await page.goto(server.EMPTY_PAGE);
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.evaluate(url => { window.open(url); }, server.EMPTY_PAGE),
]);
expect(await popup.evaluate('document.hasFocus()')).toBe(true);
expect(await page.evaluate('document.hasFocus()')).toBe(true);
});
it('should provide target for keyboard events', async ({page, server, isElectron}) => {
it.fixme(isElectron, 'BrowserContext.newPage does not work in Electron');
2020-08-03 23:41:48 +03:00
const page2 = await page.context().newPage();
await Promise.all([
page.goto(server.PREFIX + '/input/textarea.html'),
page2.goto(server.PREFIX + '/input/textarea.html'),
]);
await Promise.all([
page.focus('input'),
page2.focus('input'),
]);
const text = 'first';
const text2 = 'second';
await Promise.all([
page.keyboard.type(text),
page2.keyboard.type(text2),
]);
const results = await Promise.all([
page.evaluate('result'),
page2.evaluate('result'),
]);
expect(results).toEqual([text, text2]);
});
it('should not affect mouse event target page', async ({page, server, isElectron}) => {
it.fixme(isElectron, 'BrowserContext.newPage does not work in Electron');
2020-08-03 23:41:48 +03:00
const page2 = await page.context().newPage();
function clickCounter() {
document.onclick = () => window['clickCount'] = (window['clickCount'] || 0) + 1;
2020-08-03 23:41:48 +03:00
}
await Promise.all([
page.evaluate(clickCounter),
page2.evaluate(clickCounter),
page.focus('body'),
page2.focus('body'),
]);
await Promise.all([
page.mouse.click(1, 1),
page2.mouse.click(1, 1),
]);
const counters = await Promise.all([
page.evaluate('window.clickCount'),
page2.evaluate('window.clickCount'),
]);
expect(counters).toEqual([1,1]);
2020-08-03 23:41:48 +03:00
});
it('should change document.activeElement', async ({page, server, isElectron}) => {
it.fixme(isElectron, 'BrowserContext.newPage does not work in Electron');
2020-08-03 23:41:48 +03:00
const page2 = await page.context().newPage();
await Promise.all([
page.goto(server.PREFIX + '/input/textarea.html'),
page2.goto(server.PREFIX + '/input/textarea.html'),
]);
await Promise.all([
page.focus('input'),
page2.focus('textarea'),
]);
const active = await Promise.all([
page.evaluate('document.activeElement.tagName'),
page2.evaluate('document.activeElement.tagName'),
]);
expect(active).toEqual(['INPUT', 'TEXTAREA']);
});
it('should not affect screenshots', async ({page, server, browserName, headful, isElectron, isAndroid}) => {
it.skip(browserName === 'firefox' && headful);
it.skip(isAndroid);
it.fixme(isElectron, 'BrowserContext.newPage does not work in Electron');
2020-08-03 23:41:48 +03:00
// Firefox headful produces a different image.
const page2 = await page.context().newPage();
await Promise.all([
page.setViewportSize({width: 500, height: 500}),
page.goto(server.PREFIX + '/grid.html'),
page2.setViewportSize({width: 50, height: 50}),
page2.goto(server.PREFIX + '/grid.html'),
]);
await Promise.all([
page.focus('body'),
page2.focus('body'),
]);
const screenshots = await Promise.all([
page.screenshot(),
page2.screenshot(),
]);
2020-10-07 01:51:18 +03:00
expect(screenshots[0]).toMatchSnapshot('screenshot-sanity.png');
expect(screenshots[1]).toMatchSnapshot('grid-cell-0.png');
2020-08-03 23:41:48 +03:00
});
it('should change focused iframe', async ({page, server}) => {
2020-08-03 23:41:48 +03:00
await page.goto(server.EMPTY_PAGE);
const [frame1, frame2] = await Promise.all([
attachFrame(page, 'frame1', server.PREFIX + '/input/textarea.html'),
attachFrame(page, 'frame2', server.PREFIX + '/input/textarea.html'),
2020-08-03 23:41:48 +03:00
]);
function logger() {
self['_events'] = [];
2020-08-03 23:41:48 +03:00
const element = document.querySelector('input');
element.onfocus = element.onblur = e => self['_events'].push(e.type);
2020-08-03 23:41:48 +03:00
}
await Promise.all([
frame1.evaluate(logger),
frame2.evaluate(logger),
]);
const focused = await Promise.all([
frame1.evaluate('document.hasFocus()'),
frame2.evaluate('document.hasFocus()'),
]);
expect(focused).toEqual([false, false]);
{
await frame1.focus('input');
const events = await Promise.all([
frame1.evaluate('self._events'),
frame2.evaluate('self._events'),
]);
expect(events).toEqual([['focus'], []]);
const focused = await Promise.all([
frame1.evaluate('document.hasFocus()'),
frame2.evaluate('document.hasFocus()'),
]);
expect(focused).toEqual([true, false]);
}
{
await frame2.focus('input');
const events = await Promise.all([
frame1.evaluate('self._events'),
frame2.evaluate('self._events'),
]);
expect(events).toEqual([['focus', 'blur'], ['focus']]);
const focused = await Promise.all([
frame1.evaluate('document.hasFocus()'),
frame2.evaluate('document.hasFocus()'),
]);
expect(focused).toEqual([false, true]);
}
});
// @see https://github.com/microsoft/playwright/issues/3476
browserTest('should focus with more than one page/context', async ({contextFactory}) => {
const page1 = await (await contextFactory()).newPage();
const page2 = await (await contextFactory()).newPage();
await page1.setContent(`<button id="foo" onfocus="window.gotFocus=true">foo</button>`);
await page2.setContent(`<button id="foo" onfocus="window.gotFocus=true">foo</button>`);
await page1.focus('#foo');
await page2.focus('#foo');
expect(await page1.evaluate(() => !!window['gotFocus'])).toBe(true);
expect(await page2.evaluate(() => !!window['gotFocus'])).toBe(true);
});