觸控事件(舊版)
簡介
處理舊版 觸控事件 以回應滑動、縮放和點擊等手勢的網頁應用程式,可以透過手動分派 TouchEvent 到頁面來進行測試。以下範例說明如何使用 Locator.DispatchEventAsync()
並將 Touch 點作為引數傳遞。
請注意,Locator.DispatchEventAsync()
不會設定 Event.isTrusted
屬性。如果您的網頁依賴此屬性,請確保在測試期間停用 isTrusted
檢查。
模擬平移手勢
在以下範例中,我們模擬預期會移動地圖的平移手勢。受測試的應用程式只使用觸控點的 clientX/clientY
座標,因此我們只初始化這些項目。在更複雜的情況下,如果您的應用程式需要的話,您可能還需要設定 pageX/pageY/screenX/screenY
。
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");
}
}
模擬縮放手勢
在以下範例中,我們模擬縮放手勢,也就是兩個觸控點彼此靠近的動作。這預期會縮小地圖。受測試的應用程式只使用觸控點的 clientX/clientY
座標,因此我們只初始化這些項目。在更複雜的情況下,如果您的應用程式需要的話,您可能還需要設定 pageX/pageY/screenX/screenY
。
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" });
}
}