Skip to content

Commit a2d9b94

Browse files
committed
[java] Implementing getAriaRole and getAccessibleName operations
1 parent 794debe commit a2d9b94

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

java/client/src/org/openqa/selenium/WebElement.java

+24
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,30 @@ default String getDomAttribute(String name) {
170170
*/
171171
String getAttribute(String name);
172172

173+
/**
174+
* Gets result of computing the WAI-ARIA role of element.
175+
* <p>
176+
* See <a href="https://www.w3.org/TR/webdriver/#get-computed-role">W3C WebDriver specification</a>
177+
* for more details.
178+
*
179+
* @return the WAI-ARIA role of the element.
180+
*/
181+
default String getAriaRole() {
182+
throw new UnsupportedOperationException();
183+
}
184+
185+
/**
186+
* Gets result of a Accessible Name and Description Computation for the Accessible Name of the element.
187+
* <p>
188+
* See <a href="https://www.w3.org/TR/webdriver/#get-computed-label">W3C WebDriver specification</a>
189+
* for more details.
190+
*
191+
* @return the accessible name of the element.
192+
*/
193+
default String getAccessibleName() {
194+
throw new UnsupportedOperationException();
195+
}
196+
173197
/**
174198
* Determine whether or not this element is selected or not. This operation only applies to input
175199
* elements such as checkboxes, options in a select and radio buttons.

java/client/src/org/openqa/selenium/remote/DriverCommand.java

+8
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ static CommandPayload GET_ELEMENT_ATTRIBUTE(String id, String name) {
200200
static CommandPayload GET_ELEMENT_VALUE_OF_CSS_PROPERTY(String id, String name) {
201201
return new CommandPayload(GET_ELEMENT_VALUE_OF_CSS_PROPERTY, ImmutableMap.of("id", id, "propertyName", name));
202202
}
203+
String GET_ELEMENT_ARIA_ROLE = "getElementAriaRole";
204+
static CommandPayload GET_ELEMENT_ARIA_ROLE(String id) {
205+
return new CommandPayload(GET_ELEMENT_ARIA_ROLE, ImmutableMap.of("id", id));
206+
}
207+
String GET_ELEMENT_ACCESSIBLE_NAME = "getElementAccessibleName";
208+
static CommandPayload GET_ELEMENT_ACCESSIBLE_NAME(String id) {
209+
return new CommandPayload(GET_ELEMENT_ACCESSIBLE_NAME, ImmutableMap.of("id", id));
210+
}
203211
String ELEMENT_EQUALS = "elementEquals";
204212

205213
String SCREENSHOT = "screenshot";

java/client/src/org/openqa/selenium/remote/RemoteWebElement.java

+14
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ public String getAttribute(String name) {
155155
.getValue());
156156
}
157157

158+
@Override
159+
public String getAriaRole() {
160+
return stringValueOf(
161+
execute(DriverCommand.GET_ELEMENT_ARIA_ROLE(id))
162+
.getValue());
163+
}
164+
165+
@Override
166+
public String getAccessibleName() {
167+
return stringValueOf(
168+
execute(DriverCommand.GET_ELEMENT_ACCESSIBLE_NAME(id))
169+
.getValue());
170+
}
171+
158172
private static String stringValueOf(Object o) {
159173
if (o == null) {
160174
return null;

java/client/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_HANDLE;
3838
import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_POSITION;
3939
import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_SIZE;
40+
import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_ACCESSIBLE_NAME;
41+
import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_ARIA_ROLE;
4042
import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_ATTRIBUTE;
4143
import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_DOM_ATTRIBUTE;
4244
import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_LOCATION;
@@ -162,6 +164,8 @@ public W3CHttpCommandCodec() {
162164
String elementId = sessionId + "/element/:id";
163165
defineCommand(GET_ELEMENT_DOM_PROPERTY, get(elementId + "/property/:name"));
164166
defineCommand(GET_ELEMENT_DOM_ATTRIBUTE, get(elementId + "/attribute/:name"));
167+
defineCommand(GET_ELEMENT_ARIA_ROLE, get(elementId + "/computedrole"));
168+
defineCommand(GET_ELEMENT_ACCESSIBLE_NAME, get(elementId + "/computedlabel"));
165169

166170
// Emulate the old Actions API since everyone still likes to call these things.
167171
alias(CLICK, ACTIONS);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
import org.junit.Test;
23+
import org.openqa.selenium.environment.webserver.Page;
24+
import org.openqa.selenium.testing.JUnit4TestBase;
25+
26+
public class ElementAccessibleNameTest extends JUnit4TestBase {
27+
28+
@Test
29+
public void shouldReturnAccessibleName() {
30+
driver.get(appServer.create(
31+
new Page().withTitle("Testing Aria Role")
32+
.withBody("<h1>Level 1 Header</h1>")));
33+
WebElement header1 = driver.findElement(By.cssSelector("h1"));
34+
assertThat(header1.getAccessibleName()).isEqualTo("Level 1 Header");
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
import org.junit.Test;
23+
import org.openqa.selenium.environment.webserver.Page;
24+
import org.openqa.selenium.testing.JUnit4TestBase;
25+
26+
public class ElementAriaRoleTest extends JUnit4TestBase {
27+
28+
@Test
29+
public void shouldReturnExplicitlySpecifiedRole() {
30+
driver.get(appServer.create(
31+
new Page().withTitle("Testing Aria Role")
32+
.withBody("<div role='heading' aria-level='1'>Level 1 Header</div>")));
33+
WebElement header1 = driver.findElement(By.cssSelector("div"));
34+
assertThat(header1.getAriaRole()).isEqualTo("heading");
35+
}
36+
37+
@Test
38+
public void shouldReturnImplicitRoleDefinedByTagName() {
39+
driver.get(appServer.create(
40+
new Page().withTitle("Testing Aria Role")
41+
.withBody("<h1>Level 1 Header</h1>")));
42+
WebElement header1 = driver.findElement(By.cssSelector("h1"));
43+
assertThat(header1.getAriaRole()).isEqualTo("heading");
44+
}
45+
46+
@Test
47+
public void shouldReturnExplicitRoleEvenIfItContradictsTagName() {
48+
driver.get(appServer.create(
49+
new Page().withTitle("Testing Aria Role")
50+
.withBody("<h1 role='alert'>Level 1 Header</h1>")));
51+
WebElement header1 = driver.findElement(By.cssSelector("h1"));
52+
assertThat(header1.getAriaRole()).isEqualTo("alert");
53+
}
54+
}

0 commit comments

Comments
 (0)