|
| 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 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | + |
| 18 | +async def test_should_work(page): |
| 19 | + await page.setContent("<div id=d1 tabIndex=0></div>") |
| 20 | + assert await page.evaluate("() => document.activeElement.nodeName") == "BODY" |
| 21 | + await page.focus("#d1") |
| 22 | + assert await page.evaluate("() => document.activeElement.id") == "d1" |
| 23 | + |
| 24 | + |
| 25 | +async def test_should_emit_focus_event(page): |
| 26 | + await page.setContent("<div id=d1 tabIndex=0></div>") |
| 27 | + focused = [] |
| 28 | + await page.exposeFunction("focusEvent", lambda: focused.append(True)) |
| 29 | + await page.evaluate("() => d1.addEventListener('focus', focusEvent)") |
| 30 | + await page.focus("#d1") |
| 31 | + assert focused == [True] |
| 32 | + |
| 33 | + |
| 34 | +async def test_should_emit_blur_event(page): |
| 35 | + await page.setContent( |
| 36 | + "<div id=d1 tabIndex=0>DIV1</div><div id=d2 tabIndex=0>DIV2</div>" |
| 37 | + ) |
| 38 | + await page.focus("#d1") |
| 39 | + focused = [] |
| 40 | + blurred = [] |
| 41 | + await page.exposeFunction("focusEvent", lambda: focused.append(True)) |
| 42 | + await page.exposeFunction("blurEvent", lambda: blurred.append(True)) |
| 43 | + await page.evaluate("() => d1.addEventListener('blur', blurEvent)") |
| 44 | + await page.evaluate("() => d2.addEventListener('focus', focusEvent)") |
| 45 | + await page.focus("#d2") |
| 46 | + assert focused == [True] |
| 47 | + assert blurred == [True] |
| 48 | + |
| 49 | + |
| 50 | +async def test_should_traverse_focus(page): |
| 51 | + await page.setContent('<input id="i1"><input id="i2">') |
| 52 | + focused = [] |
| 53 | + await page.exposeFunction("focusEvent", lambda: focused.append(True)) |
| 54 | + await page.evaluate("() => i2.addEventListener('focus', focusEvent)") |
| 55 | + |
| 56 | + await page.focus("#i1") |
| 57 | + await page.keyboard.type("First") |
| 58 | + await page.keyboard.press("Tab") |
| 59 | + await page.keyboard.type("Last") |
| 60 | + |
| 61 | + assert focused == [True] |
| 62 | + assert await page.evalOnSelector("#i1", "e => e.value") == "First" |
| 63 | + assert await page.evalOnSelector("#i2", "e => e.value") == "Last" |
| 64 | + |
| 65 | + |
| 66 | +async def test_should_traverse_focus_in_all_directions(page): |
| 67 | + await page.setContent('<input value="1"><input value="2"><input value="3">') |
| 68 | + await page.keyboard.press("Tab") |
| 69 | + assert await page.evaluate("() => document.activeElement.value") == "1" |
| 70 | + await page.keyboard.press("Tab") |
| 71 | + assert await page.evaluate("() => document.activeElement.value") == "2" |
| 72 | + await page.keyboard.press("Tab") |
| 73 | + assert await page.evaluate("() => document.activeElement.value") == "3" |
| 74 | + await page.keyboard.press("Shift+Tab") |
| 75 | + assert await page.evaluate("() => document.activeElement.value") == "2" |
| 76 | + await page.keyboard.press("Shift+Tab") |
| 77 | + assert await page.evaluate("() => document.activeElement.value") == "1" |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.only_platform("darwin") |
| 81 | +@pytest.mark.only_browser("webkit") |
| 82 | +async def test_should_traverse_only_form_elements(page): |
| 83 | + await page.setContent( |
| 84 | + """ |
| 85 | + <input id="input-1"> |
| 86 | + <button id="button">buttton</button> |
| 87 | + <a href id="link">link</a> |
| 88 | + <input id="input-2"> |
| 89 | + """ |
| 90 | + ) |
| 91 | + await page.keyboard.press("Tab") |
| 92 | + assert await page.evaluate("() => document.activeElement.id") == "input-1" |
| 93 | + await page.keyboard.press("Tab") |
| 94 | + assert await page.evaluate("() => document.activeElement.id") == "input-2" |
| 95 | + await page.keyboard.press("Shift+Tab") |
| 96 | + assert await page.evaluate("() => document.activeElement.id") == "input-1" |
| 97 | + await page.keyboard.press("Alt+Tab") |
| 98 | + assert await page.evaluate("() => document.activeElement.id") == "button" |
| 99 | + await page.keyboard.press("Alt+Tab") |
| 100 | + assert await page.evaluate("() => document.activeElement.id") == "link" |
| 101 | + await page.keyboard.press("Alt+Tab") |
| 102 | + assert await page.evaluate("() => document.activeElement.id") == "input-2" |
| 103 | + await page.keyboard.press("Alt+Shift+Tab") |
| 104 | + assert await page.evaluate("() => document.activeElement.id") == "link" |
| 105 | + await page.keyboard.press("Alt+Shift+Tab") |
| 106 | + assert await page.evaluate("() => document.activeElement.id") == "button" |
| 107 | + await page.keyboard.press("Alt+Shift+Tab") |
| 108 | + assert await page.evaluate("() => document.activeElement.id") == "input-1" |
| 109 | + |
| 110 | + |
| 111 | +# }); |
| 112 | +# }); |
0 commit comments