<commit_before># 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.

import pytest

from selenium.webdriver.common.by import By


class TestPositionAndSize(object):

    def testShouldBeAbleToDetermineTheLocationOfAnElement(self, driver, pages):
        pages.load("xhtmlTest.html")

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

        assert location["x"] > 0
        assert location["y"] > 0

    def testShouldGetCoordinatesOfAnElement(self, driver, pages):
        if driver.capabilities['browserName'] == 'phantomjs':
            pytest.xfail("phantomjs calculates coordinates differently")
        pages.load("coordinates_tests/simple_page.html")
        assert self._get_location_in_viewport(driver, By.ID, "box") == {"x": 10, "y": 10}
        assert self._get_location_on_page(driver, By.ID, "box") == {"x": 10, "y": 10}

    def testShouldGetCoordinatesOfAnEmptyElement(self, driver, pages):
        if driver.capabilities['browserName'] == 'phantomjs':
            pytest.xfail("phantomjs calculates coordinates differently")
        pages.load("coordinates_tests/page_with_empty_element.html")
        assert self._get_location_in_viewport(driver, By.ID, "box") == {"x": 10, "y": 10}
        assert self._get_location_on_page(driver, By.ID, "box") == {"x": 10, "y": 10}

    def testShouldGetCoordinatesOfATransparentElement(self, driver, pages):
        if driver.capabilities['browserName'] == 'phantomjs':
            pytest.xfail("phantomjs calculates coordinates differently")
        pages.load("coordinates_tests/page_with_transparent_element.html")
        assert self._get_location_in_viewport(driver, By.ID, "box") == {"x": 10, "y": 10}
        assert self._get_location_on_page(driver, By.ID, "box") == {"x": 10, "y": 10}

    def testShouldGetCoordinatesOfAHiddenElement(self, driver, pages):
        if driver.capabilities['browserName'] == 'phantomjs':
            pytest.xfail("phantomjs calculates coordinates differently")
        pages.load("coordinates_tests/page_with_hidden_element.html")
        assert self._get_location_in_viewport(driver, By.ID, "box") == {"x": 10, "y": 10}
        assert self._get_location_on_page(driver, By.ID, "box") == {"x": 10, "y": 10}

    def testShouldGetCoordinatesOfAnInvisibleElement(self, driver, pages):
        if driver.capabilities['browserName'] == 'phantomjs':
            pytest.xfail("phantomjs calculates coordinates differently")
        pages.load("coordinates_tests/page_with_invisible_element.html")
        assert self._get_location_in_viewport(driver, By.ID, "box") == {"x": 0, "y": 0}
        assert self._get_location_on_page(driver, By.ID, "box") == {"x": 0, "y": 0}

    def testShouldScrollPageAndGetCoordinatesOfAnElementThatIsOutOfViewPort(self, driver, pages):
        pages.load("coordinates_tests/page_with_element_out_of_view.html")
        windowHeight = driver.get_window_size()["height"]
        location = self._get_location_in_viewport(driver, By.ID, "box")
        assert location["x"] == 10
        assert location["y"] >= 0
        assert location["y"] <= windowHeight - 100
        assert self._get_location_on_page(driver, By.ID, "box") == {"x": 10, "y": 5010}

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

    @pytest.mark.ignore_marionette
    def testShouldGetCoordinatesInViewPortOfAnElementInAFrame(self, driver, pages):
        if driver.capabilities['browserName'] == 'phantomjs':
            pytest.xfail("phantomjs calculates coordinates differently")
        pages.load("coordinates_tests/element_in_frame.html")
        driver.switch_to_frame(driver.find_element(By.NAME, "ifr"))
        assert self._get_location_in_viewport(driver, By.ID, "box") == {"x": 25, "y": 25}
        assert self._get_location_on_page(driver, By.ID, "box") == {"x": 10, "y": 10}

    @pytest.mark.ignore_marionette
    def testShouldGetCoordinatesInViewPortOfAnElementInANestedFrame(self, driver, pages):
        if driver.capabilities['browserName'] == 'phantomjs':
            pytest.xfail("phantomjs calculates coordinates differently")
        pages.load("coordinates_tests/element_in_nested_frame.html")
        driver.switch_to_frame(driver.find_element(By.NAME, "ifr"))
        driver.switch_to_frame(driver.find_element(By.NAME, "ifr"))
        assert self._get_location_in_viewport(driver, By.ID, "box") == {"x": 40, "y": 40}
        assert self._get_location_on_page(driver, By.ID, "box") == {"x": 10, "y": 10}

    def testShouldGetCoordinatesOfAnElementWithFixedPosition(self, driver, pages):
        pages.load("coordinates_tests/page_with_fixed_element.html")
        assert self._get_location_in_viewport(driver, By.ID, "fixed")["y"] == 0
        assert self._get_location_on_page(driver, By.ID, "fixed")["y"] == 0

        driver.find_element(By.ID, "bottom").click()
        assert self._get_location_in_viewport(driver, By.ID, "fixed")["y"] == 0
        assert self._get_location_on_page(driver, By.ID, "fixed")["y"] > 0

    def testShouldCorrectlyIdentifyThatAnElementHasWidthAndHeight(self, driver, pages):
        pages.load("xhtmlTest.html")

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

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

    def _get_location_on_page(self, driver, by, locator):
        element = driver.find_element(by, locator)
        return element.location<commit_msg>[py] Refactor position and size tests and remove xfails for PhantomJS<commit_after># 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.

import pytest

from selenium.webdriver.common.by import By


class TestPositionAndSize(object):

    def testShouldBeAbleToDetermineTheLocationOfAnElement(self, driver, pages):
        pages.load("xhtmlTest.html")
        location = driver.find_element(By.ID, "username").location_once_scrolled_into_view
        assert location["x"] > 0
        assert location["y"] > 0

    @pytest.mark.parametrize('page', (
        'coordinates_tests/simple_page.html',
        'coordinates_tests/page_with_empty_element.html',
        'coordinates_tests/page_with_transparent_element.html',
        'coordinates_tests/page_with_hidden_element.html'),
        ids=('basic', 'empty', 'transparent', 'hidden'))
    def testShouldGetCoordinatesOfAnElement(self, page, driver, pages):
        pages.load(page)
        element = driver.find_element(By.ID, "box")
        self._check_location(element.location_once_scrolled_into_view, x=10, y=10)
        self._check_location(element.location, x=10, y=10)

    def testShouldGetCoordinatesOfAnInvisibleElement(self, driver, pages):
        pages.load("coordinates_tests/page_with_invisible_element.html")
        element = driver.find_element(By.ID, "box")
        self._check_location(element.location_once_scrolled_into_view, x=0, y=0)
        self._check_location(element.location, x=0, y=0)

    def testShouldScrollPageAndGetCoordinatesOfAnElementThatIsOutOfViewPort(self, driver, pages):
        pages.load("coordinates_tests/page_with_element_out_of_view.html")
        element = driver.find_element(By.ID, "box")
        windowHeight = driver.get_window_size()["height"]
        self._check_location(element.location_once_scrolled_into_view, x=10)
        assert 0 <= element.location_once_scrolled_into_view["y"] <= (windowHeight - 100)
        self._check_location(element.location, x=10, y=5010)

    @pytest.mark.ignore_marionette
    def testShouldGetCoordinatesOfAnElementInAFrame(self, driver, pages):
        if driver.capabilities['browserName'] == 'phantomjs':
            pytest.xfail("phantomjs calculates coordinates differently")
        pages.load("coordinates_tests/element_in_frame.html")
        driver.switch_to_frame(driver.find_element(By.NAME, "ifr"))
        element = driver.find_element(By.ID, "box")
        self._check_location(element.location_once_scrolled_into_view, x=25, y=25)
        self._check_location(element.location, x=10, y=10)

    @pytest.mark.ignore_marionette
    def testShouldGetCoordinatesOfAnElementInANestedFrame(self, driver, pages):
        if driver.capabilities['browserName'] == 'phantomjs':
            pytest.xfail("phantomjs calculates coordinates differently")
        pages.load("coordinates_tests/element_in_nested_frame.html")
        driver.switch_to_frame(driver.find_element(By.NAME, "ifr"))
        driver.switch_to_frame(driver.find_element(By.NAME, "ifr"))
        element = driver.find_element(By.ID, "box")
        self._check_location(element.location_once_scrolled_into_view, x=40, y=40)
        self._check_location(element.location, x=10, y=10)

    def testShouldGetCoordinatesOfAnElementWithFixedPosition(self, driver, pages):
        pages.load("coordinates_tests/page_with_fixed_element.html")
        element = driver.find_element(By.ID, "fixed")
        self._check_location(element.location_once_scrolled_into_view, y=0)
        self._check_location(element.location, y=0)

        driver.find_element(By.ID, "bottom").click()
        self._check_location(element.location_once_scrolled_into_view, y=0)
        assert element.location["y"] > 0

    def testShouldCorrectlyIdentifyThatAnElementHasWidthAndHeight(self, driver, pages):
        pages.load("xhtmlTest.html")
        shrinko = driver.find_element(By.ID, "linkId")
        size = shrinko.size
        assert size["width"] > 0
        assert size["height"] > 0

    def _check_location(self, location, **kwargs):
        try:
            # python 2.x
            expected = kwargs.viewitems()
            actual = location.viewitems()
        except AttributeError:
            # python 3.x
            expected = kwargs.items()
            actual = location.items()
        assert expected <= actual
{'fraction_non_alphanumeric': 0.11460827916524119, 'fraction_numerical': 0.011802942182689019, 'mean_word_length': 3.9295952782462056, 'pattern_counts': {'":': 34, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1790218', 'n_tokens_mistral': 3625, 'n_tokens_neox': 3467, 'n_words': 765}