Skip to content

Commit

Permalink
Implement location of element in view in javascript when speaking to …
Browse files Browse the repository at this point in the history
…a W3C Conformant endpoint
  • Loading branch information
AutomatedTester committed Jun 15, 2016
1 parent 2adcc58 commit 48cc1a9
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
9 changes: 8 additions & 1 deletion py/selenium/webdriver/remote/webelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,14 @@ def location_once_scrolled_into_view(self):
the element is not visible.
"""
return self._execute(Command.GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW)['value']
if self._w3c:
old_loc = self._execute(Command.EXECUTE_SCRIPT,
{'script': "arguments[0].scrollIntoView(true); return arguments[0].getBoundingClientRect()",
'args':[self]})['value']
return {"x": round(old_loc['x']),
"y": round(old_loc['y'])}
else:
return self._execute(Command.GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW)['value']

@property
def size(self):
Expand Down
100 changes: 100 additions & 0 deletions py/test/selenium/webdriver/common/position_and_size_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import pytest
import unittest
from selenium.webdriver.common.by import By


class PositionAndSizeTest(unittest.TestCase):

def testShouldBeAbleToDetermineTheLocationOfAnElement(self):
self._loadPage("xhtmlTest")

location = self._get_location_in_viewport(By.ID,"username")

self.assertGreater(location["x"], 0)
self.assertGreater(location["y"], 0)

def testShouldGetCoordinatesOfAnElement(self):
self.driver.get(self.webserver.where_is("coordinates_tests/simple_page.html"))
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 10, "y": 10})
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})

def testShouldGetCoordinatesOfAnEmptyElement(self):
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_empty_element.html"))
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 10, "y": 10})
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})

def testShouldGetCoordinatesOfATransparentElement(self):
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_transparent_element.html"))
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 10, "y": 10})
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})

def testShouldGetCoordinatesOfAHiddenElement(self):
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_hidden_element.html"))
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 10, "y": 10})
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})

def testShouldGetCoordinatesOfAnInvisibleElement(self):
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_invisible_element.html"))
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 0, "y": 0})
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 0, "y": 0})

def testShouldScrollPageAndGetCoordinatesOfAnElementThatIsOutOfViewPort(self):
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_element_out_of_view.html"))
windowHeight =self.driver.get_window_size()["height"]
location = self._get_location_in_viewport(By.ID,"box")
self.assertEqual(location["x"], 10)
self.assertGreaterEqual(location["y"], 0)
self.assertLessEqual(location["y"], windowHeight - 100)
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x":10, "y":5010})

def testShouldGetCoordinatesOfAnElementInAFrame(self):
self.driver.get(self.webserver.where_is("coordinates_tests/element_in_frame.html"))
self.driver.switch_to_frame(self.driver.find_element(By.NAME, "ifr"))
box =self.driver.find_element(By.ID,"box")
self.assertEqual(box.location, {"x": 10, "y": 10})
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})

def testShouldGetCoordinatesInViewPortOfAnElementInAFrame(self):
self.driver.get(self.webserver.where_is("coordinates_tests/element_in_frame.html"))
self.driver.switch_to_frame(self.driver.find_element(By.NAME, "ifr"))
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 25, "y": 25})
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})

def testShouldGetCoordinatesInViewPortOfAnElementInANestedFrame(self):
self.driver.get(self.webserver.where_is("coordinates_tests/element_in_nested_frame.html"))
self.driver.switch_to_frame(self.driver.find_element(By.NAME, "ifr"))
self.driver.switch_to_frame(self.driver.find_element(By.NAME, "ifr"))
self.assertEqual(self._get_location_in_viewport(By.ID,"box"), {"x": 40, "y": 40})
self.assertEqual(self._get_location_on_page(By.ID,"box"), {"x": 10, "y": 10})

def testShouldGetCoordinatesOfAnElementWithFixedPosition(self):
self.driver.get(self.webserver.where_is("coordinates_tests/page_with_fixed_element.html"))
self.assertEqual(self._get_location_in_viewport(By.ID,"fixed")["y"], 0)
self.assertEqual(self._get_location_on_page(By.ID,"fixed")["y"], 0)

self.driver.find_element(By.ID,"bottom").click()
self.assertEqual(self._get_location_in_viewport(By.ID,"fixed")["y"], 0)
self.assertGreater(self._get_location_on_page(By.ID,"fixed")["y"], 0)

def testShouldCorrectlyIdentifyThatAnElementHasWidthAndHeight(self):
self._loadPage("xhtmlTest")

shrinko =self.driver.find_element(By.ID,"linkId")
size = shrinko.size
self.assertTrue(size["width"] > 0)
self.assertTrue(size["height"] > 0)

def _get_location_in_viewport(self, by, locator):
element = self.driver.find_element(by, locator)
return element.location_once_scrolled_into_view


def _get_location_on_page(self, by, locator):
element = self.driver.find_element(by, locator)
return element.location

def _pageURL(self, name):
return self.webserver.where_is(name + '.html')

def _loadPage(self, name):
self.driver.get(self._pageURL(name))

0 comments on commit 48cc1a9

Please sign in to comment.