Skip to content

Commit

Permalink
Bug 1627632 [wpt PR 22714] - Python 3: Improve webdriver tests compat…
Browse files Browse the repository at this point in the history
…ibility with Python 3, a=testonly

Automatic update from web-platform-tests
Python 3: Improve webdriver tests compatibility with Python 3 (#22714)

- Change urlparse, httplib imports to work with both 2.x and 3.x.
- Replace uses of basestring with six library calls.
- Ensure parameter for base64.decodestring() is a byte-like object.
- Convert map to list. In Python 3, map returns an iterator.
- Use six.integer_types for Possible integer types. In Python 2,
  this is long and int, and in Python 3, just int. PEP 237 specifies
  unifying Long Integers and Integers. Essentially, there is only
  one built-in integral type. In Python 3, all integers are implemented
  as “long” integer objects of arbitrary size.
- Use explicit relative imports.
--

wpt-commits: f2e918abb4b8711afb28c8b98d98be3bffc9fa9a
wpt-pr: 22714
  • Loading branch information
ziransun authored and moz-wptsync-bot committed Apr 16, 2020
1 parent 69f1038 commit 2758d33
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 57 deletions.
27 changes: 14 additions & 13 deletions testing/web-platform/tests/webdriver/tests/add_cookie/add.py
@@ -1,4 +1,5 @@
from datetime import datetime, timedelta
from six import text_type

from webdriver.transport import Response

Expand Down Expand Up @@ -59,11 +60,11 @@ def test_add_domain_cookie(session, url, server_config):

cookie = session.cookies("hello")
assert "domain" in cookie
assert isinstance(cookie["domain"], basestring)
assert isinstance(cookie["domain"], text_type)
assert "name" in cookie
assert isinstance(cookie["name"], basestring)
assert isinstance(cookie["name"], text_type)
assert "value" in cookie
assert isinstance(cookie["value"], basestring)
assert isinstance(cookie["value"], text_type)

assert cookie["name"] == "hello"
assert cookie["value"] == "world"
Expand All @@ -89,11 +90,11 @@ def test_add_cookie_for_ip(session, url, server_config, configuration):

cookie = session.cookies("hello")
assert "name" in cookie
assert isinstance(cookie["name"], basestring)
assert isinstance(cookie["name"], text_type)
assert "value" in cookie
assert isinstance(cookie["value"], basestring)
assert isinstance(cookie["value"], text_type)
assert "domain" in cookie
assert isinstance(cookie["domain"], basestring)
assert isinstance(cookie["domain"], text_type)

assert cookie["name"] == "hello"
assert cookie["value"] == "world"
Expand All @@ -118,9 +119,9 @@ def test_add_non_session_cookie(session, url):

cookie = session.cookies("hello")
assert "name" in cookie
assert isinstance(cookie["name"], basestring)
assert isinstance(cookie["name"], text_type)
assert "value" in cookie
assert isinstance(cookie["value"], basestring)
assert isinstance(cookie["value"], text_type)
assert "expiry" in cookie
assert isinstance(cookie["expiry"], int)

Expand All @@ -143,9 +144,9 @@ def test_add_session_cookie(session, url):

cookie = session.cookies("hello")
assert "name" in cookie
assert isinstance(cookie["name"], basestring)
assert isinstance(cookie["name"], text_type)
assert "value" in cookie
assert isinstance(cookie["value"], basestring)
assert isinstance(cookie["value"], text_type)
if "expiry" in cookie:
assert cookie.get("expiry") is None

Expand All @@ -168,11 +169,11 @@ def test_add_session_cookie_with_leading_dot_character_in_domain(session, url, s

cookie = session.cookies("hello")
assert "name" in cookie
assert isinstance(cookie["name"], basestring)
assert isinstance(cookie["name"], text_type)
assert "value" in cookie
assert isinstance(cookie["value"], basestring)
assert isinstance(cookie["value"], text_type)
assert "domain" in cookie
assert isinstance(cookie["domain"], basestring)
assert isinstance(cookie["domain"], text_type)

assert cookie["name"] == "hello"
assert cookie["value"] == "world"
Expand Down
@@ -1,5 +1,7 @@
import os

from six import text_type

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

Expand Down Expand Up @@ -52,7 +54,7 @@ def test_file_list(session, tmpdir):
for expected, actual in zip(files, value):
assert isinstance(actual, dict)
assert "name" in actual
assert isinstance(actual["name"], basestring)
assert isinstance(actual["name"], text_type)
assert os.path.basename(str(expected)) == actual["name"]


Expand Down
@@ -1,5 +1,7 @@
import os

from six import text_type

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

Expand Down Expand Up @@ -45,7 +47,7 @@ def test_file_list(session, tmpdir):
for expected, actual in zip(files, value):
assert isinstance(actual, dict)
assert "name" in actual
assert isinstance(actual["name"], basestring)
assert isinstance(actual["name"], text_type)
assert os.path.basename(str(expected)) == actual["name"]


Expand Down
@@ -1,3 +1,5 @@
from six import text_type

from webdriver.error import NoSuchAlertException

from tests.support.asserts import assert_error, assert_success
Expand Down Expand Up @@ -27,7 +29,7 @@ def test_get_alert_text(session):
assert isinstance(response.body, dict)
assert "value" in response.body
alert_text = response.body["value"]
assert isinstance(alert_text, basestring)
assert isinstance(alert_text, text_type)
assert alert_text == "Hello"


Expand All @@ -38,7 +40,7 @@ def test_get_confirm_text(session):
assert isinstance(response.body, dict)
assert "value" in response.body
confirm_text = response.body["value"]
assert isinstance(confirm_text, basestring)
assert isinstance(confirm_text, text_type)
assert confirm_text == "Hello"


Expand All @@ -49,7 +51,7 @@ def test_get_prompt_text(session):
assert isinstance(response.body, dict)
assert "value" in response.body
prompt_text = response.body["value"]
assert isinstance(prompt_text, basestring)
assert isinstance(prompt_text, text_type)
assert prompt_text == "Enter Your Name: "


Expand Down
@@ -1,3 +1,5 @@
from six import text_type

from tests.support import platform_name
from tests.support.inline import inline
from tests.support.asserts import assert_error, assert_success
Expand Down Expand Up @@ -31,7 +33,7 @@ def test_get_current_url_payload(session):

response = get_current_url(session)
assert response.status == 200
assert isinstance(response.body["value"], basestring)
assert isinstance(response.body["value"], text_type)


def test_get_current_url_special_pages(session):
Expand Down
20 changes: 11 additions & 9 deletions testing/web-platform/tests/webdriver/tests/get_named_cookie/get.py
@@ -1,4 +1,6 @@
from datetime import datetime, timedelta
from six import integer_types, text_type


from tests.support.asserts import assert_error, assert_success
from tests.support.helpers import clear_all_cookies
Expand Down Expand Up @@ -28,13 +30,13 @@ def test_get_named_session_cookie(session, url):
# table for cookie conversion
# https://w3c.github.io/webdriver/webdriver-spec.html#dfn-table-for-cookie-conversion
assert "name" in cookie
assert isinstance(cookie["name"], basestring)
assert isinstance(cookie["name"], text_type)
assert "value" in cookie
assert isinstance(cookie["value"], basestring)
assert isinstance(cookie["value"], text_type)
assert "path" in cookie
assert isinstance(cookie["path"], basestring)
assert isinstance(cookie["path"], text_type)
assert "domain" in cookie
assert isinstance(cookie["domain"], basestring)
assert isinstance(cookie["domain"], text_type)
assert "secure" in cookie
assert isinstance(cookie["secure"], bool)
assert "httpOnly" in cookie
Expand All @@ -60,11 +62,11 @@ def test_get_named_cookie(session, url):
assert isinstance(cookie, dict)

assert "name" in cookie
assert isinstance(cookie["name"], basestring)
assert isinstance(cookie["name"], text_type)
assert "value" in cookie
assert isinstance(cookie["value"], basestring)
assert isinstance(cookie["value"], text_type)
assert "expiry" in cookie
assert isinstance(cookie["expiry"], (int, long))
assert isinstance(cookie["expiry"], integer_types)

assert cookie["name"] == "foo"
assert cookie["value"] == "bar"
Expand Down Expand Up @@ -99,9 +101,9 @@ def test_duplicated_cookie(session, url, server_config):
assert isinstance(cookie, dict)

assert "name" in cookie
assert isinstance(cookie["name"], basestring)
assert isinstance(cookie["name"], text_type)
assert "value" in cookie
assert isinstance(cookie["value"], basestring)
assert isinstance(cookie["value"], text_type)

assert cookie["name"] == new_cookie["name"]
assert cookie["value"] == "newworld"
Expand Up @@ -2,7 +2,7 @@

import pytest

from conftest import product, flatten
from .conftest import product, flatten

from tests.support.asserts import assert_success
from tests.new_session.support.create import valid_data
Expand Down
Expand Up @@ -2,7 +2,7 @@

import pytest

from conftest import product, flatten
from .conftest import product, flatten


from tests.support.asserts import assert_success
Expand Down
@@ -1,6 +1,6 @@
import pytest

from conftest import product, flatten
from .conftest import product, flatten

from tests.new_session.support.create import invalid_data, invalid_extensions
from tests.support.asserts import assert_error
Expand Down
@@ -1,28 +1,29 @@
import uuid

import pytest

from six import text_type

from tests.support.asserts import assert_success


def test_sessionid(new_session, add_browser_capabilities):
response, _ = new_session({"capabilities": {"alwaysMatch": add_browser_capabilities({})}})
value = assert_success(response)
assert isinstance(value["sessionId"], basestring)
assert isinstance(value["sessionId"], text_type)
uuid.UUID(hex=value["sessionId"])


@pytest.mark.parametrize("capability, type", [
("browserName", basestring),
("browserVersion", basestring),
("platformName", basestring),
("browserName", text_type),
("browserVersion", text_type),
("platformName", text_type),
("acceptInsecureCerts", bool),
("pageLoadStrategy", basestring),
("pageLoadStrategy", text_type),
("proxy", dict),
("setWindowRect", bool),
("timeouts", dict),
("strictFileInteractability", bool),
("unhandledPromptBehavior", basestring),
("unhandledPromptBehavior", text_type),
])
def test_capability_type(session, capability, type):
assert isinstance(session.capabilities, dict)
Expand Down
Expand Up @@ -17,9 +17,10 @@

"""The Keys implementation."""

from inspect import getmembers
import sys

from inspect import getmembers
from six import text_type

class Keys(object):
"""
Expand Down Expand Up @@ -105,7 +106,7 @@ class Keys(object):
R_DELETE = u"\uE05D"


ALL_KEYS = getmembers(Keys, lambda x: type(x) == unicode)
ALL_KEYS = getmembers(Keys, lambda x: type(x) == text_type)

ALL_EVENTS = {
"ADD": {
Expand Down
4 changes: 3 additions & 1 deletion testing/web-platform/tests/webdriver/tests/status/status.py
@@ -1,5 +1,7 @@
import json

from six import text_type

from tests.support.asserts import assert_success


Expand All @@ -16,7 +18,7 @@ def test_get_status_no_session(http):
value = parsed_obj["value"]

assert value["ready"] in [True, False]
assert isinstance(value["message"], basestring)
assert isinstance(value["message"], text_type)


def test_status_with_session_running_on_endpoint_node(session):
Expand Down
8 changes: 4 additions & 4 deletions testing/web-platform/tests/webdriver/tests/support/asserts.py
Expand Up @@ -2,7 +2,7 @@
import imghdr
import struct

from six import PY3
from six import ensure_binary, text_type, PY3

from webdriver import Element, NoSuchAlertException, WebDriverException

Expand Down Expand Up @@ -52,8 +52,8 @@ def assert_error(response, error_code):
assert response.status == errors[error_code]
assert "value" in response.body
assert response.body["value"]["error"] == error_code
assert isinstance(response.body["value"]["message"], basestring)
assert isinstance(response.body["value"]["stacktrace"], basestring)
assert isinstance(response.body["value"]["message"], text_type)
assert isinstance(response.body["value"]["stacktrace"], text_type)
assert_response_headers(response.headers)


Expand Down Expand Up @@ -221,6 +221,6 @@ def assert_move_to_coordinates(point, target, events):

def assert_png(screenshot):
"""Test that screenshot is a Base64 encoded PNG file."""
image = base64.decodestring(screenshot)
image = base64.decodestring(ensure_binary(screenshot))
mime_type = imghdr.what("", image)
assert mime_type == "png", "Expected image to be PNG, but it was {}".format(mime_type)
@@ -1,4 +1,4 @@
import urllib
from six.moves.urllib.parse import urlencode


def basic_authentication(username=None, password=None, protocol="http"):
Expand All @@ -8,7 +8,7 @@ def basic_authentication(username=None, password=None, protocol="http"):
query = {}

return build_url("/webdriver/tests/support/authentication.py",
query=urllib.urlencode(query),
query=urlencode(query),
protocol=protocol)


Expand Down
@@ -1,11 +1,14 @@
import copy
import json
import os
import urlparse

import pytest
import webdriver

from six import string_types

from six.moves.urllib.parse import urlunsplit

from tests.support import defaults
from tests.support.helpers import cleanup_session
from tests.support.http_request import HTTPRequest
Expand Down Expand Up @@ -172,7 +175,7 @@ def inner(path, protocol="http", domain="", subdomain="", query="", fragment="")
domain = server_config["domains"][domain][subdomain]
port = server_config["ports"][protocol][0]
host = "{0}:{1}".format(domain, port)
return urlparse.urlunsplit((protocol, host, path, query, fragment))
return urlunsplit((protocol, host, path, query, fragment))

inner.__name__ = "url"
return inner
Expand All @@ -191,7 +194,7 @@ def create_dialog(dialog_type, text=None):
if text is None:
text = ""

assert isinstance(text, basestring), "`text` parameter must be a string"
assert isinstance(text, string_types), "`text` parameter must be a string"

# Script completes itself when the user prompt has been opened.
# For prompt() dialogs, add a value for the 'default' argument,
Expand Down

0 comments on commit 2758d33

Please sign in to comment.