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