Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ElementDomPropertyTest.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 org.assertj.core.api.Assertions.assertThat;
21
22
import java.util.List;
23
import org.junit.jupiter.api.Test;
24
import org.openqa.selenium.support.ui.ExpectedConditions;
25
import org.openqa.selenium.testing.JupiterTestBase;
26
27
class ElementDomPropertyTest extends JupiterTestBase {
28
29
@Test
30
void testShouldReturnNullWhenGettingTheValueOfAPropertyThatDoesNotExist() {
31
driver.get(pages.simpleTestPage);
32
WebElement head = driver.findElement(By.xpath("/html"));
33
assertThat(head.getDomProperty("cheese")).isNull();
34
}
35
36
@Test
37
void testShouldReturnAnAbsoluteUrlWhenGettingSrcAttributeOfAValidImgTag() {
38
driver.get(pages.simpleTestPage);
39
WebElement img = driver.findElement(By.id("validImgTag"));
40
assertThat(img.getDomProperty("src")).isEqualTo(appServer.whereIs("icon.gif"));
41
}
42
43
@Test
44
void testShouldReturnAnAbsoluteUrlWhenGettingHrefAttributeOfAValidAnchorTag() {
45
driver.get(pages.simpleTestPage);
46
WebElement img = driver.findElement(By.id("validAnchorTag"));
47
assertThat(img.getDomProperty("href")).isEqualTo(appServer.whereIs("icon.gif"));
48
}
49
50
@Test
51
void testShouldReturnTheValueOfTheIndexAttributeEvenIfItIsMissing() {
52
driver.get(pages.formPage);
53
WebElement multiSelect = driver.findElement(By.id("multi"));
54
List<WebElement> options = multiSelect.findElements(By.tagName("option"));
55
assertThat(options.get(1).getDomProperty("index")).isEqualTo("1");
56
}
57
58
@Test
59
void testShouldReturnTheValueOfCheckedForACheckboxOnlyIfItIsChecked() {
60
driver.get(pages.formPage);
61
WebElement checkbox = driver.findElement(By.xpath("//input[@id='checky']"));
62
assertThat(checkbox.getDomProperty("checked")).isEqualTo("false");
63
checkbox.click();
64
assertThat(checkbox.getDomProperty("checked")).isEqualTo("true");
65
}
66
67
@Test
68
void testShouldReturnTheValueOfSelectedForOptionsOnlyIfTheyAreSelected() {
69
driver.get(pages.formPage);
70
WebElement selectBox = driver.findElement(By.xpath("//select[@name='selectomatic']"));
71
List<WebElement> options = selectBox.findElements(By.tagName("option"));
72
WebElement one = options.get(0);
73
WebElement two = options.get(1);
74
assertThat(one.isSelected()).isTrue();
75
assertThat(two.isSelected()).isFalse();
76
assertThat(one.getDomProperty("selected")).isEqualTo("true");
77
assertThat(two.getDomProperty("selected")).isEqualTo("false");
78
assertThat(selectBox.getDomProperty("selectedIndex")).isEqualTo("0");
79
}
80
81
@Test
82
public void testShouldGetClassPropertiesOfAnElement() {
83
driver.get(pages.xhtmlTestPage);
84
WebElement heading = driver.findElement(By.cssSelector(".nameA"));
85
assertThat(heading.getDomProperty("class")).isNull();
86
assertThat(heading.getDomProperty("className")).isEqualTo("nameA nameBnoise nameC");
87
assertThat(heading.getDomProperty("classList")).isEqualTo("[nameA, nameBnoise, nameC]");
88
}
89
90
@Test
91
void testShouldReturnTheContentsOfATextAreaAsItsValue() {
92
driver.get(pages.formPage);
93
WebElement withText = driver.findElement(By.id("withText"));
94
assertThat(withText.getDomProperty("value")).isEqualTo("Example text");
95
}
96
97
@Test
98
void testShouldReturnInnerHtml() {
99
driver.get(pages.simpleTestPage);
100
WebElement wrapping = driver.findElement(By.id("wrappingtext"));
101
assertThat(wrapping.getDomProperty("innerHTML")).contains("<tbody>");
102
}
103
104
@Test
105
void testShouldReturnHiddenTextForTextContentProperty() {
106
driver.get(pages.simpleTestPage);
107
WebElement element = driver.findElement(By.id("hiddenline"));
108
assertThat(element.getDomProperty("textContent")).isEqualTo("A hidden line of text");
109
}
110
111
@Test
112
void testShouldGetNumericProperty() {
113
driver.get(pages.formPage);
114
WebElement element = driver.findElement(By.id("withText"));
115
assertThat(element.getDomProperty("rows")).isEqualTo("5");
116
}
117
118
@Test
119
public void testCanReturnATextApproximationOfTheStyleProperty() {
120
driver.get(pages.javascriptPage);
121
WebElement element = driver.findElement(By.id("red-item"));
122
assertThat(element.getDomProperty("style").toLowerCase()).contains("background-color");
123
}
124
125
@Test
126
void testPropertyNamesAreCaseSensitive() {
127
driver.get(pages.tables);
128
WebElement th1 = driver.findElement(By.id("th1"));
129
assertThat(th1.getDomProperty("colspan")).isNull();
130
assertThat(th1.getDomProperty("COLSPAN")).isNull();
131
assertThat(th1.getDomProperty("colSpan")).isEqualTo("3");
132
}
133
134
@Test
135
void testCanRetrieveTheCurrentValueOfATextFormField_textInput() {
136
driver.get(pages.formPage);
137
WebElement element = driver.findElement(By.id("working"));
138
assertThat(element.getDomProperty("value")).isEmpty();
139
element.sendKeys("hello world");
140
shortWait.until(ExpectedConditions.domPropertyToBe(element, "value", "hello world"));
141
}
142
143
@Test
144
void testCanRetrieveTheCurrentValueOfATextFormField_emailInput() {
145
driver.get(pages.formPage);
146
WebElement element = driver.findElement(By.id("email"));
147
assertThat(element.getDomProperty("value")).isEmpty();
148
element.sendKeys("[email protected]");
149
shortWait.until(ExpectedConditions.domPropertyToBe(element, "value", "[email protected]"));
150
}
151
152
@Test
153
void testCanRetrieveTheCurrentValueOfATextFormField_textArea() {
154
driver.get(pages.formPage);
155
WebElement element = driver.findElement(By.id("emptyTextArea"));
156
assertThat(element.getDomProperty("value")).isEmpty();
157
element.sendKeys("hello world");
158
shortWait.until(ExpectedConditions.domPropertyToBe(element, "value", "hello world"));
159
}
160
161
@Test
162
void testMultiplePropertyShouldBeTrueWhenSelectHasMultipleWithValueAsBlank() {
163
driver.get(pages.selectPage);
164
WebElement element = driver.findElement(By.id("selectWithEmptyStringMultiple"));
165
assertThat(element.getDomProperty("multiple")).isEqualTo("true");
166
}
167
168
@Test
169
void testMultiplePropertyShouldBeTrueWhenSelectHasMultipleWithoutAValue() {
170
driver.get(pages.selectPage);
171
WebElement element = driver.findElement(By.id("selectWithMultipleWithoutValue"));
172
assertThat(element.getDomProperty("multiple")).isEqualTo("true");
173
}
174
175
@Test
176
void testMultiplePropertyShouldBeTrueWhenSelectHasMultipleWithValueAsSomethingElse() {
177
driver.get(pages.selectPage);
178
WebElement element = driver.findElement(By.id("selectWithRandomMultipleValue"));
179
assertThat(element.getDomProperty("multiple")).isEqualTo("true");
180
}
181
182
@Test
183
void testGetValueOfUserDefinedProperty() {
184
driver.get(pages.userDefinedProperty);
185
WebElement element = driver.findElement(By.id("d"));
186
assertThat(element.getDomProperty("dynamicProperty")).isEqualTo("sampleValue");
187
}
188
}
189
190