Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ElementAttributeTest.java
4011 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.support.ui.ExpectedConditions;
32
import org.openqa.selenium.testing.JupiterTestBase;
33
import org.openqa.selenium.testing.NotYetImplemented;
34
35
class ElementAttributeTest 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.getAttribute("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.getAttribute("src");
50
assertThat(attribute).isNull();
51
}
52
53
@Test
54
void testShouldReturnAnAbsoluteUrlWhenGettingSrcAttributeOfAValidImgTag() {
55
driver.get(pages.simpleTestPage);
56
WebElement img = driver.findElement(By.id("validImgTag"));
57
String attribute = img.getAttribute("src");
58
assertThat(attribute).isEqualTo(appServer.whereIs("icon.gif"));
59
}
60
61
@Test
62
void testShouldReturnAnAbsoluteUrlWhenGettingHrefAttributeOfAValidAnchorTag() {
63
driver.get(pages.simpleTestPage);
64
WebElement img = driver.findElement(By.id("validAnchorTag"));
65
String attribute = img.getAttribute("href");
66
assertThat(attribute).isEqualTo(appServer.whereIs("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.getAttribute("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.getAttribute("disabled")).isNull();
81
assertThat(inputElement.isEnabled()).isTrue();
82
83
WebElement pElement = driver.findElement(By.id("peas"));
84
assertThat(pElement.getAttribute("disabled")).isNull();
85
assertThat(pElement.isEnabled()).isTrue();
86
}
87
88
@Test
89
void testShouldReturnTheValueOfTheIndexAttributeEvenIfItIsMissing() {
90
driver.get(pages.formPage);
91
92
WebElement multiSelect = driver.findElement(By.id("multi"));
93
List<WebElement> options = multiSelect.findElements(By.tagName("option"));
94
assertThat(options.get(1).getAttribute("index")).isEqualTo("1");
95
}
96
97
@Test
98
void testShouldIndicateTheElementsThatAreDisabledAreNotEnabled() {
99
driver.get(pages.formPage);
100
WebElement inputElement = driver.findElement(By.xpath("//input[@id='notWorking']"));
101
assertThat(inputElement.isEnabled()).isFalse();
102
103
inputElement = driver.findElement(By.xpath("//input[@id='working']"));
104
assertThat(inputElement.isEnabled()).isTrue();
105
}
106
107
@Test
108
void testElementsShouldBeDisabledIfTheyAreDisabledUsingRandomDisabledStrings() {
109
driver.get(pages.formPage);
110
WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));
111
assertThat(disabledTextElement1.isEnabled()).isFalse();
112
113
WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));
114
assertThat(disabledTextElement2.isEnabled()).isFalse();
115
116
WebElement disabledSubmitElement = driver.findElement(By.id("disabledSubmitElement"));
117
assertThat(disabledSubmitElement.isEnabled()).isFalse();
118
}
119
120
@Test
121
@NotYetImplemented(SAFARI)
122
public void testShouldThrowExceptionIfSendingKeysToElementDisabledUsingRandomDisabledStrings() {
123
driver.get(pages.formPage);
124
WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));
125
assertThatExceptionOfType(InvalidElementStateException.class)
126
.isThrownBy(() -> disabledTextElement1.sendKeys("foo"));
127
assertThat(disabledTextElement1.getText()).isEmpty();
128
129
WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));
130
assertThatExceptionOfType(InvalidElementStateException.class)
131
.isThrownBy(() -> disabledTextElement2.sendKeys("bar"));
132
assertThat(disabledTextElement2.getText()).isEmpty();
133
}
134
135
@Test
136
void testShouldIndicateWhenATextAreaIsDisabled() {
137
driver.get(pages.formPage);
138
WebElement textArea = driver.findElement(By.xpath("//textarea[@id='notWorkingArea']"));
139
assertThat(textArea.isEnabled()).isFalse();
140
}
141
142
@Test
143
void testShouldIndicateWhenASelectIsDisabled() {
144
driver.get(pages.formPage);
145
146
WebElement enabled = driver.findElement(By.name("selectomatic"));
147
WebElement disabled = driver.findElement(By.name("no-select"));
148
149
assertThat(enabled.isEnabled()).isTrue();
150
assertThat(disabled.isEnabled()).isFalse();
151
}
152
153
@Test
154
void testShouldReturnTheValueOfCheckedForACheckboxOnlyIfItIsChecked() {
155
driver.get(pages.formPage);
156
WebElement checkbox = driver.findElement(By.xpath("//input[@id='checky']"));
157
assertThat(checkbox.getAttribute("checked")).isNull();
158
checkbox.click();
159
assertThat(checkbox.getAttribute("checked")).isEqualTo("true");
160
}
161
162
@Test
163
void testShouldOnlyReturnTheValueOfSelectedForRadioButtonsIfItIsSet() {
164
driver.get(pages.formPage);
165
WebElement neverSelected = driver.findElement(By.id("cheese"));
166
WebElement initiallyNotSelected = driver.findElement(By.id("peas"));
167
WebElement initiallySelected = driver.findElement(By.id("cheese_and_peas"));
168
169
assertThat(neverSelected.getAttribute("selected")).isNull();
170
assertThat(initiallyNotSelected.getAttribute("selected")).isNull();
171
assertThat(initiallySelected.getAttribute("selected")).isEqualTo("true");
172
173
initiallyNotSelected.click();
174
assertThat(neverSelected.getAttribute("selected")).isNull();
175
assertThat(initiallyNotSelected.getAttribute("selected")).isEqualTo("true");
176
assertThat(initiallySelected.getAttribute("selected")).isNull();
177
}
178
179
@Test
180
void testShouldReturnTheValueOfSelectedForOptionsOnlyIfTheyAreSelected() {
181
driver.get(pages.formPage);
182
WebElement selectBox = driver.findElement(By.xpath("//select[@name='selectomatic']"));
183
List<WebElement> options = selectBox.findElements(By.tagName("option"));
184
WebElement one = options.get(0);
185
WebElement two = options.get(1);
186
assertThat(one.isSelected()).isTrue();
187
assertThat(two.isSelected()).isFalse();
188
assertThat(one.getAttribute("selected")).isEqualTo("true");
189
assertThat(two.getAttribute("selected")).isNull();
190
}
191
192
@Test
193
void testShouldReturnValueOfClassAttributeOfAnElement() {
194
driver.get(pages.xhtmlTestPage);
195
196
WebElement heading = driver.findElement(By.xpath("//h1"));
197
String className = heading.getAttribute("class");
198
199
assertThat(className).isEqualTo("header");
200
}
201
202
@Test
203
void testShouldReturnTheContentsOfATextAreaAsItsValue() {
204
driver.get(pages.formPage);
205
206
String value = driver.findElement(By.id("withText")).getAttribute("value");
207
208
assertThat(value).isEqualTo("Example text");
209
}
210
211
@Test
212
void testShouldReturnInnerHtml() {
213
driver.get(pages.simpleTestPage);
214
215
String html = driver.findElement(By.id("wrappingtext")).getAttribute("innerHTML");
216
assertThat(html).contains("<tbody>");
217
}
218
219
@Test
220
void testShouldTreatReadonlyAsAValue() {
221
driver.get(pages.formPage);
222
223
WebElement element = driver.findElement(By.name("readonly"));
224
String readonly = element.getAttribute("readonly");
225
226
assertThat(readonly).isNotNull();
227
228
WebElement textInput = driver.findElement(By.name("x"));
229
String notReadonly = textInput.getAttribute("readonly");
230
231
assertThat(readonly).isNotEqualTo(notReadonly);
232
}
233
234
@Test
235
void testShouldReturnHiddenTextForTextContentAttribute() {
236
driver.get(pages.simpleTestPage);
237
238
WebElement element = driver.findElement(By.id("hiddenline"));
239
String textContent = element.getAttribute("textContent");
240
241
assertThat(textContent).isEqualTo("A hidden line of text");
242
}
243
244
@Test
245
void testShouldGetNumericAttribute() {
246
driver.get(pages.formPage);
247
WebElement element = driver.findElement(By.id("withText"));
248
assertThat(element.getAttribute("rows")).isEqualTo("5");
249
}
250
251
@Test
252
void testCanReturnATextApproximationOfTheStyleAttribute() {
253
driver.get(pages.javascriptPage);
254
255
String style = driver.findElement(By.id("red-item")).getAttribute("style");
256
257
assertThat(style).containsIgnoringCase("background-color");
258
}
259
260
@Test
261
void testShouldCorrectlyReportValueOfColspan() {
262
driver.get(pages.tables);
263
264
WebElement th1 = driver.findElement(By.id("th1"));
265
WebElement td2 = driver.findElement(By.id("td2"));
266
267
assertThat(th1.getAttribute("id")).isEqualTo("th1");
268
assertThat(th1.getAttribute("colspan")).isEqualTo("3");
269
270
assertThat(td2.getAttribute("id")).isEqualTo("td2");
271
assertThat(td2.getAttribute("colspan")).isEqualTo("2");
272
}
273
274
// This is a test-case re-creating issue 900.
275
@Test
276
void testShouldReturnValueOfOnClickAttribute() {
277
driver.get(pages.javascriptPage);
278
279
WebElement mouseclickDiv = driver.findElement(By.id("mouseclick"));
280
281
String onClickValue = mouseclickDiv.getAttribute("onclick");
282
String expectedOnClickValue = "displayMessage('mouse click');";
283
assertThat(onClickValue)
284
.as("Javascript code")
285
.isIn(
286
"javascript:" + expectedOnClickValue, // Non-IE
287
"function anonymous()\n{\n" + expectedOnClickValue + "\n}", // IE
288
"function onclick()\n{\n" + expectedOnClickValue + "\n}"); // IE
289
290
WebElement mousedownDiv = driver.findElement(By.id("mousedown"));
291
assertThat(mousedownDiv.getAttribute("onclick")).isNull();
292
}
293
294
@Test
295
void testGetAttributeDoesNotReturnAnObjectForSvgProperties() {
296
driver.get(pages.svgPage);
297
WebElement svgElement = driver.findElement(By.id("rotate"));
298
assertThat(svgElement.getAttribute("transform")).isEqualTo("rotate(30)");
299
}
300
301
@Test
302
void testCanRetrieveTheCurrentValueOfATextFormField_textInput() {
303
driver.get(pages.formPage);
304
WebElement element = driver.findElement(By.id("working"));
305
assertThat(element.getAttribute("value")).isEmpty();
306
element.sendKeys("hello world");
307
shortWait.until(ExpectedConditions.attributeToBe(element, "value", "hello world"));
308
}
309
310
@Test
311
void testCanRetrieveTheCurrentValueOfATextFormField_emailInput() {
312
driver.get(pages.formPage);
313
WebElement element = driver.findElement(By.id("email"));
314
assertThat(element.getAttribute("value")).isEmpty();
315
element.sendKeys("[email protected]");
316
shortWait.until(ExpectedConditions.attributeToBe(element, "value", "[email protected]"));
317
}
318
319
@Test
320
void testCanRetrieveTheCurrentValueOfATextFormField_textArea() {
321
driver.get(pages.formPage);
322
WebElement element = driver.findElement(By.id("emptyTextArea"));
323
assertThat(element.getAttribute("value")).isEmpty();
324
element.sendKeys("hello world");
325
shortWait.until(ExpectedConditions.attributeToBe(element, "value", "hello world"));
326
}
327
328
@Test
329
void testShouldReturnNullForNonPresentBooleanAttributes() {
330
driver.get(pages.booleanAttributes);
331
WebElement element1 = driver.findElement(By.id("working"));
332
assertThat(element1.getAttribute("required")).isNull();
333
WebElement element2 = driver.findElement(By.id("wallace"));
334
assertThat(element2.getAttribute("nowrap")).isNull();
335
}
336
337
@Test
338
void testShouldReturnTrueForPresentBooleanAttributes() {
339
driver.get(pages.booleanAttributes);
340
WebElement element1 = driver.findElement(By.id("emailRequired"));
341
assertThat(element1.getAttribute("required")).isEqualTo("true");
342
WebElement element2 = driver.findElement(By.id("emptyTextAreaRequired"));
343
assertThat(element2.getAttribute("required")).isEqualTo("true");
344
WebElement element3 = driver.findElement(By.id("inputRequired"));
345
assertThat(element3.getAttribute("required")).isEqualTo("true");
346
WebElement element4 = driver.findElement(By.id("textAreaRequired"));
347
assertThat(element4.getAttribute("required")).isEqualTo("true");
348
WebElement element5 = driver.findElement(By.id("unwrappable"));
349
assertThat(element5.getAttribute("nowrap")).isEqualTo("true");
350
}
351
352
@Test
353
void testMultipleAttributeShouldBeNullWhenNotSet() {
354
driver.get(pages.selectPage);
355
WebElement element = driver.findElement(By.id("selectWithoutMultiple"));
356
assertThat(element.getAttribute("multiple")).isNull();
357
}
358
359
@Test
360
void testMultipleAttributeShouldBeTrueWhenSet() {
361
driver.get(pages.selectPage);
362
WebElement element = driver.findElement(By.id("selectWithMultipleEqualsMultiple"));
363
assertThat(element.getAttribute("multiple")).isEqualTo("true");
364
}
365
366
@Test
367
void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsBlank() {
368
driver.get(pages.selectPage);
369
WebElement element = driver.findElement(By.id("selectWithEmptyStringMultiple"));
370
assertThat(element.getAttribute("multiple")).isEqualTo("true");
371
}
372
373
@Test
374
void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithoutAValue() {
375
driver.get(pages.selectPage);
376
WebElement element = driver.findElement(By.id("selectWithMultipleWithoutValue"));
377
assertThat(element.getAttribute("multiple")).isEqualTo("true");
378
}
379
380
@Test
381
void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsSomethingElse() {
382
driver.get(pages.selectPage);
383
WebElement element = driver.findElement(By.id("selectWithRandomMultipleValue"));
384
assertThat(element.getAttribute("multiple")).isEqualTo("true");
385
}
386
387
@Test
388
void testGetAttributeOfUserDefinedProperty() {
389
driver.get(pages.userDefinedProperty);
390
WebElement element = driver.findElement(By.id("d"));
391
assertThat(element.getAttribute("dynamicProperty")).isEqualTo("sampleValue");
392
}
393
394
@Test
395
void shouldTreatContenteditableAsEnumeratedButNotBoolean() {
396
checkEnumeratedAttribute("contenteditable", "true", "false", "yes", "no", "", "blabla");
397
}
398
399
@Test
400
@NotYetImplemented(IE)
401
@NotYetImplemented(CHROME)
402
@NotYetImplemented(EDGE)
403
@NotYetImplemented(FIREFOX)
404
@NotYetImplemented(SAFARI)
405
public void shouldTreatDraggableAsEnumeratedButNotBoolean() {
406
checkEnumeratedAttribute("draggable", "true", "false", "yes", "no", "", "blabla");
407
}
408
409
private void checkEnumeratedAttribute(String name, String... values) {
410
List.of(values)
411
.forEach(
412
value -> {
413
driver.get(
414
appServer.create(
415
new Page()
416
.withBody(String.format("<div id=\"attr\" %s=\"%s\">", name, value))));
417
assertThat(driver.findElement(By.id("attr")).getAttribute(name)).isEqualTo(value);
418
});
419
420
driver.get(appServer.create(new Page().withBody(String.format("<div id=\"attr\" %s>", name))));
421
assertThat(driver.findElement(By.id("attr")).getAttribute(name)).isEmpty();
422
423
driver.get(appServer.create(new Page().withBody("<div id=\"attr\">")));
424
assertThat(driver.findElement(By.id("attr")).getAttribute(name)).isNull();
425
}
426
}
427
428