Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ElementDomAttributeTest.java
3989 views
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
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
22
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
23
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
24
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
25
import static org.openqa.selenium.testing.drivers.Browser.IE;
26
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
27
28
import java.util.List;
29
import org.junit.jupiter.api.Test;
30
import org.openqa.selenium.environment.webserver.Page;
31
import org.openqa.selenium.testing.JupiterTestBase;
32
import org.openqa.selenium.testing.NotYetImplemented;
33
34
class ElementDomAttributeTest extends JupiterTestBase {
35
36
@Test
37
void testShouldReturnNullWhenGettingTheValueOfAnAttributeThatIsNotListed() {
38
driver.get(pages.simpleTestPage);
39
WebElement head = driver.findElement(By.xpath("/html"));
40
String attribute = head.getDomAttribute("cheese");
41
assertThat(attribute).isNull();
42
}
43
44
@Test
45
void testShouldReturnNullWhenGettingSrcAttributeOfInvalidImgTag() {
46
driver.get(pages.simpleTestPage);
47
WebElement img = driver.findElement(By.id("invalidImgTag"));
48
String attribute = img.getDomAttribute("src");
49
assertThat(attribute).isNull();
50
}
51
52
@Test
53
void testShouldReturnTheActualValueWhenGettingSrcAttributeOfAValidImgTag() {
54
driver.get(pages.simpleTestPage);
55
WebElement img = driver.findElement(By.id("validImgTag"));
56
String attribute = img.getDomAttribute("src");
57
assertThat(attribute).isEqualTo("icon.gif");
58
}
59
60
@Test
61
void testShouldReturnTheActualValueWhenGettingHrefAttributeOfAValidAnchorTag() {
62
driver.get(pages.simpleTestPage);
63
WebElement img = driver.findElement(By.id("validAnchorTag"));
64
String attribute = img.getDomAttribute("href");
65
assertThat(attribute).isEqualTo("icon.gif");
66
}
67
68
@Test
69
void testShouldReturnEmptyAttributeValuesWhenPresentAndTheValueIsActuallyEmpty() {
70
driver.get(pages.simpleTestPage);
71
WebElement body = driver.findElement(By.xpath("//body"));
72
assertThat(body.getDomAttribute("style")).isEmpty();
73
}
74
75
@Test
76
void testShouldReturnTheValueOfTheDisabledAttributeAsNullIfNotSet() {
77
driver.get(pages.formPage);
78
WebElement inputElement = driver.findElement(By.xpath("//input[@id='working']"));
79
assertThat(inputElement.getDomAttribute("disabled")).isNull();
80
assertThat(inputElement.isEnabled()).isTrue();
81
82
WebElement pElement = driver.findElement(By.id("peas"));
83
assertThat(pElement.getDomAttribute("disabled")).isNull();
84
assertThat(pElement.isEnabled()).isTrue();
85
}
86
87
@Test
88
void testShouldNotReturnTheValueOfTheIndexAttributeIfItIsMissing() {
89
driver.get(pages.formPage);
90
WebElement multiSelect = driver.findElement(By.id("multi"));
91
List<WebElement> options = multiSelect.findElements(By.tagName("option"));
92
assertThat(options.get(1).getDomAttribute("index")).isNull();
93
}
94
95
@Test
96
void testShouldIndicateTheElementsThatAreDisabledAreNotEnabled() {
97
driver.get(pages.formPage);
98
WebElement inputElement = driver.findElement(By.xpath("//input[@id='notWorking']"));
99
assertThat(inputElement.isEnabled()).isFalse();
100
101
inputElement = driver.findElement(By.xpath("//input[@id='working']"));
102
assertThat(inputElement.isEnabled()).isTrue();
103
}
104
105
@Test
106
void testElementsShouldBeDisabledIfTheyAreDisabledUsingRandomDisabledStrings() {
107
driver.get(pages.formPage);
108
WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));
109
assertThat(disabledTextElement1.isEnabled()).isFalse();
110
111
WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));
112
assertThat(disabledTextElement2.isEnabled()).isFalse();
113
114
WebElement disabledSubmitElement = driver.findElement(By.id("disabledSubmitElement"));
115
assertThat(disabledSubmitElement.isEnabled()).isFalse();
116
}
117
118
@Test
119
@NotYetImplemented(SAFARI)
120
public void testShouldThrowExceptionIfSendingKeysToElementDisabledUsingRandomDisabledStrings() {
121
driver.get(pages.formPage);
122
WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));
123
assertThatExceptionOfType(InvalidElementStateException.class)
124
.isThrownBy(() -> disabledTextElement1.sendKeys("foo"));
125
assertThat(disabledTextElement1.getText()).isEmpty();
126
127
WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));
128
assertThatExceptionOfType(InvalidElementStateException.class)
129
.isThrownBy(() -> disabledTextElement2.sendKeys("bar"));
130
assertThat(disabledTextElement2.getText()).isEmpty();
131
}
132
133
@Test
134
void testShouldIndicateWhenATextAreaIsDisabled() {
135
driver.get(pages.formPage);
136
WebElement textArea = driver.findElement(By.xpath("//textarea[@id='notWorkingArea']"));
137
assertThat(textArea.isEnabled()).isFalse();
138
}
139
140
@Test
141
void testShouldIndicateWhenASelectIsDisabled() {
142
driver.get(pages.formPage);
143
144
WebElement enabled = driver.findElement(By.name("selectomatic"));
145
WebElement disabled = driver.findElement(By.name("no-select"));
146
147
assertThat(enabled.isEnabled()).isTrue();
148
assertThat(disabled.isEnabled()).isFalse();
149
}
150
151
@Test
152
void testShouldReturnTheValueOfSelectedForOptionsOnlyIfTheyAreSelected() {
153
driver.get(pages.formPage);
154
WebElement selectBox = driver.findElement(By.xpath("//select[@name='selectomatic']"));
155
List<WebElement> options = selectBox.findElements(By.tagName("option"));
156
WebElement one = options.get(0);
157
WebElement two = options.get(1);
158
assertThat(one.isSelected()).isTrue();
159
assertThat(two.isSelected()).isFalse();
160
assertThat(one.getDomAttribute("selected")).isEqualTo("true");
161
assertThat(two.getDomAttribute("selected")).isNull();
162
}
163
164
@Test
165
void testShouldReturnValueOfClassAttributeOfAnElement() {
166
driver.get(pages.xhtmlTestPage);
167
168
WebElement heading = driver.findElement(By.xpath("//h1"));
169
String className = heading.getDomAttribute("class");
170
171
assertThat(className).isEqualTo("header");
172
}
173
174
@Test
175
void testShouldNotReturnTheContentsOfATextAreaAsItsValue() {
176
driver.get(pages.formPage);
177
String value = driver.findElement(By.id("withText")).getDomAttribute("value");
178
assertThat(value).isNull();
179
}
180
181
@Test
182
void testShouldNotReturnInnerHtmlProperty() {
183
driver.get(pages.simpleTestPage);
184
String html = driver.findElement(By.id("wrappingtext")).getDomAttribute("innerHTML");
185
assertThat(html).isNull();
186
}
187
188
@Test
189
void testShouldTreatReadonlyAsAValue() {
190
driver.get(pages.formPage);
191
192
WebElement element = driver.findElement(By.name("readonly"));
193
String readonly = element.getDomAttribute("readonly");
194
195
assertThat(readonly).isNotNull();
196
197
WebElement textInput = driver.findElement(By.name("x"));
198
String notReadonly = textInput.getDomAttribute("readonly");
199
200
assertThat(readonly).isNotEqualTo(notReadonly);
201
}
202
203
@Test
204
void testShouldNotReturnTextContentProperty() {
205
driver.get(pages.simpleTestPage);
206
WebElement element = driver.findElement(By.id("hiddenline"));
207
assertThat(element.getDomAttribute("textContent")).isNull();
208
}
209
210
@Test
211
void testShouldGetNumericAttribute() {
212
driver.get(pages.formPage);
213
WebElement element = driver.findElement(By.id("withText"));
214
assertThat(element.getDomAttribute("rows")).isEqualTo("5");
215
}
216
217
@Test
218
void testCanReturnATextApproximationOfTheStyleAttribute() {
219
driver.get(pages.javascriptPage);
220
221
String style = driver.findElement(By.id("red-item")).getDomAttribute("style");
222
223
assertThat(style).containsIgnoringCase("background-color");
224
}
225
226
@Test
227
void testShouldCorrectlyReportValueOfColspan() {
228
driver.get(pages.tables);
229
230
WebElement th1 = driver.findElement(By.id("th1"));
231
WebElement td2 = driver.findElement(By.id("td2"));
232
233
assertThat(th1.getDomAttribute("id")).isEqualTo("th1");
234
assertThat(th1.getDomAttribute("colspan")).isEqualTo("3");
235
236
assertThat(td2.getDomAttribute("id")).isEqualTo("td2");
237
assertThat(td2.getDomAttribute("colspan")).isEqualTo("2");
238
}
239
240
// This is a test-case re-creating issue 900.
241
@Test
242
void testShouldReturnValueOfOnClickAttribute() {
243
driver.get(pages.javascriptPage);
244
245
WebElement mouseclickDiv = driver.findElement(By.id("mouseclick"));
246
247
String onClickValue = mouseclickDiv.getDomAttribute("onclick");
248
String expectedOnClickValue = "displayMessage('mouse click');";
249
assertThat(onClickValue)
250
.as("Javascript code")
251
.isIn(
252
"javascript:" + expectedOnClickValue, // Non-IE
253
"function anonymous()\n{\n" + expectedOnClickValue + "\n}", // IE
254
"function onclick()\n{\n" + expectedOnClickValue + "\n}"); // IE
255
256
WebElement mousedownDiv = driver.findElement(By.id("mousedown"));
257
assertThat(mousedownDiv.getDomAttribute("onclick")).isNull();
258
}
259
260
@Test
261
void testgetDomAttributeDoesNotReturnAnObjectForSvgProperties() {
262
driver.get(pages.svgPage);
263
WebElement svgElement = driver.findElement(By.id("rotate"));
264
assertThat(svgElement.getDomAttribute("transform")).isEqualTo("rotate(30)");
265
}
266
267
@Test
268
void testCanRetrieveTheCurrentValueOfATextFormFieldWithPresetText() {
269
driver.get(pages.formPage);
270
WebElement element = driver.findElement(By.id("inputWithText"));
271
assertThat(element.getDomAttribute("value")).isEqualTo("Example text");
272
element.sendKeys("[email protected]");
273
assertThat(element.getDomAttribute("value")).isEqualTo("Example text");
274
}
275
276
@Test
277
void testShouldNotReturnTextOfATextArea() {
278
driver.get(pages.formPage);
279
WebElement element = driver.findElement(By.id("withText"));
280
assertThat(element.getDomAttribute("value")).isNull();
281
}
282
283
@Test
284
void testShouldReturnNullForNonPresentBooleanAttributes() {
285
driver.get(pages.booleanAttributes);
286
WebElement element1 = driver.findElement(By.id("working"));
287
assertThat(element1.getDomAttribute("required")).isNull();
288
WebElement element2 = driver.findElement(By.id("wallace"));
289
assertThat(element2.getDomAttribute("nowrap")).isNull();
290
}
291
292
@Test
293
@NotYetImplemented(value = CHROME, reason = "It returns a property")
294
@NotYetImplemented(EDGE)
295
@NotYetImplemented(FIREFOX)
296
public void testShouldReturnEmptyStringForPresentBooleanAttributes() {
297
driver.get(pages.booleanAttributes);
298
WebElement element1 = driver.findElement(By.id("emailRequired"));
299
assertThat(element1.getDomAttribute("required")).isEmpty();
300
WebElement element2 = driver.findElement(By.id("emptyTextAreaRequired"));
301
assertThat(element2.getDomAttribute("required")).isEqualTo("required");
302
WebElement element3 = driver.findElement(By.id("inputRequired"));
303
assertThat(element3.getDomAttribute("required")).isEmpty();
304
WebElement element4 = driver.findElement(By.id("textAreaRequired"));
305
assertThat(element4.getDomAttribute("required")).isEqualTo("false");
306
WebElement element5 = driver.findElement(By.id("unwrappable"));
307
assertThat(element5.getDomAttribute("nowrap")).isEmpty();
308
}
309
310
@Test
311
void testMultipleAttributeShouldBeNullWhenNotSet() {
312
driver.get(pages.selectPage);
313
WebElement element = driver.findElement(By.id("selectWithoutMultiple"));
314
assertThat(element.getDomAttribute("multiple")).isNull();
315
}
316
317
@Test
318
void testMultipleAttributeShouldBeTrueWhenSet() {
319
driver.get(pages.selectPage);
320
WebElement element = driver.findElement(By.id("selectWithMultipleEqualsMultiple"));
321
assertThat(element.getDomAttribute("multiple")).isEqualTo("true");
322
}
323
324
@Test
325
void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsBlank() {
326
driver.get(pages.selectPage);
327
WebElement element = driver.findElement(By.id("selectWithEmptyStringMultiple"));
328
assertThat(element.getDomAttribute("multiple")).isEqualTo("true");
329
}
330
331
@Test
332
void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithoutAValue() {
333
driver.get(pages.selectPage);
334
WebElement element = driver.findElement(By.id("selectWithMultipleWithoutValue"));
335
assertThat(element.getDomAttribute("multiple")).isEqualTo("true");
336
}
337
338
@Test
339
void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsSomethingElse() {
340
driver.get(pages.selectPage);
341
WebElement element = driver.findElement(By.id("selectWithRandomMultipleValue"));
342
assertThat(element.getDomAttribute("multiple")).isEqualTo("true");
343
}
344
345
@Test
346
void shouldTreatContenteditableAsEnumeratedButNotBoolean() {
347
checkEnumeratedAttribute("contenteditable", "true", "false", "yes", "no", "", "blabla");
348
}
349
350
@Test
351
@NotYetImplemented(IE)
352
@NotYetImplemented(SAFARI)
353
public void shouldTreatDraggableAsEnumeratedButNotBoolean() {
354
checkEnumeratedAttribute("draggable", "true", "false", "yes", "no", "", "blabla");
355
}
356
357
private void checkEnumeratedAttribute(String name, String... values) {
358
List.of(values)
359
.forEach(
360
value -> {
361
driver.get(
362
appServer.create(
363
new Page()
364
.withBody(String.format("<div id=\"attr\" %s=\"%s\">", name, value))));
365
assertThat(driver.findElement(By.id("attr")).getDomAttribute(name)).isEqualTo(value);
366
});
367
368
driver.get(appServer.create(new Page().withBody(String.format("<div id=\"attr\" %s>", name))));
369
assertThat(driver.findElement(By.id("attr")).getDomAttribute(name)).isEmpty();
370
371
driver.get(appServer.create(new Page().withBody("<div id=\"attr\">")));
372
assertThat(driver.findElement(By.id("attr")).getDomAttribute(name)).isNull();
373
}
374
}
375
376