[webdriver][bidi] Test script.callFunction with nodes as arguments and this (#65778)

Add subtests to `call_function/this.py`:
- A node passed as `this`.
- An invalid argument with an unknown sharedId as `this` fails with
  "invalid argument", because the arguments are deserialized before
  `this`.
- An unknown sharedId as an argument, or as `this`, fails with "no such
  node" even when the function declaration doesn't evaluate to a
  function, because both are deserialized before the declaration is
  evaluated.

Add subtests to `call_function/remote_reference.py`:
- A node nested in an array, object, map and set argument.
- An unknown sharedId without a handle fails with "no such node".

(An AI model was used to review and revise the tests.)

Local results

These are from macOS (arm64), headless, with `./wpt run`. Stability was checked with `--verify`: 10 restarts for Firefox (`--verify-repeat-loop=0 --verify-repeat-restart=10`),
and 20 runs in a loop plus 5 restarts for Chrome (`--verify-repeat-loop=20`).

| Browser | Results | Stability |
|---|---|---|
| Firefox Nightly 162.0a1, geckodriver 0.37.1 | All pass | Stable |
| Chrome 158.0.8127.37, ChromeDriver 158.0.8127.88 | New subtests pass, except `test_arguments_deserialized_before_this` | Stable |
| Chrome Canary 160.0.8168.0, ChromeDriver 160.0.8168.0 | New subtests pass, except `test_arguments_deserialized_before_this` | Stable |

Chrome fails `test_arguments_deserialized_before_this` with "no such node": it
deserializes `this` before the arguments, where "script.callFunction"
deserializes the arguments first
(https://w3c.github.io/webdriver-bidi/#command-script-callFunction).
diff --git a/webdriver/tests/bidi/script/call_function/remote_reference.py b/webdriver/tests/bidi/script/call_function/remote_reference.py
index a0430f4..f2db431 100644
--- a/webdriver/tests/bidi/script/call_function/remote_reference.py
+++ b/webdriver/tests/bidi/script/call_function/remote_reference.py
@@ -212,6 +212,51 @@
     assert result == {"type": "number", "value": expected_node_type}
 
 
+@pytest.mark.parametrize(
+    "value_fn, function_declaration",
+    [
+        (
+            lambda value: {"type": "array", "value": [value]},
+            "(arg) => arg[0] === document.querySelector('img')",
+        ),
+        (
+            lambda value: {"type": "object", "value": [["nested", value]]},
+            "(arg) => arg.nested === document.querySelector('img')",
+        ),
+        (
+            lambda value: {"type": "map", "value": [["foobar", value]]},
+            "(arg) => arg.get('foobar') === document.querySelector('img')",
+        ),
+        (
+            lambda value: {"type": "set", "value": [value]},
+            "(arg) => arg.has(document.querySelector('img'))",
+        ),
+    ],
+    ids=["array", "object", "map", "set"],
+)
+async def test_remote_reference_node_nested(
+    bidi_session, get_test_page, top_context, value_fn, function_declaration
+):
+    await bidi_session.browsing_context.navigate(
+        context=top_context['context'], url=get_test_page(), wait="complete"
+    )
+
+    remote_reference = await bidi_session.script.evaluate(
+        expression="document.querySelector('img')",
+        await_promise=False,
+        target=ContextTarget(top_context["context"]),
+    )
+
+    result = await bidi_session.script.call_function(
+        function_declaration=function_declaration,
+        arguments=[value_fn(remote_reference)],
+        await_promise=False,
+        target=ContextTarget(top_context["context"]),
+    )
+
+    assert result == {"type": "boolean", "value": True}
+
+
 async def test_remote_reference_node_cdata(bidi_session, inline, top_context):
     xml_page = inline("""<foo>CDATA section: <![CDATA[ < > & ]]>.</foo>""", doctype="xml")
 
@@ -262,6 +307,16 @@
         )
 
 
+async def test_remote_reference_unknown_shared_id(bidi_session, top_context):
+    with pytest.raises(error.NoSuchNodeException):
+        await bidi_session.script.call_function(
+            function_declaration="(node) => node",
+            arguments=[{"sharedId": "foo"}],
+            await_promise=False,
+            target=ContextTarget(top_context["context"]),
+        )
+
+
 @pytest.mark.parametrize(
     "expression, function_declaration, expected",
     [
diff --git a/webdriver/tests/bidi/script/call_function/this.py b/webdriver/tests/bidi/script/call_function/this.py
index de91bc3..2ad4b02 100644
--- a/webdriver/tests/bidi/script/call_function/this.py
+++ b/webdriver/tests/bidi/script/call_function/this.py
@@ -1,4 +1,5 @@
 import pytest
+import webdriver.bidi.error as error
 
 from webdriver.bidi.modules.script import ContextTarget
 
@@ -38,6 +39,54 @@
     }, result)
 
 
+async def test_this_node(bidi_session, top_context, inline):
+    await bidi_session.browsing_context.navigate(
+        context=top_context["context"], url=inline("<div id='foo'>"), wait="complete"
+    )
+
+    node = await bidi_session.script.evaluate(
+        expression="document.querySelector('#foo')",
+        await_promise=False,
+        target=ContextTarget(top_context["context"]))
+
+    result = await bidi_session.script.call_function(
+        function_declaration="function() { return this === document.querySelector('#foo'); }",
+        this=node,
+        await_promise=False,
+        target=ContextTarget(top_context["context"]))
+
+    assert result == {"type": "boolean", "value": True}
+
+
+async def test_arguments_deserialized_before_this(bidi_session, top_context):
+    # The arguments are deserialized before "this", so an invalid argument
+    # fails the command even when "this" references an unknown node.
+    with pytest.raises(error.InvalidArgumentException):
+        await bidi_session.script.call_function(
+            function_declaration="function() {}",
+            arguments=[{"type": "date", "value": "foo"}],
+            this={"sharedId": "foo"},
+            await_promise=False,
+            target=ContextTarget(top_context["context"]))
+
+
+@pytest.mark.parametrize(
+    "parameters",
+    [{"arguments": [{"sharedId": "foo"}]}, {"this": {"sharedId": "foo"}}],
+    ids=["arguments", "this"],
+)
+async def test_deserialized_before_function_declaration(bidi_session, top_context, parameters):
+    # The arguments and "this" are deserialized before the function declaration
+    # is evaluated, so an unknown node fails the command even when the
+    # declaration doesn't evaluate to a function.
+    with pytest.raises(error.NoSuchNodeException):
+        await bidi_session.script.call_function(
+            function_declaration="42",
+            await_promise=False,
+            target=ContextTarget(top_context["context"]),
+            **parameters)
+
+
 @pytest.mark.parametrize(
     "value_fn, function_declaration",
     [