File: position_and_size_tests.py

package info (click to toggle)
python-selenium 4.24.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,404 kB
  • sloc: python: 14,901; javascript: 2,347; makefile: 124; sh: 52
file content (120 lines) | stat: -rw-r--r-- 4,637 bytes parent folder | download
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
# 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


def test_should_be_able_to_determine_the_location_of_an_element(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"),
)
@pytest.mark.xfail_safari
def test_should_get_coordinates_of_an_element(page, driver, pages):
    pages.load(page)
    element = driver.find_element(By.ID, "box")
    _check_location(element.location_once_scrolled_into_view, x=10, y=10)
    _check_location(element.location, x=10, y=10)


@pytest.mark.xfail_safari
def test_should_get_coordinates_of_an_invisible_element(driver, pages):
    pages.load("coordinates_tests/page_with_invisible_element.html")
    element = driver.find_element(By.ID, "box")
    _check_location(element.location_once_scrolled_into_view, x=0, y=0)
    _check_location(element.location, x=0, y=0)


def test_should_scroll_page_and_get_coordinates_of_an_element_that_is_out_of_view_port(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"]
    _check_location(element.location_once_scrolled_into_view, x=10)
    assert 0 <= element.location_once_scrolled_into_view["y"] <= (windowHeight - 100)
    _check_location(element.location, x=10, y=5010)


@pytest.mark.xfail_chrome
@pytest.mark.xfail_edge
@pytest.mark.xfail_firefox
@pytest.mark.xfail_remote
@pytest.mark.xfail_safari
def test_should_get_coordinates_of_an_element_in_aframe(driver, pages):
    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")
    _check_location(element.location_once_scrolled_into_view, x=25, y=25)
    _check_location(element.location, x=10, y=10)


@pytest.mark.xfail_chrome
@pytest.mark.xfail_edge
@pytest.mark.xfail_firefox
@pytest.mark.xfail_remote
@pytest.mark.xfail_safari
def test_should_get_coordinates_of_an_element_in_anested_frame(driver, pages):
    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")
    _check_location(element.location_once_scrolled_into_view, x=40, y=40)
    _check_location(element.location, x=10, y=10)


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

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


def test_should_correctly_identify_that_an_element_has_width_and_height(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(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