fix: throw on tap when hasTouch=false (#20457)

Fixes #20430
This commit is contained in:
Yury Semikhatsky 2023-01-27 15:51:57 -08:00 committed by GitHub
parent 2c27bd3b07
commit 532ca3f7b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 3 deletions

View file

@ -3498,7 +3498,7 @@ When all steps combined have not finished during the specified [`option: timeout
[TimeoutError]. Passing zero timeout disables this.
:::note
[`method: Page.tap`] requires that the [`option: hasTouch`] option of the browser context be set to true.
[`method: Page.tap`] the method will throw if [`option: hasTouch`] option of the browser context is false.
:::
### param: Page.tap.selector = %%-input-selector-%%

View file

@ -9,6 +9,10 @@ touchscreen can only be used in browser contexts that have been initialized with
Dispatches a `touchstart` and `touchend` event with a single touch at the position ([`param: x`],[`param: y`]).
:::note
[`method: Page.tap`] the method will throw if [`option: hasTouch`] option of the browser context is false.
:::
### param: Touchscreen.tap.x
* since: v1.8
- `x` <[float]>

View file

@ -1211,6 +1211,8 @@ export class Frame extends SdkObject {
}
async tap(metadata: CallMetadata, selector: string, options: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
if (!this._page._browserContext._options.hasTouch)
throw new Error('The page does not support tap. Use hasTouch context option to enable touch support.');
const controller = new ProgressController(metadata, this);
return controller.run(async progress => {
return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._tap(progress, options)));

View file

@ -3968,8 +3968,8 @@ export interface Page {
* When all steps combined have not finished during the specified `timeout`, this method throws a [TimeoutError].
* Passing zero timeout disables this.
*
* **NOTE** [page.tap(selector[, options])](https://playwright.dev/docs/api/class-page#page-tap) requires that the
* `hasTouch` option of the browser context be set to true.
* **NOTE** [page.tap(selector[, options])](https://playwright.dev/docs/api/class-page#page-tap) the method will throw
* if `hasTouch` option of the browser context is false.
* @param selector A selector to search for an element. If there are multiple elements satisfying the selector, the first will be
* used.
* @param options
@ -18098,6 +18098,9 @@ export interface Selectors {
export interface Touchscreen {
/**
* Dispatches a `touchstart` and `touchend` event with a single touch at the position (`x`,`y`).
*
* **NOTE** [page.tap(selector[, options])](https://playwright.dev/docs/api/class-page#page-tap) the method will throw
* if `hasTouch` option of the browser context is false.
* @param x
* @param y
*/

View file

@ -98,6 +98,20 @@ it('should not have touch by default', async ({ page, server }) => {
expect(await page.evaluate(() => document.body.textContent.trim())).toBe('NO');
});
it('should throw on tap if hasTouch is not enabled', async ({ page }) => {
await page.setContent(`<div>a</div>`);
{
const error = await page.tap('div').catch(e => e);
expect(error).toBeTruthy();
expect(error.message).toContain('The page does not support tap');
}
{
const error = await page.locator('div').tap().catch(e => e);
expect(error).toBeTruthy();
expect(error.message).toContain('The page does not support tap');
}
});
browserTest('should support touch with null viewport', async ({ browser, server }) => {
const context = await browser.newContext({ viewport: null, hasTouch: true });
const page = await context.newPage();