Skip to content

Commit

Permalink
Update testdriver to support computedrole/computedlabel (#38758)
Browse files Browse the repository at this point in the history
Closes web-platform-tests/interop-accessibility#5

Includes basic explicit and implcit role/label tests each for accname (in /accname) and aria role computation (in /aria/role)
  • Loading branch information
cookiecrook authored Mar 6, 2023
1 parent 031a7db commit 790bb1a
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 0 deletions.
22 changes: 22 additions & 0 deletions accname/basic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>

<div id='d' style='height: 100px; width: 100px' role="group" aria-label="test label"></div>
<h1 id="h">test heading</h1>
<script>

promise_test(async t => {
const label = await test_driver.get_computed_label(document.getElementById('d'));
assert_true(label == "test label");
}, "tests labelFrom: author");

promise_test(async t => {
const label = await test_driver.get_computed_label(document.getElementById('h'));
assert_true(label == "test heading");
}, "tests labelFrom: contents");

</script>
7 changes: 7 additions & 0 deletions docs/writing-tests/testdriver.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ the global scope.
```

### Accessibility ###
```eval_rst
.. js:autofunction:: test_driver.get_computed_label
.. js:autofunction:: test_driver.get_computed_role
```

### Seure Payment Confirmation ###
```eval_rst
.. js:autofunction:: test_driver.set_spc_transaction_mode
Expand Down
34 changes: 34 additions & 0 deletions resources/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,40 @@
return cookie;
},

/**
* Get Computed Label for an element.
*
* This matches the behaviour of the
* `Get Computed Label
* <https://w3c.github.io/webdriver/#dfn-get-computed-label>`_
* WebDriver command.
*
* @param {Element} element
* @returns {Promise} fulfilled after the computed label is returned, or
* rejected in the cases the WebDriver command errors
*/
get_computed_label: async function(element) {
let label = await window.test_driver_internal.get_computed_label(element);
return label;
},

/**
* Get Computed Role for an element.
*
* This matches the behaviour of the
* `Get Computed Label
* <https://w3c.github.io/webdriver/#dfn-get-computed-role>`_
* WebDriver command.
*
* @param {Element} element
* @returns {Promise} fulfilled after the computed role is returned, or
* rejected in the cases the WebDriver command errors
*/
get_computed_role: async function(element) {
let role = await window.test_driver_internal.get_computed_role(element);
return role;
},

/**
* Send keys to an element.
*
Expand Down
8 changes: 8 additions & 0 deletions tools/webdriver/webdriver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,14 @@ def shadow_root(self):
def attribute(self, name):
return self.send_element_command("GET", "attribute/%s" % name)

@command
def get_computed_label(self):
return self.send_element_command("GET", "computedlabel")

@command
def get_computed_role(self):
return self.send_element_command("GET", "computedrole")

# This MUST come last because otherwise @property decorators above
# will be overridden by this.
@command
Expand Down
30 changes: 30 additions & 0 deletions tools/wptrunner/wptrunner/executors/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ def __call__(self, payload):
return self.protocol.cookies.get_all_cookies()


class GetComputedLabelAction:
name = "get_computed_label"

def __init__(self, logger, protocol):
self.logger = logger
self.protocol = protocol

def __call__(self, payload):
selector = payload["selector"]
element = self.protocol.select.element_by_selector(selector)
self.logger.debug("Getting computed label for element: %s" % element)
return self.protocol.accessibility.get_computed_label(element)


class GetComputedRoleAction:
name = "get_computed_role"

def __init__(self, logger, protocol):
self.logger = logger
self.protocol = protocol

def __call__(self, payload):
selector = payload["selector"]
element = self.protocol.select.element_by_selector(selector)
self.logger.debug("Getting computed role for element: %s" % element)
return self.protocol.accessibility.get_computed_role(element)


class GetNamedCookieAction:
name = "get_named_cookie"

Expand Down Expand Up @@ -253,6 +281,8 @@ def __call__(self, payload):
DeleteAllCookiesAction,
GetAllCookiesAction,
GetNamedCookieAction,
GetComputedLabelAction,
GetComputedRoleAction,
SendKeysAction,
MinimizeWindowAction,
SetWindowRectAction,
Expand Down
13 changes: 13 additions & 0 deletions tools/wptrunner/wptrunner/executors/executorwebdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
TestharnessProtocolPart,
Protocol,
SelectorProtocolPart,
AccessibilityProtocolPart,
ClickProtocolPart,
CookiesProtocolPart,
SendKeysProtocolPart,
Expand Down Expand Up @@ -186,6 +187,17 @@ def elements_by_selector(self, selector):
return self.webdriver.find.css(selector)


class WebDriverAccessibilityProtocolPart(AccessibilityProtocolPart):
def setup(self):
self.webdriver = self.parent.webdriver

def get_computed_label(self, element):
return element.get_computed_label()

def get_computed_role(self, element):
return element.get_computed_role()


class WebDriverClickProtocolPart(ClickProtocolPart):
def setup(self):
self.webdriver = self.parent.webdriver
Expand Down Expand Up @@ -342,6 +354,7 @@ class WebDriverProtocol(Protocol):
implements = [WebDriverBaseProtocolPart,
WebDriverTestharnessProtocolPart,
WebDriverSelectorProtocolPart,
WebDriverAccessibilityProtocolPart,
WebDriverClickProtocolPart,
WebDriverCookiesProtocolPart,
WebDriverSendKeysProtocolPart,
Expand Down
21 changes: 21 additions & 0 deletions tools/wptrunner/wptrunner/executors/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,27 @@ def element(self, element):
pass



class AccessibilityProtocolPart(ProtocolPart):
"""Protocol part for accessibility introspection"""
__metaclass__ = ABCMeta

name = "accessibility"

@abstractmethod
def get_computed_label(self, element):
"""Return the computed accessibility label for a specific element.
:param element: A protocol-specific handle to an element."""
pass

def get_computed_role(self, element):
"""Return the computed accessibility role for a specific element.
:param element: A protocol-specific handle to an element."""
pass


class CookiesProtocolPart(ProtocolPart):
"""Protocol part for managing cookies"""
__metaclass__ = ABCMeta
Expand Down
12 changes: 12 additions & 0 deletions tools/wptrunner/wptrunner/testdriver-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@
return create_action("get_all_cookies", {context});
};

window.test_driver_internal.get_computed_label = function(element) {
const selector = get_selector(element);
const context = get_context(element);
return create_action("get_computed_label", {selector, context});
};

window.test_driver_internal.get_computed_role = function(element) {
const selector = get_selector(element);
const context = get_context(element);
return create_action("get_computed_role", {selector, context});
};

window.test_driver_internal.get_named_cookie = function(name, context=null) {
return create_action("get_named_cookie", {name, context});
};
Expand Down
23 changes: 23 additions & 0 deletions wai-aria/role/basic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>

<div id='d' style='height: 100px; width: 100px' role="group" aria-label="test label"></div>
<h1 id="h">test heading</h1>
<script>

promise_test(async t => {
const role = await test_driver.get_computed_role(document.getElementById('d'));
assert_true(role == "group");
}, "tests explicit role");


promise_test(async t => {
const role = await test_driver.get_computed_role(document.getElementById('h'));
assert_true(role == "heading");
}, "tests implicit role");

</script>

0 comments on commit 790bb1a

Please sign in to comment.