|
| 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 | +import asyncio |
| 15 | + |
| 16 | + |
| 17 | +async def test_should_think_that_it_is_focused_by_default(page): |
| 18 | + assert await page.evaluate("document.hasFocus()") |
| 19 | + |
| 20 | + |
| 21 | +async def test_should_think_that_all_pages_are_focused(page): |
| 22 | + page2 = await page.context.newPage() |
| 23 | + assert await page.evaluate("document.hasFocus()") |
| 24 | + assert await page2.evaluate("document.hasFocus()") |
| 25 | + await page2.close() |
| 26 | + |
| 27 | + |
| 28 | +async def test_should_focus_popups_by_default(page, server): |
| 29 | + await page.goto(server.EMPTY_PAGE) |
| 30 | + [popup, _] = await asyncio.gather( |
| 31 | + page.waitForEvent("popup"), |
| 32 | + page.evaluate("url => { window.open(url); }", server.EMPTY_PAGE), |
| 33 | + ) |
| 34 | + assert await popup.evaluate("document.hasFocus()") |
| 35 | + assert await page.evaluate("document.hasFocus()") |
| 36 | + |
| 37 | + |
| 38 | +async def test_should_provide_target_for_keyboard_events(page, server): |
| 39 | + page2 = await page.context.newPage() |
| 40 | + await asyncio.gather( |
| 41 | + page.goto(server.PREFIX + "/input/textarea.html"), |
| 42 | + page2.goto(server.PREFIX + "/input/textarea.html"), |
| 43 | + ) |
| 44 | + await asyncio.gather( |
| 45 | + page.focus("input"), page2.focus("input"), |
| 46 | + ) |
| 47 | + text = "first" |
| 48 | + text2 = "second" |
| 49 | + await asyncio.gather( |
| 50 | + page.keyboard.type(text), page2.keyboard.type(text2), |
| 51 | + ) |
| 52 | + results = await asyncio.gather(page.evaluate("result"), page2.evaluate("result"),) |
| 53 | + assert results == [text, text2] |
| 54 | + |
| 55 | + |
| 56 | +async def test_should_not_affect_mouse_event_target_page(page, server): |
| 57 | + page2 = await page.context.newPage() |
| 58 | + clickcounter = """() { |
| 59 | + document.onclick = () => window.clickCount = (window.clickCount || 0) + 1; |
| 60 | + }""" |
| 61 | + await asyncio.gather( |
| 62 | + page.evaluate(clickcounter), |
| 63 | + page2.evaluate(clickcounter), |
| 64 | + page.focus("body"), |
| 65 | + page2.focus("body"), |
| 66 | + ) |
| 67 | + await asyncio.gather( |
| 68 | + page.mouse.click(1, 1), page2.mouse.click(1, 1), |
| 69 | + ) |
| 70 | + counters = await asyncio.gather( |
| 71 | + page.evaluate("window.clickCount"), page2.evaluate("window.clickCount"), |
| 72 | + ) |
| 73 | + assert counters == [1, 1] |
| 74 | + |
| 75 | + |
| 76 | +async def test_should_change_document_activeElement(page, server): |
| 77 | + page2 = await page.context.newPage() |
| 78 | + await asyncio.gather( |
| 79 | + page.goto(server.PREFIX + "/input/textarea.html"), |
| 80 | + page2.goto(server.PREFIX + "/input/textarea.html"), |
| 81 | + ) |
| 82 | + await asyncio.gather( |
| 83 | + page.focus("input"), page2.focus("textarea"), |
| 84 | + ) |
| 85 | + active = await asyncio.gather( |
| 86 | + page.evaluate("document.activeElement.tagName"), |
| 87 | + page2.evaluate("document.activeElement.tagName"), |
| 88 | + ) |
| 89 | + assert active == ["INPUT", "TEXTAREA"] |
| 90 | + |
| 91 | + |
| 92 | +async def test_should_not_affect_screenshots(page, server, assert_to_be_golden): |
| 93 | + # Firefox headful produces a different image. |
| 94 | + page2 = await page.context.newPage() |
| 95 | + await asyncio.gather( |
| 96 | + page.setViewportSize(width=500, height=500), |
| 97 | + page.goto(server.PREFIX + "/grid.html"), |
| 98 | + page2.setViewportSize(width=50, height=50), |
| 99 | + page2.goto(server.PREFIX + "/grid.html"), |
| 100 | + ) |
| 101 | + await asyncio.gather( |
| 102 | + page.focus("body"), page2.focus("body"), |
| 103 | + ) |
| 104 | + screenshots = await asyncio.gather(page.screenshot(), page2.screenshot(),) |
| 105 | + assert_to_be_golden(screenshots[0], "screenshot-sanity.png") |
| 106 | + assert_to_be_golden(screenshots[1], "grid-cell-0.png") |
| 107 | + |
| 108 | + |
| 109 | +async def test_should_change_focused_iframe(page, server, utils): |
| 110 | + await page.goto(server.EMPTY_PAGE) |
| 111 | + [frame1, frame2] = await asyncio.gather( |
| 112 | + utils.attach_frame(page, "frame1", server.PREFIX + "/input/textarea.html"), |
| 113 | + utils.attach_frame(page, "frame2", server.PREFIX + "/input/textarea.html"), |
| 114 | + ) |
| 115 | + logger = """() => { |
| 116 | + self._events = []; |
| 117 | + const element = document.querySelector('input'); |
| 118 | + element.onfocus = element.onblur = (e) => self._events.push(e.type); |
| 119 | + }""" |
| 120 | + await asyncio.gather( |
| 121 | + frame1.evaluate(logger), frame2.evaluate(logger), |
| 122 | + ) |
| 123 | + focused = await asyncio.gather( |
| 124 | + frame1.evaluate("document.hasFocus()"), frame2.evaluate("document.hasFocus()"), |
| 125 | + ) |
| 126 | + assert focused == [False, False] |
| 127 | + await frame1.focus("input") |
| 128 | + events = await asyncio.gather( |
| 129 | + frame1.evaluate("self._events"), frame2.evaluate("self._events"), |
| 130 | + ) |
| 131 | + assert events == [["focus"], []] |
| 132 | + focused = await asyncio.gather( |
| 133 | + frame1.evaluate("document.hasFocus()"), frame2.evaluate("document.hasFocus()"), |
| 134 | + ) |
| 135 | + assert focused == [True, False] |
| 136 | + await frame2.focus("input") |
| 137 | + events = await asyncio.gather( |
| 138 | + frame1.evaluate("self._events"), frame2.evaluate("self._events"), |
| 139 | + ) |
| 140 | + assert events == [["focus", "blur"], ["focus"]] |
| 141 | + focused = await asyncio.gather( |
| 142 | + frame1.evaluate("document.hasFocus()"), frame2.evaluate("document.hasFocus()"), |
| 143 | + ) |
| 144 | + assert focused == [False, True] |
0 commit comments