Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
docs: add touch event snippets for other langs (#35028)
Yury Semikhatsky8415b6a10817
diff --git a/docs/src/touch-events.md b/docs/src/touch-events.mdindex de42abac7a..69e90fb714 100644--- a/docs/src/touch-events.md+++ b/docs/src/touch-events.md@@ -1,11 +1,11 @@---id: touch-events-title: "Emulating legacy touch events"+title: "Touch events (legacy)"---## Introduction-Web applications that handle [touch events](https://developer.mozilla.org/en-US/docs/Web/API/Touch_events) to respond to gestures like swipe, pinch, and tap can be tested by manually dispatching [TouchEvent](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/TouchEvent)s to the page. The examples below demonstrate how to use [`method: Locator.dispatchEvent`] and pass [Touch](https://developer.mozilla.org/en-US/docs/Web/API/Touch) points as arguments.+Web applications that handle legacy [touch events](https://developer.mozilla.org/en-US/docs/Web/API/Touch_events) to respond to gestures like swipe, pinch, and tap can be tested by manually dispatching [TouchEvent](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/TouchEvent)s to the page. The examples below demonstrate how to use [`method: Locator.dispatchEvent`] and pass [Touch](https://developer.mozilla.org/en-US/docs/Web/API/Touch) points as arguments.### Emulating pan gesture@@ -63,6 +63,246 @@ test(`pan gesture to move the map`, async ({ page }) => {});```+```csharp+using Microsoft.Playwright;+using System.Collections.Generic;+using System.Threading.Tasks;++public class TouchEvents+{+ public static async Task Main(string[] args)+ {+ using var playwright = await Playwright.CreateAsync();+ var browser = await playwright.Chromium.LaunchAsync();+ var context = await browser.NewContextAsync(playwright.Devices["Pixel 7"]);+ var page = await context.NewPageAsync();++ await page.GotoAsync(+ "https://www.google.com/maps/place/@37.4117722,-122.0713234,15z",+ new PageGotoOptions { WaitUntil = WaitUntilState.Commit }+ );+ await page.GetByRole(AriaRole.Button, new PageGetByRoleOptions { Name = "Keep using web" }).ClickAsync();+ await page.GetByRole(AriaRole.Button, new PageGetByRoleOptions { Name = "Keep using web" })+ .WaitForAsync(new LocatorWaitForOptions { State = WaitForSelectorState.Hidden });++ var met = page.Locator("[data-test-id='met']");+ for (int i = 0; i < 5; i++)+ {+ await Pan(met, 200, 100);+ }+ await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });+ }++ public static async Task Pan(ILocator locator, int deltaX, int deltaY, int steps = 5)+ {+ var bounds = await locator.BoundingBoxAsync();+ double centerX = bounds.X + bounds.Width / 2;+ double centerY = bounds.Y + bounds.Height / 2;++ var touches = new List<Dictionary<string, object>>+ {+ new Dictionary<string, object>+ {+ { "identifier", 0 },+ { "clientX", centerX },+ { "clientY", centerY }+ }+ };+ await locator.DispatchEventAsync("touchstart", new { touches, changedTouches = touches, targetTouches = touches });++ for (int i = 1; i <= steps; i++)+ {+ touches = new List<Dictionary<string, object>>+ {+ new Dictionary<string, object>+ {+ { "identifier", 0 },+ { "clientX", centerX + deltaX * i / steps },+ { "clientY", centerY + deltaY * i / steps }+ }+ };+ await locator.DispatchEventAsync("touchmove", new { touches, changedTouches = touches, targetTouches = touches });+ }++ await locator.DispatchEventAsync("touchend");+ }+}+```++```java+import com.microsoft.playwright.*;+import com.microsoft.playwright.options.*;++public class TouchEvents {+ public static void main(String[] args) {+ try (Playwright playwright = Playwright.create()) {+ Browser browser = playwright.chromium().launch();+ BrowserContext context = browser.newContext(new Browser.NewContextOptions()+ .setViewportSize(412, 839)+ .setDeviceScaleFactor(2.625)+ .setUserAgent("Mozilla/5.0 (Linux; Android 12; Pixel 7 Build/SP1A.210812.015) AppleWebKit/537.36" ++ " (KHTML, like Gecko) Chrome/94.0.4606.71 Mobile Safari/537.36")+ .setHasTouch(true)+ .setIsMobile(true)+ );+ Page page = context.newPage();++ page.navigate("https://www.google.com/maps/place/@37.4117722,-122.0713234,15z", new Page.NavigateOptions().setWaitUntil(WaitUntilState.COMMIT));+ page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Keep using web")).click();+ page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Keep using web")).waitFor(+ new Locator.WaitForOptions().setState(WaitForSelectorState.HIDDEN));++ Locator met = page.locator("[data-test-id='met']");+ for (int i = 0; i < 5; i++) {+ pan(met, 200, 100);+ }+ page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("screenshot.png")));+ }+ }++ public static void pan(Locator locator, int deltaX, int deltaY) {+ pan(locator, deltaX, deltaY, 5);+ }++ public static void pan(Locator locator, int deltaX, int deltaY, int steps) {+ BoundingBox bounds = locator.boundingBox();+ double centerX = bounds.x + bounds.width / 2;+ double centerY = bounds.y + bounds.height / 2;++ List<Map<String, Object>> touches = List.of(Map.of(+ "identifier", 0,+ "clientX", centerX,+ "clientY", centerY+ ));+ locator.dispatchEvent("touchstart", Map.of(+ "touches", touches,+ "changedTouches", touches,+ "targetTouches", touches+ ));++ for (int i = 1; i <= steps; i++) {+ touches = List.of(Map.of(+ "identifier", 0,+ "clientX", centerX + deltaX * i / steps,+ "clientY", centerY + deltaY * i / steps+ ));+ locator.dispatchEvent("touchmove", Map.of(+ "touches", touches,+ "changedTouches", touches,+ "targetTouches", touches+ ));+ }++ locator.dispatchEvent("touchend");+ }+}+```++```python sync+from playwright.sync_api import sync_playwright, expect++def pan(locator, deltaX=0, deltaY=0, steps=5):+ bounds = locator.bounding_box()+ centerX = bounds['x'] + bounds['width'] / 2+ centerY = bounds['y'] + bounds['height'] / 2++ touches = [{+ 'identifier': 0,+ 'clientX': centerX,+ 'clientY': centerY,+ }]+ locator.dispatch_event('touchstart', {+ 'touches': touches,+ 'changedTouches': touches,+ 'targetTouches': touches+ })++ for i in range(1, steps + 1):+ touches = [{+ 'identifier': 0,+ 'clientX': centerX + deltaX * i / steps,+ 'clientY': centerY + deltaY * i / steps,+ }]+ locator.dispatch_event('touchmove', {+ 'touches': touches,+ 'changedTouches': touches,+ 'targetTouches': touches+ })++ locator.dispatch_event('touchend')++def test_pan_gesture_to_move_the_map(page):+ page.goto('https://www.google.com/maps/place/@37.4117722,-122.0713234,15z', wait_until='commit')+ page.get_by_role('button', name='Keep using web').click()+ expect(page.get_by_role('button', name='Keep using web')).not_to_be_visible()+ met = page.locator('[data-test-id="met"]')+ for _ in range(5):+ pan(met, 200, 100)+ page.screenshot(path="screenshot.png")++with sync_playwright() as p:+ browser = p.chromium.launch()+ context = browser.new_context(**p.devices['Pixel 7'])+ page = context.new_page()+ test_pan_gesture_to_move_the_map(page)+ browser.close()+```++```python async+from playwright.async_api import async_playwright, expect++async def pan(locator, deltaX=0, deltaY=0, steps=5):+ bounds = await locator.bounding_box()+ centerX = bounds['x'] + bounds['width'] / 2+ centerY = bounds['y'] + bounds['height'] / 2++ touches = [{+ 'identifier': 0,+ 'clientX': centerX,+ 'clientY': centerY,+ }]+ await locator.dispatch_event('touchstart', {+ 'touches': touches,+ 'changedTouches': touches,+ 'targetTouches': touches+ })++ for i in range(1, steps + 1):+ touches = [{+ 'identifier': 0,+ 'clientX': centerX + deltaX * i / steps,+ 'clientY': centerY + deltaY * i / steps,+ }]+ await locator.dispatch_event('touchmove', {+ 'touches': touches,+ 'changedTouches': touches,+ 'targetTouches': touches+ })++ await locator.dispatch_event('touchend')++async def test_pan_gesture_to_move_the_map(page):+ await page.goto('https://www.google.com/maps/place/@37.4117722,-122.0713234,15z', wait_until='commit')+ await page.get_by_role('button', name='Keep using web').click()+ await expect(page.get_by_role('button', name='Keep using web')).not_to_be_visible()+ met = page.locator('[data-test-id="met"]')+ for _ in range(5):+ await pan(met, 200, 100)+ await page.screenshot(path="screenshot.png")++async def main():+ async with async_playwright() as p:+ browser = await p.chromium.launch()+ context = await browser.new_context(**p.devices['Pixel 7'])+ page = await context.new_page()+ await test_pan_gesture_to_move_the_map(page)+ await browser.close()++import asyncio+asyncio.run(main())+```++### Emulating pinch gestureIn the example below, we emulate pinch gesture, i.e. two touch points moving closer to each other. It is expected to zoom out the map. The app under test only uses `clientX/clientY` coordinates of touch points, so we initialize just that. In a more complex scenario you may need to also set `pageX/pageY/screenX/screenY`, if your app needs them.@@ -136,3 +376,285 @@ test(`pinch in gesture to zoom out the map`, async ({ page }) => {await expect(met).toHaveScreenshot();});```++```csharp+using Microsoft.Playwright;+using System.Collections.Generic;+using System.Threading.Tasks;++public class TouchEvents+{+ public static async Task Pinch(ILocator locator, int deltaX = 50, int steps = 5, string direction = "in")+ {+ var bounds = await locator.BoundingBoxAsync();+ double centerX = bounds.X + bounds.Width / 2;+ double centerY = bounds.Y + bounds.Height / 2;+ double stepDeltaX = deltaX / (steps + 1.0);++ var touches = new List<Dictionary<string, object>>+ {+ new Dictionary<string, object>+ {+ { "identifier", 0 },+ { "clientX", centerX - (direction == "in" ? deltaX : stepDeltaX) },+ { "clientY", centerY }+ },+ new Dictionary<string, object>+ {+ { "identifier", 1 },+ { "clientX", centerX + (direction == "in" ? deltaX : stepDeltaX) },+ { "clientY", centerY }+ }+ };+ await locator.DispatchEventAsync("touchstart", new { touches, changedTouches = touches, targetTouches = touches });++ for (int i = 1; i <= steps; i++)+ {+ double offset = direction == "in" ? (deltaX - i * stepDeltaX) : (stepDeltaX * (i + 1));+ touches = new List<Dictionary<string, object>>+ {+ new Dictionary<string, object>+ {+ { "identifier", 0 },+ { "clientX", centerX - offset },+ { "clientY", centerY }+ },+ new Dictionary<string, object>+ {+ { "identifier", 1 },+ { "clientX", centerX + offset },+ { "clientY", centerY }+ }+ };+ await locator.DispatchEventAsync("touchmove", new { touches, changedTouches = touches, targetTouches = touches });+ }++ await locator.DispatchEventAsync("touchend", new { touches = new List<object>(), changedTouches = new List<object>(), targetTouches = new List<object>() });+ }++ public static async Task TestPinchInGestureToZoomOutTheMap(IPage page)+ {+ await page.GotoAsync("https://www.google.com/maps/place/@37.4117722,-122.0713234,15z", new PageGotoOptions { WaitUntil = WaitUntilState.Commit });+ await page.GetByRole(AriaRole.Button, new PageGetByRoleOptions { Name = "Keep using web" }).ClickAsync();+ await page.GetByRole(AriaRole.Button, new PageGetByRoleOptions { Name = "Keep using web" }).WaitForAsync(new LocatorWaitForOptions { State = WaitForSelectorState.Hidden });++ var met = page.Locator("[data-test-id='met']");+ for (int i = 0; i < 5; i++)+ {+ await Pinch(met, 40, 5, "in");+ }+ await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });+ }+}+```++```java+import com.microsoft.playwright.*;+import com.microsoft.playwright.options.*;++public class TouchEvents {+ public static void main(String[] args) {+ try (Playwright playwright = Playwright.create()) {+ Browser browser = playwright.chromium().launch();+ BrowserContext context = browser.newContext(new Browser.NewContextOptions()+ .setViewportSize(412, 839)+ .setDeviceScaleFactor(2.625)+ .setUserAgent("Mozilla/5.0 (Linux; Android 12; Pixel 7 Build/SP1A.210812.015) AppleWebKit/537.36" ++ " (KHTML, like Gecko) Chrome/94.0.4606.71 Mobile Safari/537.36")+ .setHasTouch(true)+ .setIsMobile(true)+ );+ Page page = context.newPage();++ page.navigate("https://www.google.com/maps/place/@37.4117722,-122.0713234,15z", new Page.NavigateOptions().setWaitUntil(WaitUntilState.COMMIT));+ page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Keep using web")).click();+ page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Keep using web")).waitFor(+ new Locator.WaitForOptions().setState(WaitForSelectorState.HIDDEN));++ Locator met = page.locator("[data-test-id='met']");+ for (int i = 0; i < 5; i++) {+ pinch(met, 40, "in");+ }+ page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("screenshot.png")));+ }+ }++ public static void pinch(Locator locator, int deltaX, String direction) {+ pinch(locator, deltaX, direction, 5);+ }++ public static void pinch(Locator locator, int deltaX, String direction, int steps) {+ BoundingBox bounds = locator.boundingBox();+ double centerX = bounds.x + bounds.width / 2;+ double centerY = bounds.y + bounds.height / 2;+ double stepDeltaX = deltaX / (steps + 1.0);++ List<Map<String, Object>> touches = List.of(+ Map.of("identifier", 0, "clientX", centerX - (direction.equals("in") ? deltaX : stepDeltaX), "clientY", centerY),+ Map.of("identifier", 1, "clientX", centerX + (direction.equals("in") ? deltaX : stepDeltaX), "clientY", centerY)+ );+ locator.dispatchEvent("touchstart", Map.of("touches", touches, "changedTouches", touches, "targetTouches", touches));++ for (int i = 1; i <= steps; i++) {+ double offset = direction.equals("in") ? (deltaX - i * stepDeltaX) : (stepDeltaX * (i + 1));+ touches = List.of(+ Map.of("identifier", 0, "clientX", centerX - offset, "clientY", centerY),+ Map.of("identifier", 1, "clientX", centerX + offset, "clientY", centerY)+ );+ locator.dispatchEvent("touchmove", Map.of("touches", touches, "changedTouches", touches, "targetTouches", touches));+ }++ locator.dispatchEvent("touchend", Map.of("touches", List.of(), "changedTouches", List.of(), "targetTouches", List.of()));+ }+}+```++```python async+from playwright.async_api import async_playwright, expect++async def pinch(locator, arg):+ bounds = await locator.bounding_box()+ centerX = bounds['x'] + bounds['width'] / 2+ centerY = bounds['y'] + bounds['height'] / 2++ deltaX = arg.get('deltaX', 50)+ steps = arg.get('steps', 5)+ stepDeltaX = deltaX / (steps + 1)++ touches = [+ {+ 'identifier': 0,+ 'clientX': centerX - (deltaX if arg.get('direction') == 'in' else stepDeltaX),+ 'clientY': centerY,+ },+ {+ 'identifier': 1,+ 'clientX': centerX + (deltaX if arg.get('direction') == 'in' else stepDeltaX),+ 'clientY': centerY,+ },+ ]+ await locator.dispatch_event('touchstart', {+ 'touches': touches,+ 'changedTouches': touches,+ 'targetTouches': touches+ })++ for i in range(1, steps + 1):+ offset = deltaX - i * stepDeltaX if arg.get('direction') == 'in' else stepDeltaX * (i + 1)+ touches = [+ {+ 'identifier': 0,+ 'clientX': centerX - offset,+ 'clientY': centerY,+ },+ {+ 'identifier': 1,+ 'clientX': centerX + offset,+ 'clientY': centerY,+ },+ ]+ await locator.dispatch_event('touchmove', {+ 'touches': touches,+ 'changedTouches': touches,+ 'targetTouches': touches+ })++ await locator.dispatch_event('touchend', {+ 'touches': [],+ 'changedTouches': [],+ 'targetTouches': []+ })++async def test_pinch_in_gesture_to_zoom_out_the_map(page):+ await page.goto('https://www.google.com/maps/place/@37.4117722,-122.0713234,15z', wait_until='commit')+ await page.get_by_role('button', name='Keep using web').click()+ await expect(page.get_by_role('button', name='Keep using web')).not_to_be_visible()+ met = page.locator('[data-test-id="met"]')+ for _ in range(5):+ await pinch(met, {'deltaX': 40, 'direction': 'in'})+ await page.screenshot(path="screenshot.png")++async def main():+ async with async_playwright() as p:+ browser = await p.chromium.launch()+ context = await browser.new_context(**p.devices['Pixel 7'])+ page = await context.new_page()+ await test_pinch_in_gesture_to_zoom_out_the_map(page)+ await browser.close()++import asyncio+asyncio.run(main())+```++```python sync+from playwright.sync_api import sync_playwright, expect++def pinch(locator, arg):+ bounds = locator.bounding_box()+ centerX = bounds['x'] + bounds['width'] / 2+ centerY = bounds['y'] + bounds['height'] / 2++ deltaX = arg.get('deltaX', 50)+ steps = arg.get('steps', 5)+ stepDeltaX = deltaX / (steps + 1)++ touches = [+ {+ 'identifier': 0,+ 'clientX': centerX - (deltaX if arg.get('direction') == 'in' else stepDeltaX),+ 'clientY': centerY,+ },+ {+ 'identifier': 1,+ 'clientX': centerX + (deltaX if arg.get('direction') == 'in' else stepDeltaX),+ 'clientY': centerY,+ },+ ]+ locator.dispatch_event('touchstart', {+ 'touches': touches,+ 'changedTouches': touches,+ 'targetTouches': touches+ })++ for i in range(1, steps + 1):+ offset = deltaX - i * stepDeltaX if arg.get('direction') == 'in' else stepDeltaX * (i + 1)+ touches = [+ {+ 'identifier': 0,+ 'clientX': centerX - offset,+ 'clientY': centerY,+ },+ {+ 'identifier': 1,+ 'clientX': centerX + offset,+ 'clientY': centerY,+ },+ ]+ locator.dispatch_event('touchmove', {+ 'touches': touches,+ 'changedTouches': touches,+ 'targetTouches': touches+ })++ locator.dispatch_event('touchend', {+ 'touches': [],+ 'changedTouches': [],+ 'targetTouches': []+ })++def test_pinch_in_gesture_to_zoom_out_the_map(page):+ page.goto('https://www.google.com/maps/place/@37.4117722,-122.0713234,15z', wait_until='commit')+ page.get_by_role('button', name='Keep using web').click()+ expect(page.get_by_role('button', name='Keep using web')).not_to_be_visible()+ met = page.locator('[data-test-id="met"]')+ for _ in range(5):+ pinch(met, {'deltaX': 40, 'direction': 'in'})+ page.screenshot(path="screenshot.png")++with sync_playwright() as p:+ browser = p.chromium.launch()+ context = browser.new_context(**p.devices['Pixel 7'])+ page = context.new_page()+ test_pinch_in_gesture_to_zoom_out_the_map(page)+ browser.close()+```\ No newline at end of file