How to use test_should_dispatch_click_event 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_dispatch_event.py

Source: test_dispatch_event.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
17
18async def test_should_dispatch_click_event(page, server):
19    await page.goto(server.PREFIX + "/input/button.html")
20    await page.dispatch_event("button", "click")
21    assert await page.evaluate("() => result") == "Clicked"
22
23
24async def test_should_dispatch_click_event_properties(page, server):
25    await page.goto(server.PREFIX + "/input/button.html")
26    await page.dispatch_event("button", "click")
27    assert await page.evaluate("() => bubbles")
28    assert await page.evaluate("() => cancelable")
29    assert await page.evaluate("() => composed")
30
31
32async def test_should_dispatch_click_svg(page):
33    await page.set_content(
34        """
35      <svg height="100" width="100">
36        <circle onclick="javascript:window.__CLICKED=42" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
37      </svg>
38    """
39    )
40    await page.dispatch_event("circle", "click")
41    assert await page.evaluate("() => window.__CLICKED") == 42
42
43
44async def test_should_dispatch_click_on_a_span_with_an_inline_element_inside(page):
45    await page.set_content(
46        """
47      <style>
48      span::before {
49        content: 'q';
50      }
51      </style>
52      <span onclick='javascript:window.CLICKED=42'></span>
53    """
54    )
55    await page.dispatch_event("span", "click")
56    assert await page.evaluate("() => window.CLICKED") == 42
57
58
59async def test_should_dispatch_click_after_navigation(page, server):
60    await page.goto(server.PREFIX + "/input/button.html")
61    await page.dispatch_event("button", "click")
62    await page.goto(server.PREFIX + "/input/button.html")
63    await page.dispatch_event("button", "click")
64    assert await page.evaluate("() => result") == "Clicked"
65
66
67async def test_should_dispatch_click_after_a_cross_origin_navigation(page, server):
68    await page.goto(server.PREFIX + "/input/button.html")
69    await page.dispatch_event("button", "click")
70    await page.goto(server.CROSS_PROCESS_PREFIX + "/input/button.html")
71    await page.dispatch_event("button", "click")
72    assert await page.evaluate("() => result") == "Clicked"
73
74
75async def test_should_not_fail_when_element_is_blocked_on_hover(page, server):
76    await page.set_content(
77        """<style>
78      container { display: block; position: relative; width: 200px; height: 50px; }
79      div, button { position: absolute; left: 0; top: 0; bottom: 0; right: 0; }
80      div { pointer-events: none; }
81      container:hover div { pointer-events: auto; background: red; }
82    </style>
83    <container>
84      <button onclick="window.clicked=true">Click me</button>
85      <div></div>
86    </container>"""
87    )
88    await page.dispatch_event("button", "click")
89    assert await page.evaluate("() => window.clicked")
90
91
92async def test_should_dispatch_click_when_node_is_added_in_shadow_dom(page, server):
93    await page.goto(server.EMPTY_PAGE)
94    watchdog = page.dispatch_event("span", "click")
95    await page.evaluate(
96        """() => {
97      const div = document.createElement('div');
98      div.attachShadow({mode: 'open'});
99      document.body.appendChild(div);
100    }"""
101    )
102    await page.evaluate("() => new Promise(f => setTimeout(f, 100))")
103    await page.evaluate(
104        """() => {
105      const span = document.createElement('span');
106      span.textContent = 'Hello from shadow';
107      span.addEventListener('click', () => window.clicked = true);
108      document.querySelector('div').shadowRoot.appendChild(span);
109    }"""
110    )
111    await watchdog
112    assert await page.evaluate("() => window.clicked")
113
114
115async def test_should_be_atomic(selectors, page, utils):
116    await utils.register_selector_engine(
117        selectors,
118        "dispatch_event",
119        """{
120            create(root, target) { },
121            query(root, selector) {
122                const result = root.querySelector(selector);
123                if (result)
124                Promise.resolve().then(() => result.onclick = "");
125                return result;
126            },
127            queryAll(root, selector) {
128                const result = Array.from(root.query_selector_all(selector));
129                for (const e of result)
130                Promise.resolve().then(() => result.onclick = "");
131                return result;
132            }
133        }""",
134    )
135    await page.set_content('<div onclick="window._clicked=true">Hello</div>')
136    await page.dispatch_event("dispatch_event=div", "click")
137    assert await page.evaluate("() => window._clicked")
138
139
140@pytest.mark.skip_browser("webkit")
141async def test_should_dispatch_drag_drop_events(page, server):
142    await page.goto(server.PREFIX + "/drag-n-drop.html")
143    dataTransfer = await page.evaluate_handle("() => new DataTransfer()")
144    await page.dispatch_event("#source", "dragstart", {"dataTransfer": dataTransfer})
145    await page.dispatch_event("#target", "drop", {"dataTransfer": dataTransfer})
146    assert await page.evaluate(
147        """() => {
148      return source.parentElement === target;
149    }"""
150    )
151
152
153@pytest.mark.skip_browser("webkit")
154async def test_should_dispatch_drag_and_drop_events_element_handle(page, server):
155    await page.goto(server.PREFIX + "/drag-n-drop.html")
156    dataTransfer = await page.evaluate_handle("() => new DataTransfer()")
157    source = await page.query_selector("#source")
158    await source.dispatch_event("dragstart", {"dataTransfer": dataTransfer})
159    target = await page.query_selector("#target")
160    await target.dispatch_event("drop", {"dataTransfer": dataTransfer})
161    assert await page.evaluate(
162        """() => {
163      return source.parentElement === target;
164    }"""
165    )
166
167
168async def test_should_dispatch_click_event_element_handle(page, server):
169    await page.goto(server.PREFIX + "/input/button.html")
170    button = await page.query_selector("button")
171    await button.dispatch_event("click")
172    assert await page.evaluate("() => result") == "Clicked"
173
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)