Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/PositionAndSizeTest.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
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
22
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
23
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
24
import static org.openqa.selenium.testing.drivers.Browser.IE;
25
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
26
27
import org.junit.jupiter.api.Test;
28
import org.openqa.selenium.interactions.Locatable;
29
import org.openqa.selenium.testing.Ignore;
30
import org.openqa.selenium.testing.JupiterTestBase;
31
import org.openqa.selenium.testing.NotYetImplemented;
32
import org.openqa.selenium.testing.SwitchToTopAfterTest;
33
34
class PositionAndSizeTest extends JupiterTestBase {
35
36
@Test
37
void testShouldBeAbleToDetermineTheLocationOfAnElement() {
38
driver.get(pages.xhtmlTestPage);
39
40
WebElement element = driver.findElement(By.id("username"));
41
Point location = element.getLocation();
42
43
assertThat(location.getX()).isPositive();
44
assertThat(location.getY()).isPositive();
45
}
46
47
@Test
48
void testShouldGetCoordinatesOfAnElement() {
49
driver.get(appServer.whereIs("coordinates_tests/simple_page.html"));
50
assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(10, 10));
51
assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));
52
}
53
54
@Test
55
void testShouldGetCoordinatesOfAnEmptyElement() {
56
driver.get(appServer.whereIs("coordinates_tests/page_with_empty_element.html"));
57
assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(10, 10));
58
assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));
59
}
60
61
@Test
62
void testShouldGetCoordinatesOfATransparentElement() {
63
driver.get(appServer.whereIs("coordinates_tests/page_with_transparent_element.html"));
64
assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(10, 10));
65
assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));
66
}
67
68
@Test
69
@NotYetImplemented(SAFARI)
70
public void testShouldGetCoordinatesOfAHiddenElement() {
71
driver.get(appServer.whereIs("coordinates_tests/page_with_hidden_element.html"));
72
assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(10, 10));
73
assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));
74
}
75
76
@Test
77
@NotYetImplemented(SAFARI)
78
public void testShouldGetCoordinatesOfAnInvisibleElement() {
79
driver.get(appServer.whereIs("coordinates_tests/page_with_invisible_element.html"));
80
assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(0, 0));
81
assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(0, 0));
82
}
83
84
@Test
85
void testShouldScrollPageAndGetCoordinatesOfAnElementThatIsOutOfViewPort() {
86
driver.get(appServer.whereIs("coordinates_tests/page_with_element_out_of_view.html"));
87
int windowHeight = driver.manage().window().getSize().getHeight();
88
Point location = getLocationInViewPort(By.id("box"));
89
assertThat(location.getX()).isEqualTo(10);
90
assertThat(location.getY()).isGreaterThanOrEqualTo(0);
91
assertThat(location.getY()).isLessThanOrEqualTo(windowHeight - 100);
92
assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 5010));
93
}
94
95
@SwitchToTopAfterTest
96
@Test
97
@NotYetImplemented(SAFARI)
98
public void testShouldGetCoordinatesOfAnElementInAFrame() {
99
driver.get(appServer.whereIs("coordinates_tests/element_in_frame.html"));
100
driver.switchTo().frame("ifr");
101
WebElement box = driver.findElement(By.id("box"));
102
assertThat(box.getLocation()).isEqualTo(new Point(10, 10));
103
assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));
104
}
105
106
@SwitchToTopAfterTest
107
@Test
108
@NotYetImplemented(SAFARI)
109
@NotYetImplemented(FIREFOX)
110
@NotYetImplemented(IE)
111
@NotYetImplemented(CHROME)
112
@NotYetImplemented(EDGE)
113
public void testShouldGetCoordinatesInViewPortOfAnElementInAFrame() {
114
driver.get(appServer.whereIs("coordinates_tests/element_in_frame.html"));
115
driver.switchTo().frame("ifr");
116
assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(25, 25));
117
assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));
118
}
119
120
@SwitchToTopAfterTest
121
@Test
122
@NotYetImplemented(SAFARI)
123
@NotYetImplemented(FIREFOX)
124
@NotYetImplemented(IE)
125
@NotYetImplemented(CHROME)
126
@NotYetImplemented(EDGE)
127
public void testShouldGetCoordinatesInViewPortOfAnElementInANestedFrame() {
128
driver.get(appServer.whereIs("coordinates_tests/element_in_nested_frame.html"));
129
driver.switchTo().frame("ifr");
130
driver.switchTo().frame("ifr");
131
assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(40, 40));
132
assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));
133
}
134
135
@Test
136
void testShouldGetCoordinatesOfAnElementWithFixedPosition() {
137
driver.get(appServer.whereIs("coordinates_tests/page_with_fixed_element.html"));
138
assertThat(getLocationInViewPort(By.id("fixed")).getY()).isZero();
139
assertThat(getLocationOnPage(By.id("fixed")).getY()).isZero();
140
141
driver.findElement(By.id("bottom")).click();
142
assertThat(getLocationInViewPort(By.id("fixed")).getY()).isZero();
143
assertThat(getLocationOnPage(By.id("fixed")).getY()).isPositive();
144
}
145
146
@Test
147
void testShouldCorrectlyIdentifyThatAnElementHasWidthAndHeight() {
148
driver.get(pages.xhtmlTestPage);
149
150
WebElement shrinko = driver.findElement(By.id("linkId"));
151
Dimension size = shrinko.getSize();
152
assertThat(size.width).isPositive();
153
assertThat(size.height).isPositive();
154
}
155
156
// TODO: This test's value seems dubious at best. The CSS spec does not define how browsers
157
// should handle sub-pixel rendering, and every browser seems to be different anyhow:
158
// http://ejohn.org/blog/sub-pixel-problems-in-css/
159
@Test
160
@Ignore(IE)
161
@NotYetImplemented(value = CHROME, reason = "WebKit bug 28804")
162
@NotYetImplemented(value = EDGE, reason = "WebKit bug 28804")
163
@NotYetImplemented(SAFARI)
164
@Ignore(FIREFOX)
165
public void testShouldHandleNonIntegerPositionAndSize() {
166
driver.get(pages.rectanglesPage);
167
168
WebElement r2 = driver.findElement(By.id("r2"));
169
String left = r2.getCssValue("left");
170
assertThat(left).startsWith("10.9");
171
String top = r2.getCssValue("top");
172
assertThat(top).startsWith("10.1");
173
assertThat(r2.getLocation()).isEqualTo(new Point(11, 10));
174
String width = r2.getCssValue("width");
175
assertThat(width).startsWith("48.6");
176
String height = r2.getCssValue("height");
177
assertThat(height).startsWith("49.3");
178
assertThat(r2.getSize()).isEqualTo(new Dimension(49, 49));
179
}
180
181
private Point getLocationInViewPort(By locator) {
182
WebElement element = driver.findElement(locator);
183
return ((Locatable) element).getCoordinates().inViewPort();
184
}
185
186
private Point getLocationOnPage(By locator) {
187
WebElement element = driver.findElement(locator);
188
return ((Locatable) element).getCoordinates().onPage();
189
}
190
}
191
192