Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions py/selenium/webdriver/remote/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,10 @@ def get_window_size(self, windowHandle: str = "current") -> dict:
if size.get("value", None):
size = size["value"]

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

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

return {k: position[k] for k in ("x", "y")}
try:
return {k: position[k] for k in ("x", "y")}
except KeyError as e:
raise KeyError(f"No position with key: {e.args[0]} existed in {position}")

def _check_if_window_handle_is_current(self, windowHandle: str) -> None:
"""Warns if the window handle is not equal to `current`."""
Expand Down
23 changes: 23 additions & 0 deletions py/test/selenium/webdriver/common/window_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import re
from unittest.mock import Mock

import pytest

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


@pytest.mark.parametrize("missing_key", ["width", "height"])
def test_get_the_size_of_the_current_window_raises(driver, missing_key):
driver.get_window_rect = Mock(return_value={k: 100 for k in ("width", "height") if k != missing_key})

with pytest.raises(
KeyError, match=re.escape(f"No size with key: {missing_key} existed in {driver.get_window_rect.return_value}")
):
driver.get_window_size()


def test_should_set_the_size_of_the_current_window(driver):
size = driver.get_window_size()

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


@pytest.mark.parametrize("missing_key", ["x", "y"])
def test_get_the_position_of_the_current_window_raises(driver, missing_key):
driver.get_window_rect = Mock(return_value={k: 100 for k in ("x", "y") if k != missing_key})

with pytest.raises(
KeyError,
match=re.escape(f"No position with key: {missing_key} existed in {driver.get_window_rect.return_value}"),
):
driver.get_window_position()


def test_should_set_the_position_of_the_current_window(driver):
position = driver.get_window_position()

Expand Down