Skip to content

Commit

Permalink
[py] Add ARIA APIs for getting the role and label of an Element
Browse files Browse the repository at this point in the history
  • Loading branch information
AutomatedTester committed Feb 15, 2021
1 parent 450eb05 commit eeb0cb0
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions py/selenium/webdriver/remote/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class Command(object):
GET_ELEMENT_ATTRIBUTE = "getElementAttribute"
GET_ELEMENT_PROPERTY = "getElementProperty"
GET_ELEMENT_VALUE_OF_CSS_PROPERTY = "getElementValueOfCssProperty"
GET_ELEMENT_ARIA_ROLE = "getElementAriaRole"
GET_ELEMENT_ARIA_LABEL = "getElementAriaLabel"
SCREENSHOT = "screenshot"
ELEMENT_SCREENSHOT = "elementScreenshot"
IMPLICIT_WAIT = "implicitlyWait"
Expand Down
4 changes: 4 additions & 0 deletions py/selenium/webdriver/remote/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ def __init__(self, remote_server_addr, keep_alive=False, resolve_ip=None, ignore
('GET', '/session/$sessionId/element/$id/attribute/$name'),
Command.GET_ELEMENT_PROPERTY:
('GET', '/session/$sessionId/element/$id/property/$name'),
Command.GET_ELEMENT_ARIA_ROLE:
('GET', '/session/$sessionId/element/$id/computedrole'),
Command.GET_ELEMENT_ARIA_LABEL:
('GET', '/session/$sessionId/element/$id/computedlabel'),
Command.GET_ALL_COOKIES: ('GET', '/session/$sessionId/cookie'),
Command.ADD_COOKIE: ('POST', '/session/$sessionId/cookie'),
Command.GET_COOKIE: ('GET', '/session/$sessionId/cookie/$name'),
Expand Down
10 changes: 10 additions & 0 deletions py/selenium/webdriver/remote/webelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,16 @@ def rect(self) -> dict:
"""A dictionary with the size and location of the element."""
return self._execute(Command.GET_ELEMENT_RECT)['value']

@property
def aria_role(self) -> str:
""" Returns the ARIA role of the current web element"""
return self._execute(Command.GET_ELEMENT_ARIA_ROLE)['value']

@property
def accessible_name(self) -> str:
"""Returns the ARIA Lavel of the current webelement"""
return self._execute(Command.GET_ELEMENT_ARIA_LABEL)['value']

@property
def screenshot_as_base64(self) -> str:
"""
Expand Down
28 changes: 28 additions & 0 deletions py/test/selenium/webdriver/common/element_aria_label_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License

from selenium.webdriver.common.by import By

import pytest


@pytest.mark.xfail_firefox
@pytest.mark.xfail_safari
def test_shouldReturnAccessibleName(driver):
driver.get("data:text/html,<h1>Level 1 Header</h1>")
header1 = driver.find_element(By.CSS_SELECTOR, "h1")
assert header1.accessible_name == "Level 1 Header"
44 changes: 44 additions & 0 deletions py/test/selenium/webdriver/common/element_aria_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License

from selenium.webdriver.common.by import By

import pytest


@pytest.mark.xfail_firefox
@pytest.mark.xfail_safari
def test_should_return_explicitly_specified_role(driver):
driver.get("data:text/html,<div role='heading' aria-level='1'>Level 1 Header</div>")
header1 = driver.find_element(By.CSS_SELECTOR, "div")
assert header1.aria_role == "heading"


@pytest.mark.xfail_firefox
@pytest.mark.xfail_safari
def test_shouldReturnImplicitRoleDefinedByTagName(driver):
driver.get("data:text/html,<h1>Level 1 Header</h1>")
header1 = driver.find_element(By.CSS_SELECTOR, "h1")
assert header1.aria_role == "heading"


@pytest.mark.xfail_firefox
@pytest.mark.xfail_safari
def test_should_return_explicit_role_even_if_it_contradicts_tag_name(driver):
driver.get("data:text/html,<h1 role='alert'>Level 1 Header</h1>")
header1 = driver.find_element(By.CSS_SELECTOR, "h1")
assert header1.aria_role == "alert"

0 comments on commit eeb0cb0

Please sign in to comment.