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