|
| 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