|
| 1 | +import pytest |
| 2 | + |
| 3 | +from webdriver.bidi.modules.script import ContextTarget |
| 4 | + |
| 5 | +pytestmark = pytest.mark.asyncio |
| 6 | + |
| 7 | + |
| 8 | +async def test_node_set_event_handler_value(bidi_session, inline): |
| 9 | + context = await bidi_session.browsing_context.create(type_hint="tab") |
| 10 | + url = inline(""" |
| 11 | + <script>window.onload = ()=>{console.log('onload')}</script> |
| 12 | + """) |
| 13 | + await bidi_session.browsing_context.navigate(context=context["context"], |
| 14 | + url=url, wait="complete") |
| 15 | + |
| 16 | + result = await bidi_session.script.evaluate( |
| 17 | + expression="window.onload", |
| 18 | + target=ContextTarget(context["context"]), |
| 19 | + await_promise=False, |
| 20 | + ) |
| 21 | + assert result == {'type': 'function'} |
| 22 | + |
| 23 | + # Disable scripting. |
| 24 | + await bidi_session.emulation.set_scripting_enabled( |
| 25 | + enabled=False, |
| 26 | + contexts=[context["context"]], |
| 27 | + ) |
| 28 | + |
| 29 | + result = await bidi_session.script.evaluate( |
| 30 | + expression="window.onload", |
| 31 | + target=ContextTarget(context["context"]), |
| 32 | + await_promise=False, |
| 33 | + ) |
| 34 | + assert result == {'type': 'function'} |
| 35 | + |
| 36 | + |
| 37 | +async def test_node_added_event_handlers(bidi_session, inline): |
| 38 | + context = await bidi_session.browsing_context.create(type_hint="tab") |
| 39 | + url = inline(""" |
| 40 | + <script> |
| 41 | + addEventListener("message", (event) => { console.log('onmessage')}) |
| 42 | + </script> |
| 43 | + """) |
| 44 | + await bidi_session.browsing_context.navigate(context=context["context"], |
| 45 | + url=url, wait="complete") |
| 46 | + |
| 47 | + result = await bidi_session.script.evaluate( |
| 48 | + expression="window.onmessage", |
| 49 | + target=ContextTarget(context["context"]), |
| 50 | + await_promise=False, |
| 51 | + ) |
| 52 | + assert result == {'type': 'null'} |
| 53 | + |
| 54 | + await bidi_session.script.evaluate( |
| 55 | + expression="""addEventListener("message", (event) => { console.log('onmessage')})""", |
| 56 | + target=ContextTarget(context["context"]), |
| 57 | + await_promise=False, |
| 58 | + ) |
| 59 | + |
| 60 | + result = await bidi_session.script.evaluate( |
| 61 | + expression="window.onmessage", |
| 62 | + target=ContextTarget(context["context"]), |
| 63 | + await_promise=False, |
| 64 | + ) |
| 65 | + assert result == {'type': 'null'} |
| 66 | + |
| 67 | + # Disable scripting. |
| 68 | + await bidi_session.emulation.set_scripting_enabled( |
| 69 | + enabled=False, |
| 70 | + contexts=[context["context"]], |
| 71 | + ) |
| 72 | + |
| 73 | + result = await bidi_session.script.evaluate( |
| 74 | + expression="window.onmessage", |
| 75 | + target=ContextTarget(context["context"]), |
| 76 | + await_promise=False, |
| 77 | + ) |
| 78 | + assert result == {'type': 'null'} |
| 79 | + |
| 80 | + |
| 81 | +async def test_finalization_registry(bidi_session, inline): |
| 82 | + """ |
| 83 | + The test relies on `window.gc()` to force GC. In Chrome, it is exposed when |
| 84 | + run with `--js-flags=--expose-gc` flag. |
| 85 | + """ |
| 86 | + context = await bidi_session.browsing_context.create(type_hint="tab") |
| 87 | + |
| 88 | + url = inline("""<script> |
| 89 | + window.finalizationRegistryTriggered = false; |
| 90 | + const registry = new FinalizationRegistry((heldValue) => { |
| 91 | + window.finalizationRegistryTriggered = true; |
| 92 | + }); |
| 93 | +
|
| 94 | + function createAndRegister() { |
| 95 | + const myObject = {}; |
| 96 | + registry.register(myObject); |
| 97 | + } |
| 98 | + createAndRegister(); |
| 99 | + </script> |
| 100 | + """) |
| 101 | + |
| 102 | + await bidi_session.browsing_context.navigate(context=context["context"], |
| 103 | + url=url, wait="complete") |
| 104 | + |
| 105 | + result = await bidi_session.script.evaluate( |
| 106 | + expression="window.finalizationRegistryTriggered", |
| 107 | + target=ContextTarget(context["context"]), |
| 108 | + await_promise=False, |
| 109 | + ) |
| 110 | + assert result == {'type': 'boolean', 'value': False} |
| 111 | + |
| 112 | + # Disable scripting. |
| 113 | + await bidi_session.emulation.set_scripting_enabled( |
| 114 | + enabled=False, |
| 115 | + contexts=[context["context"]], |
| 116 | + ) |
| 117 | + |
| 118 | + result = await bidi_session.script.evaluate( |
| 119 | + expression="window.finalizationRegistryTriggered", |
| 120 | + target=ContextTarget(context["context"]), |
| 121 | + await_promise=False, |
| 122 | + ) |
| 123 | + assert result == {'type': 'boolean', 'value': False} |
| 124 | + |
| 125 | + # Force GC to collect `myObject`. |
| 126 | + await bidi_session.script.evaluate( |
| 127 | + expression="window.gc()", |
| 128 | + target=ContextTarget(context["context"]), |
| 129 | + await_promise=False, |
| 130 | + ) |
| 131 | + |
| 132 | + result = await bidi_session.script.evaluate( |
| 133 | + expression="window.finalizationRegistryTriggered", |
| 134 | + target=ContextTarget(context["context"]), |
| 135 | + await_promise=False, |
| 136 | + ) |
| 137 | + assert result == {'type': 'boolean', 'value': True} |
| 138 | + |
| 139 | + |
| 140 | +async def test_enqueue_promise_job(bidi_session, inline): |
| 141 | + context = await bidi_session.browsing_context.create(type_hint="tab") |
| 142 | + |
| 143 | + # Disable scripting. |
| 144 | + await bidi_session.emulation.set_scripting_enabled( |
| 145 | + enabled=False, |
| 146 | + contexts=[context["context"]], |
| 147 | + ) |
| 148 | + |
| 149 | + result = await bidi_session.script.evaluate( |
| 150 | + expression="Promise.resolve(true).then(x=>x)", |
| 151 | + target=ContextTarget(context["context"]), |
| 152 | + await_promise=True, |
| 153 | + ) |
| 154 | + assert result == {'type': 'boolean', 'value': True} |
0 commit comments