跳到主要內容

觸控事件 (舊版)

簡介

Web 應用程式若處理舊版觸控事件以回應滑動、捏合和點擊等手勢,則可以透過手動派發 TouchEvent 到頁面來進行測試。以下範例示範如何使用 locator.dispatch_event() 並傳遞 Touch 點作為引數。

模擬平移手勢

在以下範例中,我們模擬預期會移動地圖的平移手勢。受測應用程式僅使用觸控點的 clientX/clientY 座標,因此我們僅初始化這些座標。在更複雜的情況下,如果您的應用程式需要,您可能還需要設定 pageX/pageY/screenX/screenY

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()

模擬捏合手勢

在以下範例中,我們模擬捏合手勢,即兩個觸控點彼此靠近。預期會縮小地圖。受測應用程式僅使用觸控點的 clientX/clientY 座標,因此我們僅初始化這些座標。在更複雜的情況下,如果您的應用程式需要,您可能還需要設定 pageX/pageY/screenX/screenY

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()