summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py
blob: ecd3173e8784a8973453f1ad54e7ade61d235381 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import pytest
import webdriver.bidi.error as error

from webdriver.bidi.modules.script import ContextTarget

pytestmark = pytest.mark.asyncio


MAX_INT = 9007199254740991


async def navigate_to_page(bidi_session, inline, top_context):
    url = inline("""<div>foo</div>""")
    await bidi_session.browsing_context.navigate(
        context=top_context["context"], url=url, wait="complete"
    )


@pytest.mark.parametrize("value", [None, False, 42, {}, []])
async def test_params_context_invalid_type(bidi_session, inline, top_context, value):
    await navigate_to_page(bidi_session, inline, top_context)

    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.browsing_context.locate_nodes(
            context=value, locator={"type": "css", "value": "div"}
        )


@pytest.mark.parametrize("value", [None, False, 42, {}, []])
async def test_params_locator_type_invalid_type(bidi_session, inline, top_context, value):
    await navigate_to_page(bidi_session, inline, top_context)

    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.browsing_context.locate_nodes(
            context=top_context["context"], locator={ "type": value, "value": "div" }
        )


@pytest.mark.parametrize("type", ["", "invalid"])
async def test_params_locator_type_invalid_value(bidi_session, inline, top_context, type):
    await navigate_to_page(bidi_session, inline, top_context)

    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.browsing_context.locate_nodes(
            context=top_context["context"], locator={ "type": type, "value": "div" }
        )


@pytest.mark.parametrize("type,value", [
    ("css", "a*b"),
    ("xpath", ""),
    ("innerText", "")
])
async def test_params_locator_value_invalid_value(bidi_session, inline, top_context, type, value):
    await navigate_to_page(bidi_session, inline, top_context)

    with pytest.raises(error.InvalidSelectorException):
        await bidi_session.browsing_context.locate_nodes(
            context=top_context["context"], locator={ "type": type, "value": value }
        )


async def test_params_locator_xpath_unknown_error(bidi_session, inline, top_context):
    await navigate_to_page(bidi_session, inline, top_context)

    with pytest.raises(error.UnknownErrorException):
        await bidi_session.browsing_context.locate_nodes(
            context=top_context["context"], locator={"type": "xpath", "value": "/foo:bar"}
        )


@pytest.mark.parametrize("value", [False, "string", 1.5, {}, []])
async def test_params_max_node_count_invalid_type(bidi_session, inline, top_context, value):
    await navigate_to_page(bidi_session, inline, top_context)

    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.browsing_context.locate_nodes(
            context=top_context["context"],
            locator={ "type": "css", "value": "div" },
            max_node_count=value
        )


@pytest.mark.parametrize("value", [0, MAX_INT + 1])
async def test_params_max_node_count_invalid_value(bidi_session, inline, top_context, value):
    await navigate_to_page(bidi_session, inline, top_context)

    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.browsing_context.locate_nodes(
            context=top_context["context"],
            locator={ "type": "invalid", "value": "div" },
            max_node_count=value
        )


@pytest.mark.parametrize("value", [False, 42, "foo", []])
async def test_params_serialization_options_invalid_type(bidi_session, inline, top_context, value):
    await navigate_to_page(bidi_session, inline, top_context)

    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.browsing_context.locate_nodes(
            context=top_context["context"],
            locator={ "type": "css", "value": "div" },
            serialization_options=value
        )


@pytest.mark.parametrize("value", [False, "string", 42, {}])
async def test_params_start_nodes_invalid_type(bidi_session, inline, top_context, value):
    await navigate_to_page(bidi_session, inline, top_context)

    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.browsing_context.locate_nodes(
            context=top_context["context"],
            locator={ "type": "css", "value": "div" },
            start_nodes=value
        )


async def test_params_start_nodes_empty_list(bidi_session, inline, top_context):
    await navigate_to_page(bidi_session, inline, top_context)

    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.browsing_context.locate_nodes(
            context=top_context["context"],
            locator={ "type": "css", "value": "div" },
            start_nodes=[]
        )


@pytest.mark.parametrize(
    "value",
    [
        {"type": "number", "value": 3},
        {"type": "window"},
        {"type": "array", "value": ["test"]},
        {
            "type": "object",
            "value": [
                ["1", {"type": "string", "value": "foo"}],
            ],
        },
    ],
)
async def test_params_start_nodes_not_dom_node(
    bidi_session, inline, top_context, value
):
    await navigate_to_page(bidi_session, inline, top_context)

    if value["type"] == "window":
        value["value"] = top_context["context"]

    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.browsing_context.locate_nodes(
            context=top_context["context"],
            locator={"type": "css", "value": "div"},
            start_nodes=[value],
        )


@pytest.mark.parametrize(
    "expression",
    [
        "document.querySelector('input#button').attributes[0]",
        "document.querySelector('#with-text-node').childNodes[0]",
        """document.createProcessingInstruction("xml-stylesheet", "href='foo.css'")""",
        "document.querySelector('#with-comment').childNodes[0]",
        "document.doctype",
        "document.getElementsByTagName('div')",
        "document.querySelectorAll('div')"
    ],
)
async def test_params_start_nodes_dom_node_not_element(
    bidi_session, inline, top_context, get_test_page, expression
):
    await navigate_to_page(bidi_session, inline, top_context)

    await bidi_session.browsing_context.navigate(
        context=top_context['context'], url=get_test_page(), wait="complete"
    )

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

    with pytest.raises(error.InvalidArgumentException):
        await bidi_session.browsing_context.locate_nodes(
            context=top_context["context"],
            locator={"type": "css", "value": "div"},
            start_nodes=[remote_reference],
        )