Skip to content

Commit 92d9528

Browse files
committed
[py] Add detailed error message if KeyError thrown in get_window_size and get_window_position (SeleniumHQ#15503)
1 parent 4d413c6 commit 92d9528

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

py/selenium/webdriver/remote/webdriver.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,10 @@ def get_window_size(self, windowHandle: str = "current") -> dict:
10441044
if size.get("value", None):
10451045
size = size["value"]
10461046

1047-
return {k: size[k] for k in ("width", "height")}
1047+
try:
1048+
return {k: size[k] for k in ("width", "height")}
1049+
except KeyError as e:
1050+
raise KeyError(f"No size with key: {e.args[0]} existed in {size}")
10481051

10491052
def set_window_position(self, x: float, y: float, windowHandle: str = "current") -> dict:
10501053
"""Sets the x,y position of the current window. (window.moveTo)
@@ -1075,7 +1078,10 @@ def get_window_position(self, windowHandle="current") -> dict:
10751078
self._check_if_window_handle_is_current(windowHandle)
10761079
position = self.get_window_rect()
10771080

1078-
return {k: position[k] for k in ("x", "y")}
1081+
try:
1082+
return {k: position[k] for k in ("x", "y")}
1083+
except KeyError as e:
1084+
raise KeyError(f"No position with key: {e.args[0]} existed in {position}")
10791085

10801086
def _check_if_window_handle_is_current(self, windowHandle: str) -> None:
10811087
"""Warns if the window handle is not equal to `current`."""

py/test/selenium/webdriver/common/window_tests.py

+24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
import re
18+
from random import randint
19+
from unittest.mock import Mock
1720

1821
import pytest
1922

@@ -45,6 +48,16 @@ def test_should_get_the_size_of_the_current_window(driver):
4548
assert size.get("height") > 0
4649

4750

51+
@pytest.mark.parametrize("missing_key", ["width", "height"])
52+
def test_get_the_size_of_the_current_window_raises(driver, missing_key):
53+
driver.get_window_rect = Mock(return_value={k: randint(1, 15360) for k in ("width", "height") if k != missing_key})
54+
55+
with pytest.raises(
56+
KeyError, match=re.escape(f"No size with key: {missing_key} existed in {driver.get_window_rect.return_value}")
57+
):
58+
driver.get_window_size()
59+
60+
4861
def test_should_set_the_size_of_the_current_window(driver):
4962
size = driver.get_window_size()
5063

@@ -64,6 +77,17 @@ def test_should_get_the_position_of_the_current_window(driver):
6477
assert position.get("y") >= 0
6578

6679

80+
@pytest.mark.parametrize("missing_key", ["x", "y"])
81+
def test_get_the_position_of_the_current_window_raises(driver, missing_key):
82+
driver.get_window_rect = Mock(return_value={k: randint(0, 15360) for k in ("x", "y") if k != missing_key})
83+
84+
with pytest.raises(
85+
KeyError,
86+
match=re.escape(f"No position with key: {missing_key} existed in {driver.get_window_rect.return_value}"),
87+
):
88+
driver.get_window_position()
89+
90+
6791
def test_should_set_the_position_of_the_current_window(driver):
6892
position = driver.get_window_position()
6993

0 commit comments

Comments
 (0)