Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

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

Yury Semikhatsky532ca3f7b3d8
diff --git a/docs/src/api/class-page.md b/docs/src/api/class-page.md
index ef2baf1ab7..2fc55bb2f2 100644
--- a/docs/src/api/class-page.md
+++ b/docs/src/api/class-page.md
@@ -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-%%
diff --git a/docs/src/api/class-touchscreen.md b/docs/src/api/class-touchscreen.md
index 03f8d8abda..90ea8cd759 100644
--- a/docs/src/api/class-touchscreen.md
+++ b/docs/src/api/class-touchscreen.md
@@ -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]>
diff --git a/packages/playwright-core/src/server/frames.ts b/packages/playwright-core/src/server/frames.ts
index d5a9763a1b..2a1c7646fd 100644
--- a/packages/playwright-core/src/server/frames.ts
+++ b/packages/playwright-core/src/server/frames.ts
@@ -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)));
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index e61b6edf83..71e6418b19 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -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
*/
diff --git a/tests/library/browsercontext-viewport.spec.ts b/tests/library/browsercontext-viewport.spec.ts
index 1451cb7165..9c1c6fd468 100644
--- a/tests/library/browsercontext-viewport.spec.ts
+++ b/tests/library/browsercontext-viewport.spec.ts
@@ -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();