/**
* 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.
*/
import { test as it, expect } from './pageTest';
import { attachFrame, detachFrame } from '../config/utils';
async function giveItAChanceToClick(page) {
for (let i = 0; i < 5; i++)
await page.evaluate(() => new Promise(f => requestAnimationFrame(() => requestAnimationFrame(f))));
}
it('should click the button @smoke', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.click('button');
expect(await page.evaluate('result')).toBe('Clicked');
});
it('should click button inside frameset', async ({ page, server }) => {
await page.goto(server.PREFIX + '/frames/frameset.html');
const frameElement = await page.$('frame');
await frameElement.evaluate((frame: HTMLFrameElement) => frame.src = '/input/button.html');
const frame = await frameElement.contentFrame();
await frame.click('button');
expect(await frame.evaluate('result')).toBe('Clicked');
});
it('should click a button that closes popup', async ({ page }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/20093' });
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.evaluate(() => window.open('')),
]);
await popup.setContent(``);
await popup.$eval('button', body => body.addEventListener('click', () => {
window.close();
}));
await Promise.all([
popup.locator('button').click(), // throws in Firefox, but not in Chromium or WebKit
popup.waitForEvent('close'),
]);
});
it('should issue clicks in parallel in page and popup', async ({ page, server }) => {
await page.goto(server.PREFIX + '/counter.html');
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.evaluate(() => window.open('/counter.html')),
]);
const clickPromises = [];
for (let i = 0; i < 21; ++i) {
if (i % 3 === 0)
clickPromises.push(popup.locator('button').click());
else
clickPromises.push(page.locator('button').click());
}
await Promise.all(clickPromises);
expect(await page.evaluate(() => window['count'])).toBe(14);
expect(await popup.evaluate(() => window['count'])).toBe(7);
});
it('should click svg', async ({ page }) => {
await page.setContent(`
`);
await page.click('circle');
expect(await page.evaluate('__CLICKED')).toBe(42);
});
it('should click the button if window.Node is removed', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.evaluate(() => delete window.Node);
await page.click('button');
expect(await page.evaluate('result')).toBe('Clicked');
});
// @see https://github.com/GoogleChrome/puppeteer/issues/4281
it('should click on a span with an inline element inside', async ({ page }) => {
await page.setContent(`
`);
await page.click('span');
expect(await page.evaluate('CLICKED')).toBe(42);
});
it('should not throw UnhandledPromiseRejection when page closes', async ({ page, isWebView2 }) => {
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
await Promise.all([
page.close(),
page.mouse.click(1, 2),
]).catch(e => {});
});
it('should click the 1x1 div', async ({ page }) => {
await page.setContent(`
`);
await page.click('div');
expect(await page.evaluate('window.__clicked')).toBe(true);
});
it('should click the button after navigation ', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.click('button');
await page.goto(server.PREFIX + '/input/button.html');
await page.click('button');
expect(await page.evaluate('result')).toBe('Clicked');
});
it('should click the button after a cross origin navigation ', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.click('button');
await page.goto(server.CROSS_PROCESS_PREFIX + '/input/button.html');
await page.click('button');
expect(await page.evaluate('result')).toBe('Clicked');
});
it('should click when one of inline box children is outside of viewport', async ({ page }) => {
await page.setContent(`
woofdoggo
`);
await page.click('span');
expect(await page.evaluate('CLICKED')).toBe(42);
});
it('should select the text by triple clicking', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/textarea.html');
const text = 'This is the text that we are going to try to select. Let\'s see how it goes.';
await page.fill('textarea', text);
await page.click('textarea', { clickCount: 3 });
expect(await page.evaluate(() => {
const textarea = document.querySelector('textarea');
return textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
})).toBe(text);
});
it('should click offscreen buttons', async ({ page, server }) => {
await page.goto(server.PREFIX + '/offscreenbuttons.html');
const messages = [];
page.on('console', msg => messages.push(msg.text()));
for (let i = 0; i < 11; ++i) {
// We might've scrolled to click a button - reset to (0, 0).
await page.evaluate(() => window.scrollTo(0, 0));
await page.click(`#btn${i}`);
}
expect(messages).toEqual([
'button #0 clicked',
'button #1 clicked',
'button #2 clicked',
'button #3 clicked',
'button #4 clicked',
'button #5 clicked',
'button #6 clicked',
'button #7 clicked',
'button #8 clicked',
'button #9 clicked',
'button #10 clicked'
]);
});
it('should waitFor visible when already visible', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.click('button');
expect(await page.evaluate('result')).toBe('Clicked');
});
it('should not wait with force', async ({ page, server }) => {
let error = null;
await page.goto(server.PREFIX + '/input/button.html');
await page.$eval('button', b => b.style.display = 'none');
await page.click('button', { force: true }).catch(e => error = e);
expect(error.message).toContain('Element is not visible');
expect(await page.evaluate('result')).toBe('Was not clicked');
});
it('should waitFor display:none to be gone', async ({ page, server }) => {
let done = false;
await page.goto(server.PREFIX + '/input/button.html');
await page.$eval('button', b => b.style.display = 'none');
const clicked = page.click('button', { timeout: 0 }).then(() => done = true);
await giveItAChanceToClick(page);
expect(await page.evaluate('result')).toBe('Was not clicked');
expect(done).toBe(false);
await page.$eval('button', b => b.style.display = 'block');
await clicked;
expect(done).toBe(true);
expect(await page.evaluate('result')).toBe('Clicked');
});
it('should waitFor visibility:hidden to be gone', async ({ page, server }) => {
let done = false;
await page.goto(server.PREFIX + '/input/button.html');
await page.$eval('button', b => b.style.visibility = 'hidden');
const clicked = page.click('button', { timeout: 0 }).then(() => done = true);
await giveItAChanceToClick(page);
expect(await page.evaluate('result')).toBe('Was not clicked');
expect(done).toBe(false);
await page.$eval('button', b => b.style.visibility = 'visible');
await clicked;
expect(done).toBe(true);
expect(await page.evaluate('result')).toBe('Clicked');
});
it('should waitFor visible when parent is hidden', async ({ page, server }) => {
let done = false;
await page.goto(server.PREFIX + '/input/button.html');
await page.$eval('button', b => b.parentElement.style.display = 'none');
const clicked = page.click('button', { timeout: 0 }).then(() => done = true);
await giveItAChanceToClick(page);
expect(done).toBe(false);
await page.$eval('button', b => b.parentElement.style.display = 'block');
await clicked;
expect(done).toBe(true);
expect(await page.evaluate('result')).toBe('Clicked');
});
it('should click wrapped links', async ({ page, server }) => {
await page.goto(server.PREFIX + '/wrappedlink.html');
await page.click('a');
expect(await page.evaluate('__clicked')).toBe(true);
});
it('should click on checkbox input and toggle', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/checkbox.html');
expect(await page.evaluate(() => window['result'].check)).toBe(null);
await page.click('input#agree');
expect(await page.evaluate(() => window['result'].check)).toBe(true);
expect(await page.evaluate(() => window['result'].events)).toEqual([
'mouseover',
'mouseenter',
'mousemove',
'mousedown',
'mouseup',
'click',
'input',
'change',
]);
await page.click('input#agree');
expect(await page.evaluate(() => window['result'].check)).toBe(false);
});
it('should click on checkbox label and toggle', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/checkbox.html');
expect(await page.evaluate(() => window['result'].check)).toBe(null);
await page.click('label[for="agree"]');
expect(await page.evaluate(() => window['result'].check)).toBe(true);
expect(await page.evaluate(() => window['result'].events)).toEqual([
'click',
'input',
'change',
]);
await page.click('label[for="agree"]');
expect(await page.evaluate(() => window['result'].check)).toBe(false);
});
it('should scroll and click the button', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/scrollable.html');
await page.click('#button-5');
expect(await page.evaluate(() => document.querySelector('#button-5').textContent)).toBe('clicked');
await page.click('#button-80');
expect(await page.evaluate(() => document.querySelector('#button-80').textContent)).toBe('clicked');
});
it('should scroll and click the button with smooth scroll behavior', async ({ page, server }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/12370' });
await page.goto(server.PREFIX + '/input/scrollable.html');
await page.addStyleTag({ content: 'html { scroll-behavior: smooth; }' });
for (let i = 0; i < 10; i++) {
await page.click('#button-80');
expect(await page.evaluate(() => document.querySelector('#button-80').textContent)).toBe('clicked');
await page.click('#button-20');
expect(await page.evaluate(() => document.querySelector('#button-20').textContent)).toBe('clicked');
}
});
it('should double click the button', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.evaluate(() => {
window['double'] = false;
const button = document.querySelector('button');
button.addEventListener('dblclick', event => {
window['double'] = true;
});
});
await page.dblclick('button');
expect(await page.evaluate('double')).toBe(true);
expect(await page.evaluate('result')).toBe('Clicked');
});
it('should click a partially obscured button', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.evaluate(() => {
const button = document.querySelector('button');
button.textContent = 'Some really long text that will go offscreen';
button.style.position = 'absolute';
button.style.left = '368px';
});
await page.click('button');
expect(await page.evaluate(() => window['result'])).toBe('Clicked');
});
it('should click a rotated button', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/rotatedButton.html');
await page.click('button');
expect(await page.evaluate('result')).toBe('Clicked');
});
it('should fire contextmenu event on right click', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/scrollable.html');
await page.click('#button-8', { button: 'right' });
expect(await page.evaluate(() => document.querySelector('#button-8').textContent)).toBe('context menu');
});
it('should click links which cause navigation', async ({ page, server }) => {
// @see https://github.com/GoogleChrome/puppeteer/issues/206
await page.setContent(`empty.html`);
// This await should not hang.
await page.click('a');
});
it('should click the button inside an iframe', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent('
');
await page.click('text=Click target');
expect(await page.evaluate('__CLICKED')).toBe(true);
});
it('should wait for BUTTON to be clickable when it has pointer-events:none', async ({ page }) => {
await page.setContent('');
let done = false;
const clickPromise = page.click('text=Click target').then(() => done = true);
await giveItAChanceToClick(page);
expect(await page.evaluate('window.__CLICKED')).toBe(undefined);
expect(done).toBe(false);
await page.evaluate(() => document.querySelector('button').style.removeProperty('pointer-events'));
await clickPromise;
expect(await page.evaluate('__CLICKED')).toBe(true);
});
it('should wait for LABEL to be clickable when it has pointer-events:none', async ({ page }) => {
await page.setContent('');
const clickPromise = page.click('text=Click target');
// Do a few roundtrips to the page.
for (let i = 0; i < 5; ++i)
expect(await page.evaluate('window.__CLICKED')).toBe(undefined);
// remove `pointer-events: none` css from button.
await page.evaluate(() => document.querySelector('label').style.removeProperty('pointer-events'));
await clickPromise;
expect(await page.evaluate('__CLICKED')).toBe(true);
});
it('should update modifiers correctly', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.click('button', { modifiers: ['Shift'] });
expect(await page.evaluate('shiftKey')).toBe(true);
await page.click('button', { modifiers: [] });
expect(await page.evaluate('shiftKey')).toBe(false);
await page.keyboard.down('Shift');
await page.click('button', { modifiers: [] });
expect(await page.evaluate('shiftKey')).toBe(false);
await page.click('button');
expect(await page.evaluate('shiftKey')).toBe(true);
await page.keyboard.up('Shift');
await page.click('button');
expect(await page.evaluate('shiftKey')).toBe(false);
});
it('should click an offscreen element when scroll-behavior is smooth', async ({ page }) => {
await page.setContent(`
`);
await page.click('button');
expect(await page.evaluate('window.clicked')).toBe(true);
});
it('should report nice error when element is detached and force-clicked', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/animating-button.html');
await page.evaluate('addButton()');
const handle = await page.$('button');
await page.evaluate('stopButton(true)');
const promise = handle.click({ force: true }).catch(e => e);
const error = await promise;
expect(await page.evaluate('window.clicked')).toBe(undefined);
expect(error.message).toContain('Element is not attached to the DOM');
});
it('should fail when element detaches after animation', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/animating-button.html');
await page.evaluate('addButton()');
const handle = await page.$('button');
const promise = handle.click().catch(e => e);
await page.evaluate('stopButton(true)');
const error = await promise;
expect(await page.evaluate('window.clicked')).toBe(undefined);
expect(error.message).toContain('Element is not attached to the DOM');
});
it('should retry when element detaches after animation', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/animating-button.html');
await page.evaluate('addButton()');
let clicked = false;
const promise = page.click('button').then(() => clicked = true);
expect(clicked).toBe(false);
expect(await page.evaluate('window.clicked')).toBe(undefined);
await page.evaluate('stopButton(true)');
await page.evaluate('addButton()');
expect(clicked).toBe(false);
expect(await page.evaluate('window.clicked')).toBe(undefined);
await page.evaluate('stopButton(true)');
await page.evaluate('addButton()');
expect(clicked).toBe(false);
expect(await page.evaluate('window.clicked')).toBe(undefined);
await page.evaluate('stopButton(false)');
await promise;
expect(clicked).toBe(true);
expect(await page.evaluate('clicked')).toBe(true);
});
it('should retry when element is animating from outside the viewport', async ({ page }) => {
await page.setContent(`
`);
const handle = await page.$('button');
const promise = handle.click();
await handle.evaluate(button => button.className = 'animated');
await promise;
expect(await page.evaluate('clicked')).toBe(true);
});
it('should fail when element is animating from outside the viewport with force', async ({ page }) => {
await page.setContent(`
`);
const handle = await page.$('button');
const promise = handle.click({ force: true }).catch(e => e);
await handle.evaluate(button => button.className = 'animated');
const error = await promise;
expect(await page.evaluate('window.clicked')).toBe(undefined);
expect(error.message).toContain('Element is outside of the viewport');
});
it('should dispatch microtasks in order', async ({ page }) => {
await page.setContent(`
`);
await page.click('button');
expect(await page.evaluate(() => window['result'])).toBe(1);
});
it('should click the button when window.innerWidth is corrupted', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.evaluate(() => Object.defineProperty(window, 'innerWidth', { value: 0 }));
await page.click('button');
expect(await page.evaluate('result')).toBe('Clicked');
});
it('should click zero-sized input by label', async ({ page }) => {
await page.setContent(`
`);
await page.click('text=Click me');
expect(await page.evaluate('window.__clicked')).toBe(true);
});
it('should not throw protocol error when navigating during the click', async ({ page, server, mode }) => {
it.skip(mode !== 'default');
await page.goto(server.PREFIX + '/input/button.html');
let firstTime = true;
const __testHookBeforeStable = async () => {
if (!firstTime)
return;
firstTime = false;
await page.goto(server.PREFIX + '/input/button.html');
};
await page.click('button', { __testHookBeforeStable } as any);
expect(await page.evaluate('result')).toBe('Clicked');
});
it('should retry when navigating during the click', async ({ page, server, mode, isAndroid }) => {
it.skip(mode !== 'default');
it.fixme(isAndroid);
await page.goto(server.PREFIX + '/input/button.html');
let firstTime = true;
const __testHookBeforeStable = async () => {
if (!firstTime)
return;
firstTime = false;
await page.goto(server.EMPTY_PAGE);
};
const error = await page.click('button', { __testHookBeforeStable, timeout: 2000 } as any).catch(e => e);
expect(error.message).toContain('element was detached from the DOM, retrying');
});
it('should not hang when frame is detached', async ({ page, server, mode }) => {
it.skip(mode !== 'default');
await attachFrame(page, 'frame1', server.EMPTY_PAGE);
const frame = page.frames()[1];
await frame.goto(server.PREFIX + '/input/button.html');
// Start moving the button.
await frame.$eval('button', button => {
button.style.transition = 'margin 5s linear 0s';
button.style.marginLeft = '200px';
});
let resolveDetachPromise;
const detachPromise = new Promise(resolve => resolveDetachPromise = resolve);
const __testHookBeforeStable = () => {
// Detach the frame after "waiting for stable" has started.
setTimeout(async () => {
await detachFrame(page, 'frame1');
resolveDetachPromise();
}, 1000);
};
const promise = frame.click('button', { __testHookBeforeStable } as any).catch(e => e);
await detachPromise;
const error = await promise;
expect(error).toBeTruthy();
expect(error.message).toMatch(/frame got detached|Frame was detached/);
});
it('should climb dom for inner label with pointer-events:none', async ({ page }) => {
await page.setContent('');
await page.click('text=Click target');
expect(await page.evaluate('__CLICKED')).toBe(true);
});
it('should climb up to [role=button]', async ({ page }) => {
await page.setContent('
Click target
');
await page.click('text=Click target');
expect(await page.evaluate('__CLICKED')).toBe(true);
});
it('should climb up to a anchor', async ({ page }) => {
// For Firefox its not allowed to return anything: https://bugzilla.mozilla.org/show_bug.cgi?id=1392046
// Note the intermediate div - it is necessary, otherwise is not recognized as a clickable link.
await page.setContent(`