forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPositionAndSizeTest.cs
197 lines (173 loc) · 8.81 KB
/
PositionAndSizeTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// <copyright file="PositionAndSizeTest.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>
using NUnit.Framework;
using OpenQA.Selenium.Environment;
using System;
using System.Drawing;
namespace OpenQA.Selenium
{
[TestFixture]
public class PositionAndSizeTest : DriverTestFixture
{
[Test]
public void ShouldBeAbleToDetermineTheLocationOfAnElement()
{
driver.Url = xhtmlTestPage;
IWebElement element = driver.FindElement(By.Id("username"));
Point location = element.Location;
Assert.That(location.X, Is.GreaterThan(0));
Assert.That(location.Y, Is.GreaterThan(0));
}
[Test]
public void ShouldGetCoordinatesOfAnElement()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("coordinates_tests/simple_page.html");
Assert.That(GetLocationInViewPort(By.Id("box")), Is.EqualTo(new Point(10, 10)));
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(10, 10)));
}
[Test]
public void ShouldGetCoordinatesOfAnEmptyElement()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("coordinates_tests/page_with_empty_element.html");
Assert.That(GetLocationInViewPort(By.Id("box")), Is.EqualTo(new Point(10, 10)));
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(10, 10)));
}
[Test]
public void ShouldGetCoordinatesOfATransparentElement()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("coordinates_tests/page_with_transparent_element.html");
Assert.That(GetLocationInViewPort(By.Id("box")), Is.EqualTo(new Point(10, 10)));
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(10, 10)));
}
[Test]
public void ShouldGetCoordinatesOfAHiddenElement()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("coordinates_tests/page_with_hidden_element.html");
Assert.That(GetLocationInViewPort(By.Id("box")), Is.EqualTo(new Point(10, 10)));
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(10, 10)));
}
[Test]
public void ShouldGetCoordinatesOfAnInvisibleElement()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("coordinates_tests/page_with_invisible_element.html");
Assert.That(GetLocationInViewPort(By.Id("box")), Is.EqualTo(new Point(0, 0)));
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(0, 0)));
}
[Test]
public void ShouldScrollPageAndGetCoordinatesOfAnElementThatIsOutOfViewPort()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("coordinates_tests/page_with_element_out_of_view.html");
int windowHeight = driver.Manage().Window.Size.Height;
Point location = GetLocationInViewPort(By.Id("box"));
Assert.That(location.X, Is.EqualTo(10));
Assert.That(location.Y, Is.GreaterThanOrEqualTo(0));
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(10, 5010)));
// GetLocationInViewPort only works within the context of a single frame
// for W3C-spec compliant remote ends.
// Assert.That(location.Y, Is.LessThanOrEqualTo(windowHeight - 100));
}
[Test]
public void ShouldGetCoordinatesOfAnElementInAFrame()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("coordinates_tests/element_in_frame.html");
driver.SwitchTo().Frame("ifr");
IWebElement box = driver.FindElement(By.Id("box"));
Assert.That(box.Location, Is.EqualTo(new Point(10, 10)));
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(10, 10)));
}
[Test]
public void ShouldGetCoordinatesInViewPortOfAnElementInAFrame()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("coordinates_tests/element_in_frame.html");
driver.SwitchTo().Frame("ifr");
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(10, 10)));
// GetLocationInViewPort only works within the context of a single frame
// for W3C-spec compliant remote ends.
// Assert.That(GetLocationInViewPort(By.Id("box")), Is.EqualTo(new Point(25, 25)));
}
[Test]
public void ShouldGetCoordinatesInViewPortOfAnElementInANestedFrame()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("coordinates_tests/element_in_nested_frame.html");
driver.SwitchTo().Frame("ifr");
driver.SwitchTo().Frame("ifr");
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(10, 10)));
// GetLocationInViewPort only works within the context of a single frame
// for W3C-spec compliant remote ends.
// Assert.That(GetLocationInViewPort(By.Id("box")), Is.EqualTo(new Point(40, 40)));
}
[Test]
public void ShouldGetCoordinatesOfAnElementWithFixedPosition()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("coordinates_tests/page_with_fixed_element.html");
Assert.That(GetLocationInViewPort(By.Id("fixed")).Y, Is.EqualTo(0));
Assert.That(GetLocationOnPage(By.Id("fixed")).Y, Is.EqualTo(0));
driver.FindElement(By.Id("bottom")).Click();
Assert.That(GetLocationInViewPort(By.Id("fixed")).Y, Is.EqualTo(0));
Assert.That(GetLocationOnPage(By.Id("fixed")).Y, Is.GreaterThan(0));
}
[Test]
public void ShouldCorrectlyIdentifyThatAnElementHasWidthAndHeight()
{
driver.Url = xhtmlTestPage;
IWebElement shrinko = driver.FindElement(By.Id("linkId"));
Size size = shrinko.Size;
Assert.That(size.Width, Is.GreaterThan(0), "Width expected to be greater than 0");
Assert.That(size.Height, Is.GreaterThan(0), "Height expected to be greater than 0");
}
[Test]
public void ShouldHandleNonIntegerPositionAndSize()
{
driver.Url = rectanglesPage;
IWebElement r2 = driver.FindElement(By.Id("r2"));
string left = r2.GetCssValue("left");
Assert.That(Math.Round(Convert.ToDecimal(left.Replace("px", "")), 1), Is.EqualTo(10.9));
string top = r2.GetCssValue("top");
Assert.That(Math.Round(Convert.ToDecimal(top.Replace("px", "")), 1), Is.EqualTo(10.1));
Assert.That(r2.Location, Is.EqualTo(new Point(11, 10)));
string width = r2.GetCssValue("width");
Assert.That(Math.Round(Convert.ToDecimal(width.Replace("px", "")), 1), Is.EqualTo(48.7));
string height = r2.GetCssValue("height");
Assert.That(Math.Round(Convert.ToDecimal(height.Replace("px", "")), 1), Is.EqualTo(49.3));
Assert.That(r2.Size, Is.EqualTo(new Size(49, 49)));
}
//------------------------------------------------------------------
// Tests below here are not included in the Java test suite
//------------------------------------------------------------------
[Test]
public void ShouldBeAbleToDetermineTheSizeOfAnElement()
{
driver.Url = xhtmlTestPage;
IWebElement element = driver.FindElement(By.Id("username"));
Size size = element.Size;
Assert.That(size.Width, Is.GreaterThan(0));
Assert.That(size.Height, Is.GreaterThan(0));
}
private Point GetLocationInViewPort(By locator)
{
IWebElement element = driver.FindElement(locator);
return ((ILocatable)element).Coordinates.LocationInViewport;
}
private Point GetLocationOnPage(By locator)
{
IWebElement element = driver.FindElement(locator);
return ((ILocatable)element).Coordinates.LocationInDom;
}
}
}