How to use test_should_not_send_mouse_events_touchend_is_canceled method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test_tap.py

Source: test_tap.py Github

copy
1# Copyright (c) Microsoft Corporation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import pytest
16
17from playwright.sync_api import ElementHandle, JSHandle
18
19
20@pytest.fixture
21def context(browser):
22    context = browser.new_context(has_touch=True)
23    yield context
24    context.close()
25
26
27def test_should_send_all_of_the_correct_events(page):
28    page.set_content(
29        """
30            <div id="a" style="background: lightblue; width: 50px; height: 50px">a</div>
31            <div id="b" style="background: pink; width: 50px; height: 50px">b</div>
32        """
33    )
34    page.tap("#a")
35    element_handle = track_events(page.query_selector("#b"))
36    page.tap("#b")
37    assert element_handle.json_value() == [
38        "pointerover",
39        "pointerenter",
40        "pointerdown",
41        "touchstart",
42        "pointerup",
43        "pointerout",
44        "pointerleave",
45        "touchend",
46        "mouseover",
47        "mouseenter",
48        "mousemove",
49        "mousedown",
50        "mouseup",
51        "click",
52    ]
53
54
55def test_should_not_send_mouse_events_touchstart_is_canceled(page):
56    page.set_content("hello world")
57    page.evaluate(
58        """() => {
59            // touchstart is not cancelable unless passive is false
60            document.addEventListener('touchstart', t => t.preventDefault(), {passive: false});
61        }"""
62    )
63    events_handle = track_events(page.query_selector("body"))
64    page.tap("body")
65    assert events_handle.json_value() == [
66        "pointerover",
67        "pointerenter",
68        "pointerdown",
69        "touchstart",
70        "pointerup",
71        "pointerout",
72        "pointerleave",
73        "touchend",
74    ]
75
76
77def test_should_not_send_mouse_events_touchend_is_canceled(page):
78    page.set_content("hello world")
79    page.evaluate(
80        """() => {
81            // touchstart is not cancelable unless passive is false
82            document.addEventListener('touchend', t => t.preventDefault());
83        }"""
84    )
85    events_handle = track_events(page.query_selector("body"))
86    page.tap("body")
87    assert events_handle.json_value() == [
88        "pointerover",
89        "pointerenter",
90        "pointerdown",
91        "touchstart",
92        "pointerup",
93        "pointerout",
94        "pointerleave",
95        "touchend",
96    ]
97
98
99def track_events(target: ElementHandle) -> JSHandle:
100    return target.evaluate_handle(
101        """target => {
102            const events = [];
103            for (const event of [
104                'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'click',
105                'pointercancel', 'pointerdown', 'pointerenter', 'pointerleave', 'pointermove', 'pointerout', 'pointerover', 'pointerup',
106                'touchstart', 'touchend', 'touchmove', 'touchcancel',])
107                target.addEventListener(event, () => events.push(event), false);
108            return events;
109        }"""
110    )
111
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)