Skip to content

Commit ffce538

Browse files
committed
Fix python backward compatibility for window commands
Fixes #4937
1 parent 5cf3950 commit ffce538

File tree

4 files changed

+108
-27
lines changed

4 files changed

+108
-27
lines changed

py/selenium/webdriver/remote/remote_connection.py

-8
Original file line numberDiff line numberDiff line change
@@ -350,20 +350,12 @@ def __init__(self, remote_server_addr, keep_alive=False, resolve_ip=True):
350350
('POST', '/session/$sessionId/moveto'),
351351
Command.GET_WINDOW_SIZE:
352352
('GET', '/session/$sessionId/window/$windowHandle/size'),
353-
Command.W3C_GET_WINDOW_SIZE:
354-
('GET', '/session/$sessionId/window/size'),
355353
Command.SET_WINDOW_SIZE:
356354
('POST', '/session/$sessionId/window/$windowHandle/size'),
357-
Command.W3C_SET_WINDOW_SIZE:
358-
('POST', '/session/$sessionId/window/size'),
359355
Command.GET_WINDOW_POSITION:
360356
('GET', '/session/$sessionId/window/$windowHandle/position'),
361357
Command.SET_WINDOW_POSITION:
362358
('POST', '/session/$sessionId/window/$windowHandle/position'),
363-
Command.W3C_GET_WINDOW_POSITION:
364-
('GET', '/session/$sessionId/window/position'),
365-
Command.W3C_SET_WINDOW_POSITION:
366-
('POST', '/session/$sessionId/window/position'),
367359
Command.SET_WINDOW_RECT:
368360
('POST', '/session/$sessionId/window/rect'),
369361
Command.GET_WINDOW_RECT:

py/selenium/webdriver/remote/webdriver.py

+26-18
Original file line numberDiff line numberDiff line change
@@ -948,13 +948,15 @@ def set_window_size(self, width, height, windowHandle='current'):
948948
:Usage:
949949
driver.set_window_size(800,600)
950950
"""
951-
command = Command.SET_WINDOW_SIZE
952951
if self.w3c:
953-
command = Command.W3C_SET_WINDOW_SIZE
954-
self.execute(command, {
955-
'width': int(width),
956-
'height': int(height),
957-
'windowHandle': windowHandle})
952+
if windowHandle != 'current':
953+
warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
954+
self.set_window_rect(width=int(width), height=int(height))
955+
else:
956+
self.execute(Command.SET_WINDOW_SIZE, {
957+
'width': int(width),
958+
'height': int(height),
959+
'windowHandle': windowHandle})
958960

959961
def get_window_size(self, windowHandle='current'):
960962
"""
@@ -965,13 +967,16 @@ def get_window_size(self, windowHandle='current'):
965967
"""
966968
command = Command.GET_WINDOW_SIZE
967969
if self.w3c:
968-
command = Command.W3C_GET_WINDOW_SIZE
969-
size = self.execute(command, {'windowHandle': windowHandle})
970+
if windowHandle != 'current':
971+
warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
972+
size = self.get_window_rect()
973+
else:
974+
size = self.execute(command, {'windowHandle': windowHandle})
970975

971976
if size.get('value', None) is not None:
972-
return size['value']
973-
else:
974-
return size
977+
size = size['value']
978+
979+
return {k: size[k] for k in ('width', 'height')}
975980

976981
def set_window_position(self, x, y, windowHandle='current'):
977982
"""
@@ -985,10 +990,9 @@ def set_window_position(self, x, y, windowHandle='current'):
985990
driver.set_window_position(0,0)
986991
"""
987992
if self.w3c:
988-
return self.execute(Command.W3C_SET_WINDOW_POSITION, {
989-
'x': int(x),
990-
'y': int(y)
991-
})
993+
if windowHandle != 'current':
994+
warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
995+
return self.set_window_rect(x=int(x), y=int(y))
992996
else:
993997
self.execute(Command.SET_WINDOW_POSITION,
994998
{
@@ -1005,10 +1009,14 @@ def get_window_position(self, windowHandle='current'):
10051009
driver.get_window_position()
10061010
"""
10071011
if self.w3c:
1008-
return self.execute(Command.W3C_GET_WINDOW_POSITION)['value']
1012+
if windowHandle != 'current':
1013+
warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
1014+
position = self.get_window_rect()
10091015
else:
1010-
return self.execute(Command.GET_WINDOW_POSITION, {
1011-
'windowHandle': windowHandle})['value']
1016+
position = self.execute(Command.GET_WINDOW_POSITION,
1017+
{'windowHandle': windowHandle})['value']
1018+
1019+
return {k: position[k] for k in ('x', 'y')}
10121020

10131021
def get_window_rect(self):
10141022
"""

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

+81
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import pytest
1919

20+
from selenium.common.exceptions import WebDriverException
2021
from selenium.webdriver.support.wait import WebDriverWait
2122

2223

@@ -38,3 +39,83 @@ def testShouldMaximizeTheWindow(driver):
3839
new_size = driver.get_window_size()
3940
assert new_size["width"] > size["width"]
4041
assert new_size["height"] > size["height"]
42+
43+
44+
def test_should_get_the_size_of_the_current_window(driver):
45+
size = driver.get_window_size()
46+
assert size.get('width') > 0
47+
assert size.get('height') > 0
48+
49+
50+
def test_should_set_the_size_of_the_current_window(driver):
51+
size = driver.get_window_size()
52+
53+
target_width = size.get('width') - 20
54+
target_height = size.get('height') - 20
55+
driver.set_window_size(width=target_width, height=target_height)
56+
57+
new_size = driver.get_window_size()
58+
assert new_size.get('width') == target_width
59+
assert new_size.get('height') == target_height
60+
61+
62+
def test_should_get_the_position_of_the_current_window(driver):
63+
position = driver.get_window_position()
64+
assert position.get('x') >= 0
65+
assert position.get('y') >= 0
66+
67+
68+
def test_should_set_the_position_of_the_current_window(driver):
69+
position = driver.get_window_position()
70+
71+
target_x = position.get('x') + 10
72+
target_y = position.get('y') + 10
73+
driver.set_window_position(x=target_x, y=target_y)
74+
75+
WebDriverWait(driver, 2).until(lambda d: d.get_window_position()['x'] != position['x'] and
76+
d.get_window_position()['y'] != position['y'])
77+
78+
new_position = driver.get_window_position()
79+
assert new_position.get('x') == target_x
80+
assert new_position.get('y') == target_y
81+
82+
83+
@pytest.mark.xfail_chrome(raises=WebDriverException,
84+
reason='Get Window Rect command not implemented')
85+
@pytest.mark.xfail_phantomjs(raises=WebDriverException,
86+
reason='Get Window Rect command not implemented')
87+
@pytest.mark.xfail_safari(raises=WebDriverException,
88+
reason='Get Window Rect command not implemented')
89+
def test_should_get_the_rect_of_the_current_window(driver):
90+
rect = driver.get_window_rect()
91+
assert rect.get('x') >= 0
92+
assert rect.get('y') >= 0
93+
assert rect.get('width') >= 0
94+
assert rect.get('height') >= 0
95+
96+
97+
@pytest.mark.xfail_chrome(raises=WebDriverException,
98+
reason='Get Window Rect command not implemented')
99+
@pytest.mark.xfail_phantomjs(raises=WebDriverException,
100+
reason='Get Window Rect command not implemented')
101+
@pytest.mark.xfail_safari(raises=WebDriverException,
102+
reason='Get Window Rect command not implemented')
103+
def test_should_set_the_rect_of_the_current_window(driver):
104+
rect = driver.get_window_rect()
105+
106+
target_x = rect.get('x') + 10
107+
target_y = rect.get('y') + 10
108+
target_width = rect.get('width') + 10
109+
target_height = rect.get('height') + 10
110+
111+
driver.set_window_rect(x=target_x, y=target_y, width=target_width, height=target_height)
112+
113+
WebDriverWait(driver, 2).until(lambda d: d.get_window_position()['x'] != rect['x'] and
114+
d.get_window_position()['y'] != rect['y'])
115+
116+
new_rect = driver.get_window_rect()
117+
118+
assert new_rect.get('x') == target_x
119+
assert new_rect.get('y') == target_y
120+
assert new_rect.get('width') == target_width
121+
assert new_rect.get('height') == target_height

py/tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ envlist = py{27,33,34,35,36}-{unit,chrome,edge,firefox,ie,phantomjs,remote,safar
55
passenv = DISPLAY PYTEST_ADDOPTS
66
commands =
77
py{27,33,34,35,36}-unit: py.test -n=auto {posargs:test/unit}
8-
py{27,36}-chrome: py.test -n=auto --driver=Chrome {posargs}
8+
py{27,36}-chrome: py.test --driver=Chrome {posargs}
99
py{27,36}-edge: py.test --driver=Edge {posargs}
1010
py{27,36}-firefox: py.test --driver=Firefox {posargs}
1111
py{27,36}-ie: py.test --driver=Ie {posargs}

0 commit comments

Comments
 (0)