Skip to content

Commit 9564bde

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

File tree

2 files changed

+31
-2
lines changed

2 files changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
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 unittest.mock import Mock
1719

1820
import pytest
1921

@@ -45,6 +47,16 @@ def test_should_get_the_size_of_the_current_window(driver):
4547
assert size.get("height") > 0
4648

4749

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

@@ -64,6 +76,17 @@ def test_should_get_the_position_of_the_current_window(driver):
6476
assert position.get("y") >= 0
6577

6678

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

0 commit comments

Comments
 (0)