Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gecko Bug 1453009] Test serialisation of collections in wdspec. #10460

Merged
merged 1 commit into from Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
158 changes: 158 additions & 0 deletions webdriver/tests/execute_async_script/collections.py
@@ -0,0 +1,158 @@
import os

from tests.support.asserts import assert_same_element, assert_success
from tests.support.inline import inline


def execute_async_script(session, script, args=None):
if args is None:
args = []
body = {"script": script, "args": args}
return session.transport.send(
"POST",
"/session/{session_id}/execute/async".format(**vars(session)),
body)


def test_arguments(session):
response = execute_async_script(session, """
let [resolve] = arguments;
function func() {
return arguments;
}
resolve(func("foo", "bar"));
""")
assert_success(response, [u"foo", u"bar"])


def test_array(session):
response = execute_async_script(session, """
let [resolve] = arguments;
resolve([1, 2]);
""")
assert_success(response, [1, 2])


def test_file_list(session, tmpdir):
files = [tmpdir.join("foo.txt"), tmpdir.join("bar.txt")]

session.url = inline("<input type=file multiple>")
upload = session.find.css("input", all=False)
for file in files:
file.write("morn morn")
upload.send_keys(str(file))

response = execute_async_script(session, """
let [resolve] = arguments;
resolve(document.querySelector('input').files);
""")
value = assert_success(response)
assert isinstance(value, list)
assert len(value) == len(files)
for expected, actual in zip(files, value):
assert isinstance(actual, dict)
assert "name" in actual
assert isinstance(actual["name"], basestring)
assert os.path.basename(str(expected)) == actual["name"]


def test_html_all_collection(session):
session.url = inline("""
<p>foo
<p>bar
""")
html = session.find.css("html", all=False)
head = session.find.css("head", all=False)
body = session.find.css("body", all=False)
ps = session.find.css("p")

response = execute_async_script(session, """
let [resolve] = arguments;
resolve(document.all);
""")
value = assert_success(response)
assert isinstance(value, list)
# <html>, <head>, <body>, <p>, <p>
assert len(value) == 5

assert_same_element(session, html, value[0])
assert_same_element(session, head, value[1])
assert_same_element(session, body, value[2])
assert_same_element(session, ps[0], value[3])
assert_same_element(session, ps[1], value[4])


def test_html_collection(session):
session.url = inline("""
<p>foo
<p>bar
""")
ps = session.find.css("p")

response = execute_async_script(session, """
let [resolve] = arguments;
resolve(document.getElementsByTagName('p'));
""")
value = assert_success(response)
assert isinstance(value, list)
assert len(value) == 2
for expected, actual in zip(ps, value):
assert_same_element(session, expected, actual)


def test_html_form_controls_collection(session):
session.url = inline("""
<form>
<input>
<input>
</form>
""")
inputs = session.find.css("input")

response = execute_async_script(session, """
let [resolve] = arguments;
resolve(document.forms[0].elements);
""")
value = assert_success(response)
assert isinstance(value, list)
assert len(value) == 2
for expected, actual in zip(inputs, value):
assert_same_element(session, expected, actual)


def test_html_options_collection(session):
session.url = inline("""
<select>
<option>
<option>
</select>
""")
options = session.find.css("option")

response = execute_async_script(session, """
let [resolve] = arguments;
resolve(document.querySelector('select').options);
""")
value = assert_success(response)
assert isinstance(value, list)
assert len(value) == 2
for expected, actual in zip(options, value):
assert_same_element(session, expected, actual)


def test_node_list(session):
session.url = inline("""
<p>foo
<p>bar
""")
ps = session.find.css("p")

response = execute_async_script(session, """
let [resolve] = arguments;
resolve(document.querySelectorAll('p'));
""")
value = assert_success(response)
assert isinstance(value, list)
assert len(value) == 2
for expected, actual in zip(ps, value):
assert_same_element(session, expected, actual)
136 changes: 136 additions & 0 deletions webdriver/tests/execute_script/collections.py
@@ -0,0 +1,136 @@
import os

from tests.support.asserts import assert_same_element, assert_success
from tests.support.inline import inline


def execute_script(session, script, args=None):
if args is None:
args = []
body = {"script": script, "args": args}
return session.transport.send(
"POST",
"/session/{session_id}/execute/sync".format(**vars(session)),
body)


def test_arguments(session):
response = execute_script(session, """
function func() {
return arguments;
}
return func("foo", "bar");
""")
assert_success(response, [u"foo", u"bar"])


def test_array(session):
response = execute_script(session, "return [1, 2]")
assert_success(response, [1, 2])


def test_file_list(session, tmpdir):
files = [tmpdir.join("foo.txt"), tmpdir.join("bar.txt")]

session.url = inline("<input type=file multiple>")
upload = session.find.css("input", all=False)
for file in files:
file.write("morn morn")
upload.send_keys(str(file))

response = execute_script(session, "return document.querySelector('input').files")
value = assert_success(response)
assert isinstance(value, list)
assert len(value) == len(files)
for expected, actual in zip(files, value):
assert isinstance(actual, dict)
assert "name" in actual
assert isinstance(actual["name"], basestring)
assert os.path.basename(str(expected)) == actual["name"]


def test_html_all_collection(session):
session.url = inline("""
<p>foo
<p>bar
""")
html = session.find.css("html", all=False)
head = session.find.css("head", all=False)
body = session.find.css("body", all=False)
ps = session.find.css("p")

response = execute_script(session, "return document.all")
value = assert_success(response)
assert isinstance(value, list)
# <html>, <head>, <body>, <p>, <p>
assert len(value) == 5

assert_same_element(session, html, value[0])
assert_same_element(session, head, value[1])
assert_same_element(session, body, value[2])
assert_same_element(session, ps[0], value[3])
assert_same_element(session, ps[1], value[4])


def test_html_collection(session):
session.url = inline("""
<p>foo
<p>bar
""")
ps = session.find.css("p")

response = execute_script(session, "return document.getElementsByTagName('p')")
value = assert_success(response)
assert isinstance(value, list)
assert len(value) == 2
for expected, actual in zip(ps, value):
assert_same_element(session, expected, actual)


def test_html_form_controls_collection(session):
session.url = inline("""
<form>
<input>
<input>
</form>
""")
inputs = session.find.css("input")

response = execute_script(session, "return document.forms[0].elements")
value = assert_success(response)
assert isinstance(value, list)
assert len(value) == 2
for expected, actual in zip(inputs, value):
assert_same_element(session, expected, actual)


def test_html_options_collection(session):
session.url = inline("""
<select>
<option>
<option>
</select>
""")
options = session.find.css("option")

response = execute_script(session, "return document.querySelector('select').options")
value = assert_success(response)
assert isinstance(value, list)
assert len(value) == 2
for expected, actual in zip(options, value):
assert_same_element(session, expected, actual)


def test_node_list(session):
session.url = inline("""
<p>foo
<p>bar
""")
ps = session.find.css("p")

response = execute_script(session, "return document.querySelectorAll('p')")
value = assert_success(response)
assert isinstance(value, list)
assert len(value) == 2
for expected, actual in zip(ps, value):
assert_same_element(session, expected, actual)