Skip to content

Commit 09b887e

Browse files
committed
Normalise how we handle window commands.
This allows the codec to massage the parameters being passed to the remote end. We try and have our default parameters look like the ones for the w3c protocol.
1 parent 9573544 commit 09b887e

File tree

6 files changed

+108
-51
lines changed

6 files changed

+108
-51
lines changed

java/client/src/org/openqa/selenium/remote/DriverCommand.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,8 @@ public interface DriverCommand {
158158
String TOUCH_FLICK = "touchFlick";
159159

160160
// Window API
161-
String SET_WINDOW_SIZE = "setWindowSize";
162-
String SET_WINDOW_POSITION = "setWindowPosition";
163-
String GET_WINDOW_SIZE = "getWindowSize";
161+
String SET_CURRENT_WINDOW_POSITION = "setWindowPosition";
164162
String GET_WINDOW_POSITION = "getWindowPosition";
165-
String MAXIMIZE_WINDOW = "maximizeWindow";
166163

167164
// W3C compatible Window API
168165
String SET_CURRENT_WINDOW_SIZE = "setCurrentWindowSize";

java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java

+6-25
Original file line numberDiff line numberDiff line change
@@ -879,32 +879,18 @@ public Timeouts pageLoadTimeout(long time, TimeUnit unit) {
879879
protected class RemoteWindow implements Window {
880880

881881
public void setSize(Dimension targetSize) {
882-
if (getW3CStandardComplianceLevel() == 0) {
883-
execute(DriverCommand.SET_WINDOW_SIZE,
884-
ImmutableMap.of("windowHandle", "current",
885-
"width", targetSize.width, "height", targetSize.height));
886-
} else {
887-
execute(DriverCommand.SET_CURRENT_WINDOW_SIZE,
888-
ImmutableMap.of("width", targetSize.width, "height", targetSize.height));
889-
}
882+
execute(DriverCommand.SET_CURRENT_WINDOW_SIZE,
883+
ImmutableMap.of("width", targetSize.width, "height", targetSize.height));
890884
}
891885

892886
public void setPosition(Point targetPosition) {
893-
if (getW3CStandardComplianceLevel() == 0) {
894-
execute(DriverCommand.SET_WINDOW_POSITION,
895-
ImmutableMap
896-
.of("windowHandle", "current", "x", targetPosition.x, "y", targetPosition.y));
897-
} else {
898-
executeScript("window.screenX = arguments[0]; window.screenY = arguments[1]",
899-
targetPosition.x, targetPosition.y);
900-
}
887+
execute(DriverCommand.SET_CURRENT_WINDOW_POSITION,
888+
ImmutableMap.of("x", targetPosition.x, "y", targetPosition.y));
901889
}
902890

903891
@SuppressWarnings({"unchecked"})
904892
public Dimension getSize() {
905-
Response response = getW3CStandardComplianceLevel() == 0
906-
? execute(DriverCommand.GET_WINDOW_SIZE, ImmutableMap.of("windowHandle", "current"))
907-
: execute(DriverCommand.GET_CURRENT_WINDOW_SIZE);
893+
Response response = execute(DriverCommand.GET_CURRENT_WINDOW_SIZE);
908894

909895
Map<String, Object> rawSize = (Map<String, Object>) response.getValue();
910896

@@ -933,12 +919,7 @@ public Point getPosition() {
933919
}
934920

935921
public void maximize() {
936-
if (getW3CStandardComplianceLevel() == 0) {
937-
execute(DriverCommand.MAXIMIZE_WINDOW,
938-
ImmutableMap.of("windowHandle", "current"));
939-
} else {
940-
execute(DriverCommand.MAXIMIZE_CURRENT_WINDOW);
941-
}
922+
execute(DriverCommand.MAXIMIZE_CURRENT_WINDOW);
942923
}
943924

944925
public void fullscreen() {

java/client/src/org/openqa/selenium/remote/http/AbstractHttpCommandCodec.java

+22-18
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
*
5353
* @see <a href="https://w3.org/tr/webdriver">W3C WebDriver spec</a>
5454
*/
55-
public class AbstractHttpCommandCodec implements CommandCodec<HttpRequest> {
55+
public abstract class AbstractHttpCommandCodec implements CommandCodec<HttpRequest> {
5656
private static final Splitter PATH_SPLITTER = Splitter.on('/').omitEmptyStrings();
5757
private static final String SESSION_ID_PARAM = "sessionId";
5858

@@ -79,17 +79,9 @@ public AbstractHttpCommandCodec() {
7979
defineCommand(SWITCH_TO_WINDOW, post("/session/:sessionId/window"));
8080

8181
defineCommand(GET_WINDOW_HANDLES, get("/session/:sessionId/window/handles"));
82-
defineCommand(MAXIMIZE_WINDOW, post("/session/:sessionId/window/:windowHandle/maximize"));
83-
defineCommand(GET_WINDOW_SIZE, get("/session/:sessionId/window/:windowHandle/size"));
84-
defineCommand(SET_WINDOW_SIZE, post("/session/:sessionId/window/:windowHandle/size"));
8582
defineCommand(GET_WINDOW_POSITION, get("/session/:sessionId/window/:windowHandle/position"));
86-
defineCommand(SET_WINDOW_POSITION, post("/session/:sessionId/window/:windowHandle/position"));
8783
defineCommand(GET_CURRENT_WINDOW_HANDLE, get("/session/:sessionId/window"));
88-
89-
defineCommand(MAXIMIZE_CURRENT_WINDOW, post("/session/:sessionId/window/maximize"));
9084
defineCommand(FULLSCREEN_CURRENT_WINDOW, post("/session/:sessionId/window/fullscreen"));
91-
defineCommand(GET_CURRENT_WINDOW_SIZE, get("/session/:sessionId/window/size"));
92-
defineCommand(SET_CURRENT_WINDOW_SIZE, post("/session/:sessionId/window/size"));
9385

9486
defineCommand(GET_CURRENT_URL, get("/session/:sessionId/url"));
9587
defineCommand(GET, post("/session/:sessionId/url"));
@@ -204,12 +196,14 @@ public HttpRequest encode(Command command) {
204196
if (spec == null) {
205197
throw new UnsupportedCommandException(command.getName());
206198
}
207-
String uri = buildUri(command, spec);
199+
Map<String, ?> parameters = amendParameters(command.getName(), command.getParameters());
200+
String uri = buildUri(command.getName(), command.getSessionId(), parameters, spec);
208201

209202
HttpRequest request = new HttpRequest(spec.method, uri);
210203

211204
if (HttpMethod.POST == spec.method) {
212-
String content = beanToJsonConverter.convert(command.getParameters());
205+
206+
String content = beanToJsonConverter.convert(parameters);
213207
byte[] data = content.getBytes(UTF_8);
214208

215209
request.setHeader(CONTENT_LENGTH, String.valueOf(data.length));
@@ -224,6 +218,8 @@ public HttpRequest encode(Command command) {
224218
return request;
225219
}
226220

221+
protected abstract Map<String,?> amendParameters(String name, Map<String, ?> parameters);
222+
227223
@Override
228224
public Command decode(final HttpRequest encodedCommand) {
229225
final String path = Strings.isNullOrEmpty(encodedCommand.getUri())
@@ -293,7 +289,11 @@ protected static CommandSpec post(String path) {
293289
return new CommandSpec(HttpMethod.POST, path);
294290
}
295291

296-
private String buildUri(Command command, CommandSpec spec) {
292+
private String buildUri(
293+
String commandName,
294+
SessionId sessionId,
295+
Map<String, ?> parameters,
296+
CommandSpec spec) {
297297
StringBuilder builder = new StringBuilder();
298298
for (String part : spec.pathSegments) {
299299
if (part.isEmpty()) {
@@ -302,24 +302,28 @@ private String buildUri(Command command, CommandSpec spec) {
302302

303303
builder.append("/");
304304
if (part.startsWith(":")) {
305-
builder.append(getParameter(part.substring(1), command));
305+
builder.append(getParameter(part.substring(1), commandName, sessionId, parameters));
306306
} else {
307307
builder.append(part);
308308
}
309309
}
310310
return builder.toString();
311311
}
312312

313-
private String getParameter(String parameterName, Command command) {
313+
private String getParameter(
314+
String parameterName,
315+
String commandName,
316+
SessionId sessionId,
317+
Map<String, ?> parameters) {
314318
if ("sessionId".equals(parameterName)) {
315-
SessionId id = command.getSessionId();
316-
checkArgument(id != null, "Session ID may not be null for command %s", command.getName());
319+
SessionId id = sessionId;
320+
checkArgument(id != null, "Session ID may not be null for command %s", commandName);
317321
return id.toString();
318322
}
319323

320-
Object value = command.getParameters().get(parameterName);
324+
Object value = parameters.get(parameterName);
321325
checkArgument(value != null,
322-
"Missing required parameter \"%s\" for command %s", parameterName, command.getName());
326+
"Missing required parameter \"%s\" for command %s", parameterName, commandName);
323327
return Urls.urlEncode(String.valueOf(value));
324328
}
325329

java/client/src/org/openqa/selenium/remote/http/JsonHttpCommandCodec.java

+33
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@
2222
import static org.openqa.selenium.remote.DriverCommand.EXECUTE_ASYNC_SCRIPT;
2323
import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT;
2424
import static org.openqa.selenium.remote.DriverCommand.GET_ALERT_TEXT;
25+
import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_SIZE;
26+
import static org.openqa.selenium.remote.DriverCommand.MAXIMIZE_CURRENT_WINDOW;
2527
import static org.openqa.selenium.remote.DriverCommand.SET_ALERT_VALUE;
28+
import static org.openqa.selenium.remote.DriverCommand.SET_CURRENT_WINDOW_POSITION;
29+
import static org.openqa.selenium.remote.DriverCommand.SET_CURRENT_WINDOW_SIZE;
30+
31+
import com.google.common.collect.ImmutableMap;
32+
33+
import org.openqa.selenium.remote.DriverCommand;
34+
35+
import java.util.Map;
2636

2737
/**
2838
* A command codec that adheres to the Selenium project's JSON/HTTP wire protocol.
@@ -33,6 +43,11 @@
3343
public class JsonHttpCommandCodec extends AbstractHttpCommandCodec {
3444

3545
public JsonHttpCommandCodec() {
46+
defineCommand(MAXIMIZE_CURRENT_WINDOW, post("/session/:sessionId/window/:windowHandle/maximize"));
47+
defineCommand(SET_CURRENT_WINDOW_POSITION, post("/session/:sessionId/window/:windowHandle/position"));
48+
defineCommand(GET_CURRENT_WINDOW_SIZE, get("/session/:sessionId/window/:windowHandle/size"));
49+
defineCommand(SET_CURRENT_WINDOW_SIZE, post("/session/:sessionId/window/:windowHandle/size"));
50+
3651
defineCommand(ACCEPT_ALERT, post("/session/:sessionId/accept_alert"));
3752
defineCommand(DISMISS_ALERT, post("/session/:sessionId/dismiss_alert"));
3853
defineCommand(GET_ALERT_TEXT, get("/session/:sessionId/alert_text"));
@@ -41,4 +56,22 @@ public JsonHttpCommandCodec() {
4156
defineCommand(EXECUTE_SCRIPT, post("/session/:sessionId/execute"));
4257
defineCommand(EXECUTE_ASYNC_SCRIPT, post("/session/:sessionId/execute_async"));
4358
}
59+
60+
@Override
61+
protected Map<String, ?> amendParameters(String name, Map<String, ?> parameters) {
62+
switch (name) {
63+
case DriverCommand.GET_CURRENT_WINDOW_SIZE:
64+
case DriverCommand.MAXIMIZE_CURRENT_WINDOW:
65+
case DriverCommand.SET_CURRENT_WINDOW_SIZE:
66+
case DriverCommand.SET_CURRENT_WINDOW_POSITION:
67+
return ImmutableMap.<String, Object>builder()
68+
.putAll(parameters)
69+
.put("windowHandle", "current")
70+
.put("handle", "current")
71+
.build();
72+
73+
default:
74+
return parameters;
75+
}
76+
}
4477
}

java/client/src/org/openqa/selenium/remote/http/W3CHttpCommandCodec.java

+45
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,20 @@
2222
import static org.openqa.selenium.remote.DriverCommand.EXECUTE_ASYNC_SCRIPT;
2323
import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT;
2424
import static org.openqa.selenium.remote.DriverCommand.GET_ALERT_TEXT;
25+
import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_SIZE;
26+
import static org.openqa.selenium.remote.DriverCommand.MAXIMIZE_CURRENT_WINDOW;
2527
import static org.openqa.selenium.remote.DriverCommand.SET_ALERT_VALUE;
28+
import static org.openqa.selenium.remote.DriverCommand.SET_CURRENT_WINDOW_SIZE;
29+
import static org.openqa.selenium.remote.DriverCommand.SET_CURRENT_WINDOW_POSITION;
30+
31+
import com.google.common.collect.ImmutableMap;
32+
import com.google.common.collect.Iterables;
33+
import com.google.common.collect.Lists;
34+
35+
import org.openqa.selenium.remote.DriverCommand;
36+
import org.openqa.selenium.remote.internal.WebElementToJsonConverter;
37+
38+
import java.util.Map;
2639

2740
/**
2841
* A command codec that adheres to the W3C's WebDriver wire protocol.
@@ -32,6 +45,12 @@
3245
public class W3CHttpCommandCodec extends AbstractHttpCommandCodec {
3346

3447
public W3CHttpCommandCodec() {
48+
defineCommand(MAXIMIZE_CURRENT_WINDOW, post("/session/:sessionId/window/maximize"));
49+
defineCommand(SET_CURRENT_WINDOW_POSITION, post("/session/:sessionId/execute/sync"));
50+
defineCommand(GET_CURRENT_WINDOW_SIZE, get("/session/:sessionId/window/size"));
51+
defineCommand(SET_CURRENT_WINDOW_SIZE, post("/session/:sessionId/window/size"));
52+
53+
3554
defineCommand(ACCEPT_ALERT, post("/session/:sessionId/alert/accept"));
3655
defineCommand(DISMISS_ALERT, post("/session/:sessionId/alert/dismiss"));
3756
defineCommand(GET_ALERT_TEXT, get("/session/:sessionId/alert/text"));
@@ -40,4 +59,30 @@ public W3CHttpCommandCodec() {
4059
defineCommand(EXECUTE_SCRIPT, post("/session/:sessionId/execute/sync"));
4160
defineCommand(EXECUTE_ASYNC_SCRIPT, post("/session/:sessionId/execute/async"));
4261
}
62+
63+
@Override
64+
protected Map<String, ?> amendParameters(String name, Map<String, ?> parameters) {
65+
switch (name) {
66+
case DriverCommand.SET_CURRENT_WINDOW_POSITION:
67+
return toScript(
68+
"window.screenX = arguments[0]; window.screenY = arguments[1]",
69+
parameters.get("x"),
70+
parameters.get("y"));
71+
72+
default:
73+
return parameters;
74+
}
75+
}
76+
77+
private Map<String, ?> toScript(String script, Object... args) {
78+
// Escape the quote marks
79+
script = script.replaceAll("\"", "\\\"");
80+
81+
Iterable<Object> convertedArgs = Iterables.transform(
82+
Lists.newArrayList(args), new WebElementToJsonConverter());
83+
84+
return ImmutableMap.of(
85+
"script", script,
86+
"args", Lists.newArrayList(convertedArgs));
87+
}
4388
}

java/server/src/org/openqa/selenium/remote/server/JsonHttpCommandHandler.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,10 @@ private void setUpMappings() {
264264
addNewMapping(SWITCH_TO_WINDOW, SwitchToWindow.class);
265265
addNewMapping(CLOSE, CloseWindow.class);
266266

267-
addNewMapping(GET_WINDOW_SIZE, GetWindowSize.class);
268267
addNewMapping(GET_CURRENT_WINDOW_SIZE, GetWindowSize.class);
269-
addNewMapping(SET_WINDOW_SIZE, SetWindowSize.class);
270268
addNewMapping(SET_CURRENT_WINDOW_SIZE, SetWindowSize.class);
271269
addNewMapping(GET_WINDOW_POSITION, GetWindowPosition.class);
272-
addNewMapping(SET_WINDOW_POSITION, SetWindowPosition.class);
273-
addNewMapping(MAXIMIZE_WINDOW, MaximizeWindow.class);
270+
addNewMapping(SET_CURRENT_WINDOW_POSITION, SetWindowPosition.class);
274271
addNewMapping(MAXIMIZE_CURRENT_WINDOW, MaximizeWindow.class);
275272
addNewMapping(FULLSCREEN_CURRENT_WINDOW, FullscreenWindow.class);
276273

0 commit comments

Comments
 (0)