Skip to content

Commit

Permalink
[webdriver] Test serialization of script values
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike committed Nov 2, 2018
1 parent ffeba23 commit 26a3c32
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
76 changes: 76 additions & 0 deletions webdriver/tests/execute_async_script/execute_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,82 @@ def test_no_browsing_context(session, closed_window):
assert_error(response, "no such window")


@pytest.mark.parametrize("values", [
{"primitive": "null", "expected": None},
{"primitive": "undefined", "expected": None},
{"primitive": "true", "expected": True},
{"primitive": "false", "expected": False},
{"primitive": "23", "expected": 23},
{"primitive": "'a string'", "expected": "a string"}
])
def test_primitive_serialization(session, values):
response = execute_async_script(
session, "arguments[0](%s);" % values["primitive"]
)
assert_success(response)
assert response.body["value"] == values["expected"]


# > To clone an object, taking the arguments value, seen, and clone algorithm:
# >
# > 1. Let result be the value of the first matching statement, matching on
# > value:
# >
# > instance of NodeList
# > instance of HTMLCollection
# > instance of Array
# > A new Array which length property is equal to the result of getting
# > the property length of value.
# > Otherwise
# > A new Object.
#
# https://w3c.github.io/webdriver/#dfn-clone-an-object
@pytest.mark.parametrize("collection", [
"[]",
"document.createElement('div').childNodes",
"document.createElement('div').children"
])
def test_array_serialization(session, collection):
response = execute_async_script(session, "arguments[0](%s);" % collection)
assert_success(response)
assert isinstance(response.body["value"], list)
assert len(response.body["value"]) == 0


@pytest.mark.parametrize("collection", [
"[]",
"document.createElement('div').childNodes",
"document.createElement('div').children"
])
def test_foreign_array_serialization(session, create_window, collection):
session.window_handle = create_window()

response = execute_async_script(session, "arguments[0](%s);" % collection)
assert_success(response)
assert isinstance(response.body["value"], list)
assert len(response.body["value"]) == 0


@pytest.mark.parametrize("non_array", [
"(function() { return arguments; }(1, 2, 3))",
"new Proxy([1, 2, 3], {})",
"({length: 3, 0: 1, 1: 2, 2: 3})"
])
def test_non_array_serialization(session, non_array):
response = execute_async_script(session, "arguments[0](%s);" % non_array)
assert_success(response)
value = response.body["value"]
assert isinstance(value, dict)
assert "length" in value
assert value["length"] == 3
assert "0" in value
assert value["0"] == 1
assert "1" in value
assert value["1"] == 2
assert "2" in value
assert value["2"] == 3


@pytest.mark.parametrize("dialog_type", ["alert", "confirm", "prompt"])
def test_abort_by_user_prompt(session, dialog_type):
response = execute_async_script(
Expand Down
74 changes: 74 additions & 0 deletions webdriver/tests/execute_script/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,80 @@ def test_ending_comment(session):
assert_success(response, 1)


@pytest.mark.parametrize("values", [
{"primitive": "null", "expected": None},
{"primitive": "undefined", "expected": None},
{"primitive": "true", "expected": True},
{"primitive": "false", "expected": False},
{"primitive": "23", "expected": 23},
{"primitive": "'a string'", "expected": "a string"}
])
def test_primitive_serialization(session, values):
response = execute_script(session, "return %s;" % values["primitive"])
assert_success(response)
assert response.body["value"] == values["expected"]


# > To clone an object, taking the arguments value, seen, and clone algorithm:
# >
# > 1. Let result be the value of the first matching statement, matching on
# > value:
# >
# > instance of NodeList
# > instance of HTMLCollection
# > instance of Array
# > A new Array which length property is equal to the result of getting
# > the property length of value.
# > Otherwise
# > A new Object.
#
# https://w3c.github.io/webdriver/#dfn-clone-an-object
@pytest.mark.parametrize("collection", [
"[]",
"document.createElement('div').childNodes",
"document.createElement('div').children"
])
def test_array_serialization(session, collection):
response = execute_script(session, "return %s;" % collection)
assert_success(response)
assert isinstance(response.body["value"], list)
assert len(response.body["value"]) == 0


@pytest.mark.parametrize("collection", [
"[]",
"document.createElement('div').childNodes",
"document.createElement('div').children"
])
def test_foreign_array_serialization(session, create_window, collection):
session.window_handle = create_window()

response = execute_script(session, "return %s;" % collection)
assert_success(response)
assert isinstance(response.body["value"], list)
assert len(response.body["value"]) == 0


@pytest.mark.parametrize("non_array", [
"(function() { return arguments; }(1, 2, 3))",
"new Proxy([1, 2, 3], {})",
"({length: 3, 0: 1, 1: 2, 2: 3})"
])
def test_non_array_serialization(session, non_array):
response = execute_script(session, "return %s;" % non_array)
assert_success(response)
value = response.body["value"]
assert isinstance(value, dict)
assert "length" in value
assert value["length"] == 3
assert "0" in value
assert value["0"] == 1
assert "1" in value
assert value["1"] == 2
assert "2" in value
assert value["2"] == 3


@pytest.mark.parametrize("dialog_type", ["alert", "confirm", "prompt"])
def test_abort_by_user_prompt(session, dialog_type):
response = execute_script(
Expand Down

0 comments on commit 26a3c32

Please sign in to comment.