示例#1
0
async def test_strict_mode(bidi_session, top_context):

    # As long as there is no `SOME_VARIABLE`, the command should fail in strict mode.
    with pytest.raises(ScriptEvaluateResultException) as exception:
        await bidi_session.script.call_function(
            function_declaration="()=>{'use strict';return SOME_VARIABLE=1}",
            await_promise=False,
            target=ContextTarget(top_context["context"]),
        )
    recursive_compare(specific_error_response({"type": "error"}), exception.value.result)

    # In non-strict mode, the command should succeed and global `SOME_VARIABLE` should be created.
    result = await bidi_session.script.call_function(
        function_declaration="()=>{return SOME_VARIABLE=1}",
        await_promise=False,
        target=ContextTarget(top_context["context"]),
    )
    assert result == {
        "type": "number",
        "value": 1}

    # Access created by the previous command `SOME_VARIABLE`.
    result = await bidi_session.script.call_function(
        function_declaration="()=>{'use strict';return SOME_VARIABLE=1}",
        await_promise=False,
        target=ContextTarget(top_context["context"]),
    )
    assert result == {
        "type": "number",
        "value": 1}
示例#2
0
async def test_arrow_function(bidi_session, top_context):
    result = await bidi_session.script.call_function(
        function_declaration="()=>{return 1+2;}",
        await_promise=False,
        target=ContextTarget(top_context["context"]))

    assert result == {"type": "number", "value": 3}
示例#3
0
async def test_default_arguments(bidi_session, top_context):
    result = await bidi_session.script.call_function(
        function_declaration="(...args)=>{return args}",
        await_promise=False,
        target=ContextTarget(top_context["context"]))

    recursive_compare({"type": "array", "value": []}, result)
示例#4
0
async def test_arguments(bidi_session, top_context):
    result = await bidi_session.script.call_function(
        function_declaration="(...args)=>{return args}",
        arguments=[{
            "type": "string",
            "value": "ARGUMENT_STRING_VALUE"
        }, {
            "type": "number",
            "value": 42
        }],
        await_promise=False,
        target=ContextTarget(top_context["context"]))

    recursive_compare(
        {
            "type":
            "array",
            "value": [{
                "type": 'string',
                "value": 'ARGUMENT_STRING_VALUE'
            }, {
                "type": 'number',
                "value": 42
            }]
        }, result)
示例#5
0
async def test_params_await_promise_invalid_type(bidi_session, top_context,
                                                 await_promise):
    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.script.evaluate(expression="1 + 2",
                                           await_promise=await_promise,
                                           target=ContextTarget(
                                               top_context["context"]))
示例#6
0
async def test_await_resolve_array(bidi_session, top_context):
    result = await bidi_session.script.evaluate(
        expression=
        "Promise.resolve([1, 'text', true, ['will not be serialized']])",
        await_promise=True,
        target=ContextTarget(top_context["context"]),
    )

    assert result == {
        "type":
        "array",
        "value": [
            {
                "type": "number",
                "value": 1
            },
            {
                "type": "string",
                "value": "text"
            },
            {
                "type": "boolean",
                "value": True
            },
            {
                "type": "array"
            },
        ],
    }
async def test_exception_details(bidi_session, top_context, await_promise,
                                 expression, expected):
    if await_promise:
        expression = f"Promise.reject({expression})"
    else:
        expression = f"throw {expression}"

    with pytest.raises(ScriptEvaluateResultException) as exception:
        await bidi_session.script.evaluate(
            expression=expression,
            target=ContextTarget(top_context["context"]),
            await_promise=await_promise,
        )

    recursive_compare(
        {
            "realm": any_string,
            "exceptionDetails": {
                "columnNumber": any_int,
                "exception": expected,
                "lineNumber": any_int,
                "stackTrace": any_stack_trace,
                "text": any_string,
            },
        },
        exception.value.result,
    )
async def test_evaluate_window_open_without_url(bidi_session, wait_for_event,
                                                top_context):
    # Unsubscribe in case a previous tests subscribed to the event
    await bidi_session.session.unsubscribe(events=[CONTEXT_CREATED_EVENT])

    await bidi_session.session.subscribe(events=[CONTEXT_CREATED_EVENT])

    on_entry = wait_for_event(CONTEXT_CREATED_EVENT)

    await bidi_session.script.evaluate(expression="""window.open();""",
                                       target=ContextTarget(
                                           top_context["context"]),
                                       await_promise=False)

    context_info = await on_entry

    assert_browsing_context(
        context_info,
        context=None,
        children=None,
        url="about:blank",
        parent=None,
    )

    await bidi_session.session.unsubscribe(events=[CONTEXT_CREATED_EVENT])
async def test_exception_details(bidi_session, top_context, await_promise,
                                 expression, expected):
    function_declaration = f"()=>{{ throw {expression} }}"
    if await_promise:
        function_declaration = "async" + function_declaration

    with pytest.raises(ScriptEvaluateResultException) as exception:
        await bidi_session.script.call_function(
            function_declaration=function_declaration,
            await_promise=await_promise,
            target=ContextTarget(top_context["context"]),
        )

    recursive_compare(
        {
            "realm": any_string,
            "exceptionDetails": {
                "columnNumber": any_int,
                "exception": expected,
                "lineNumber": any_int,
                "stackTrace": any_stack_trace,
                "text": any_string,
            },
        },
        exception.value.result,
    )
示例#10
0
async def test_params_this_invalid_type(bidi_session, top_context, this):
    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.script.call_function(function_declaration="1 + 2",
                                                this=this,
                                                await_promise=False,
                                                target=ContextTarget(
                                                    top_context["context"]))
示例#11
0
async def test_type(bidi_session, value):
    contexts = await bidi_session.browsing_context.get_tree(max_depth=0)
    assert len(contexts) == 1

    new_context = await bidi_session.browsing_context.create(type_hint=value)
    assert contexts[0]["context"] != new_context["context"]

    # Check there is an additional browsing context
    contexts = await bidi_session.browsing_context.get_tree(max_depth=0)
    assert len(contexts) == 2

    # Retrieve the new context info
    contexts = await bidi_session.browsing_context.get_tree(
        max_depth=0, root=new_context["context"]
    )

    assert_browsing_context(
        contexts[0],
        new_context["context"],
        children=None,
        is_root=True,
        parent=None,
        url="about:blank",
    )

    opener_protocol_value = await bidi_session.script.evaluate(
        expression="!!window.opener",
        target=ContextTarget(new_context["context"]),
        await_promise=False)
    assert opener_protocol_value["value"] is False

    await bidi_session.browsing_context.close(context=new_context["context"])
示例#12
0
async def test_eval(bidi_session, top_context):
    result = await bidi_session.script.evaluate(expression="1 + 2",
                                                target=ContextTarget(
                                                    top_context["context"]),
                                                await_promise=True)

    assert result == {"type": "number", "value": 3}
示例#13
0
async def test_resolved_promise_with_await_promise_false(
        bidi_session, top_context):
    result = await bidi_session.script.evaluate(
        expression="Promise.resolve('SOME_RESOLVED_RESULT')",
        target=ContextTarget(top_context["context"]),
        await_promise=False)

    recursive_compare({"type": "promise", "handle": any_string}, result)
示例#14
0
async def test_remote_value_promise_no_await(bidi_session, top_context):
    result = await bidi_session.script.call_function(
        function_declaration="()=>Promise.resolve(42)",
        await_promise=False,
        target=ContextTarget(top_context["context"]),
    )

    assert result == {"type": "promise"}
示例#15
0
async def test_resolved_promise_with_await_promise_true(
        bidi_session, top_context):
    result = await bidi_session.script.evaluate(
        expression="Promise.resolve('SOME_RESOLVED_RESULT')",
        target=ContextTarget(top_context["context"]),
        await_promise=True)

    assert result == {"type": "string", "value": "SOME_RESOLVED_RESULT"}
async def test_return_value(bidi_session, top_context, await_promise, result_ownership, should_contain_handle):
    result = await bidi_session.script.call_function(
        function_declaration="async function(){return {a:1}}",
        await_promise=await_promise,
        result_ownership=result_ownership,
        target=ContextTarget(top_context["context"]))

    assert_handle(result, should_contain_handle)
示例#17
0
async def test_params_result_ownership_invalid_value(bidi_session, top_context,
                                                     result_ownership):
    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.script.call_function(
            function_declaration="1 + 2",
            await_promise=False,
            target=ContextTarget(top_context["context"]),
            result_ownership=result_ownership)
示例#18
0
async def test_return_value(bidi_session, top_context, await_promise, result_ownership, should_contain_handle):
    result = await bidi_session.script.evaluate(
        expression="Promise.resolve({a:1})",
        await_promise=await_promise,
        result_ownership=result_ownership,
        target=ContextTarget(top_context["context"]))

    assert_handle(result, should_contain_handle)
示例#19
0
async def test_params_result_ownership_invalid_value(bidi_session, top_context,
                                                     result_ownership):
    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.script.evaluate(expression="1 + 2",
                                           result_ownership=result_ownership,
                                           target=ContextTarget(
                                               top_context["context"]),
                                           await_promise=True)
示例#20
0
async def test_primitive_values(bidi_session, top_context, expression, expected):
    result = await bidi_session.script.evaluate(
        expression=expression,
        target=ContextTarget(top_context["context"]),
        await_promise=True,
    )

    assert result == expected
示例#21
0
async def test_remote_value_argument(bidi_session, top_context):
    remote_value_result = await bidi_session.script.evaluate(
        expression="({SOME_PROPERTY:'SOME_VALUE'})",
        await_promise=False,
        target=ContextTarget(top_context["context"]))

    remote_value_handle = remote_value_result["handle"]

    result = await bidi_session.script.call_function(
        function_declaration="(obj)=>{return obj.SOME_PROPERTY;}",
        arguments=[{
            "handle": remote_value_handle}],
        await_promise=False,
        target=ContextTarget(top_context["context"]))

    assert result == {
        "type": "string",
        "value": "SOME_VALUE"}
示例#22
0
async def test_throw_exception(bidi_session, top_context, result_ownership, should_contain_handle):
    with pytest.raises(ScriptEvaluateResultException) as exception:
        await bidi_session.script.evaluate(
            expression='throw {a:1}',
            await_promise=False,
            result_ownership=result_ownership,
            target=ContextTarget(top_context["context"]))

    assert_handle(exception.value.result["exceptionDetails"]["exception"], should_contain_handle)
示例#23
0
async def test_invalid_script(bidi_session, top_context, result_ownership, should_contain_handle):
    with pytest.raises(ScriptEvaluateResultException) as exception:
        await bidi_session.script.evaluate(
            expression="))) !!@@## some invalid JS script (((",
            await_promise=False,
            result_ownership=result_ownership,
            target=ContextTarget(top_context["context"]))

    assert_handle(exception.value.result["exceptionDetails"]["exception"], should_contain_handle)
async def test_rejected_promise(bidi_session, top_context, result_ownership, should_contain_handle):
    with pytest.raises(ScriptEvaluateResultException) as exception:
        await bidi_session.script.call_function(
            function_declaration="()=>{return Promise.reject({a:1})}",
            await_promise=True,
            result_ownership=result_ownership,
            target=ContextTarget(top_context["context"]))

    assert_handle(exception.value.result["exceptionDetails"]["exception"], should_contain_handle)
示例#25
0
async def test_default_this(bidi_session, top_context):
    result = await bidi_session.script.call_function(
        function_declaration="function(){return this}",
        await_promise=False,
        target=ContextTarget(top_context["context"]))

    # Note: https://github.com/w3c/webdriver-bidi/issues/251
    recursive_compare({
        "type": 'window',
    }, result)
示例#26
0
async def test_interact_with_dom(bidi_session, top_context):
    result = await bidi_session.script.evaluate(
        expression="'window.location.href: ' + window.location.href",
        target=ContextTarget(top_context["context"]),
        await_promise=True)

    assert result == {
        "type": "string",
        "value": "window.location.href: about:blank"
    }
示例#27
0
async def test_await_promise_async_arrow(bidi_session, top_context,
                                         await_promise):
    result = await bidi_session.script.call_function(
        function_declaration="async ()=>{return 'SOME_VALUE'}",
        await_promise=await_promise,
        target=ContextTarget(top_context["context"]))

    if await_promise:
        assert result == {"type": "string", "value": "SOME_VALUE"}
    else:
        recursive_compare({"type": "promise"}, result)
示例#28
0
async def test_await_resolve_date(bidi_session, top_context):
    result = await bidi_session.script.evaluate(
        expression="Promise.resolve(new Date(0))",
        await_promise=True,
        target=ContextTarget(top_context["context"]),
    )

    assert result == {
        "type": "date",
        "value": "1970-01-01T00:00:00.000Z",
    }
示例#29
0
async def test_primitive_values(bidi_session, top_context, await_promise, expression, expected):
    function_declaration = f"()=>{expression}"
    if await_promise:
        function_declaration = "async" + function_declaration

    result = await bidi_session.script.call_function(
        function_declaration=function_declaration,
        await_promise=await_promise,
        target=ContextTarget(top_context["context"]),
    )

    assert result == expected
示例#30
0
async def test_await_delayed_promise(bidi_session, top_context):
    result = await bidi_session.script.evaluate(
        expression="""
          new Promise(r => {{
            setTimeout(() => r("SOME_DELAYED_RESULT"), 0);
          }})
        """,
        await_promise=True,
        target=ContextTarget(top_context["context"]),
    )

    assert result == {"type": "string", "value": "SOME_DELAYED_RESULT"}