Skip to content

Commit 48cc1a9

Browse files
Implement location of element in view in javascript when speaking to a W3C Conformant endpoint
1 parent 2adcc58 commit 48cc1a9

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

py/selenium/webdriver/remote/webelement.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,14 @@ def location_once_scrolled_into_view(self):
335335
the element is not visible.
336336
337337
"""
338-
return self._execute(Command.GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW)['value']
338+
if self._w3c:
339+
old_loc = self._execute(Command.EXECUTE_SCRIPT,
340+
{'script': "arguments[0].scrollIntoView(true); return arguments[0].getBoundingClientRect()",
341+
'args':[self]})['value']
342+
return {"x": round(old_loc['x']),
343+
"y": round(old_loc['y'])}
344+
else:
345+
return self._execute(Command.GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW)['value']
339346

340347
@property
341348
def size(self):
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import pytest
2+
import unittest
3+
from selenium.webdriver.common.by import By
4+
5+
6+
class PositionAndSizeTest(unittest.TestCase):
7+
8+
def testShouldBeAbleToDetermineTheLocationOfAnElement(self):
9+
self._loadPage("xhtmlTest")
10+
11+
location = self._get_location_in_viewport(By.ID,"username")
12+
13+
self.assertGreater(location["x"], 0)
14+
self.assertGreater(location["y"], 0)
15+
16+
def testShouldGetCoordinatesOfAnElement(self):
17+
self.driver.get(self.webserver.where_is("coordinates_tests/simple_page.html"))
18+
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 10, "y": 10})
19+
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})
20+
21+
def testShouldGetCoordinatesOfAnEmptyElement(self):
22+
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_empty_element.html"))
23+
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 10, "y": 10})
24+
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})
25+
26+
def testShouldGetCoordinatesOfATransparentElement(self):
27+
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_transparent_element.html"))
28+
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 10, "y": 10})
29+
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})
30+
31+
def testShouldGetCoordinatesOfAHiddenElement(self):
32+
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_hidden_element.html"))
33+
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 10, "y": 10})
34+
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})
35+
36+
def testShouldGetCoordinatesOfAnInvisibleElement(self):
37+
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_invisible_element.html"))
38+
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 0, "y": 0})
39+
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 0, "y": 0})
40+
41+
def testShouldScrollPageAndGetCoordinatesOfAnElementThatIsOutOfViewPort(self):
42+
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_element_out_of_view.html"))
43+
windowHeight =self.driver.get_window_size()["height"]
44+
location = self._get_location_in_viewport(By.ID,"box")
45+
self.assertEqual(location["x"], 10)
46+
self.assertGreaterEqual(location["y"], 0)
47+
self.assertLessEqual(location["y"], windowHeight - 100)
48+
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x":10, "y":5010})
49+
50+
def testShouldGetCoordinatesOfAnElementInAFrame(self):
51+
self.driver.get(self.webserver.where_is("coordinates_tests/element_in_frame.html"))
52+
self.driver.switch_to_frame(self.driver.find_element(By.NAME, "ifr"))
53+
box =self.driver.find_element(By.ID,"box")
54+
self.assertEqual(box.location, {"x": 10, "y": 10})
55+
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})
56+
57+
def testShouldGetCoordinatesInViewPortOfAnElementInAFrame(self):
58+
self.driver.get(self.webserver.where_is("coordinates_tests/element_in_frame.html"))
59+
self.driver.switch_to_frame(self.driver.find_element(By.NAME, "ifr"))
60+
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 25, "y": 25})
61+
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})
62+
63+
def testShouldGetCoordinatesInViewPortOfAnElementInANestedFrame(self):
64+
self.driver.get(self.webserver.where_is("coordinates_tests/element_in_nested_frame.html"))
65+
self.driver.switch_to_frame(self.driver.find_element(By.NAME, "ifr"))
66+
self.driver.switch_to_frame(self.driver.find_element(By.NAME, "ifr"))
67+
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 40, "y": 40})
68+
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})
69+
70+
def testShouldGetCoordinatesOfAnElementWithFixedPosition(self):
71+
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_fixed_element.html"))
72+
self.assertEqual(self._get_location_in_viewport(By.ID,"fixed")["y"], 0)
73+
self.assertEqual(self._get_location_on_page(By.ID,"fixed")["y"], 0)
74+
75+
self.driver.find_element(By.ID,"bottom").click()
76+
self.assertEqual(self._get_location_in_viewport(By.ID,"fixed")["y"], 0)
77+
self.assertGreater(self._get_location_on_page(By.ID,"fixed")["y"], 0)
78+
79+
def testShouldCorrectlyIdentifyThatAnElementHasWidthAndHeight(self):
80+
self._loadPage("xhtmlTest")
81+
82+
shrinko =self.driver.find_element(By.ID,"linkId")
83+
size = shrinko.size
84+
self.assertTrue(size["width"] > 0)
85+
self.assertTrue(size["height"] > 0)
86+
87+
def _get_location_in_viewport(self, by, locator):
88+
element = self.driver.find_element(by, locator)
89+
return element.location_once_scrolled_into_view
90+
91+
92+
def _get_location_on_page(self, by, locator):
93+
element = self.driver.find_element(by, locator)
94+
return element.location
95+
96+
def _pageURL(self, name):
97+
return self.webserver.where_is(name + '.html')
98+
99+
def _loadPage(self, name):
100+
self.driver.get(self._pageURL(name))

0 commit comments

Comments
 (0)