Skip to content

Commit 4f28354

Browse files
committed
Allow commands to be aliased in the CommandCodec
Rather than just relying on the URL being the same, make the fact that some commands are aliases of another clear.
1 parent ff35766 commit 4f28354

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,11 @@ public interface CommandCodec<T> {
5050
* Enhance this command codec with additional commands.
5151
*/
5252
void defineCommand(String name, HttpMethod method, String pathPattern);
53+
54+
/**
55+
* Allow commands to have aliases.
56+
* @param commandName The command being added.
57+
* @param isAnAliasFor The command name that this is an alias for.
58+
*/
59+
void alias(String commandName, String isAnAliasFor);
5360
}

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public abstract class AbstractHttpCommandCodec implements CommandCodec<HttpReque
151151
private static final String SESSION_ID_PARAM = "sessionId";
152152

153153
private final BiMap<String, CommandSpec> nameToSpec = HashBiMap.create();
154+
private final Map<String, String> aliases = new HashMap<>();
154155
private final BeanToJsonConverter beanToJsonConverter = new BeanToJsonConverter();
155156
private final JsonToBeanConverter jsonToBeanConverter = new JsonToBeanConverter();
156157

@@ -277,12 +278,13 @@ public AbstractHttpCommandCodec() {
277278

278279
@Override
279280
public HttpRequest encode(Command command) {
280-
CommandSpec spec = nameToSpec.get(command.getName());
281+
String name = aliases.getOrDefault(command.getName(), command.getName());
282+
CommandSpec spec = nameToSpec.get(name);
281283
if (spec == null) {
282284
throw new UnsupportedCommandException(command.getName());
283285
}
284286
Map<String, ?> parameters = amendParameters(command.getName(), command.getParameters());
285-
String uri = buildUri(command.getName(), command.getSessionId(), parameters, spec);
287+
String uri = buildUri(name, command.getSessionId(), parameters, spec);
286288

287289
HttpRequest request = new HttpRequest(spec.method, uri);
288290

@@ -357,6 +359,11 @@ public void defineCommand(String name, HttpMethod method, String pathPattern) {
357359
defineCommand(name, new CommandSpec(method, pathPattern));
358360
}
359361

362+
@Override
363+
public void alias(String commandName, String isAnAliasFor) {
364+
aliases.put(commandName, isAnAliasFor);
365+
}
366+
360367
protected void defineCommand(String name, CommandSpec spec) {
361368
checkNotNull(name, "null name");
362369
nameToSpec.put(name, spec);

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@
6161
public class W3CHttpCommandCodec extends AbstractHttpCommandCodec {
6262

6363
public W3CHttpCommandCodec() {
64-
defineCommand(GET_ELEMENT_ATTRIBUTE, post("/session/:sessionId/execute/sync"));
65-
defineCommand(GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW, post("/session/:sessionId/execute/sync"));
66-
defineCommand(SUBMIT_ELEMENT, post("/session/:sessionId/execute/sync"));
64+
alias(GET_ELEMENT_ATTRIBUTE, EXECUTE_SCRIPT);
65+
alias(GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW, EXECUTE_SCRIPT);
66+
alias(SUBMIT_ELEMENT, EXECUTE_SCRIPT);
6767

6868
defineCommand(EXECUTE_SCRIPT, post("/session/:sessionId/execute/sync"));
6969
defineCommand(EXECUTE_ASYNC_SCRIPT, post("/session/:sessionId/execute/async"));
7070

71-
defineCommand(GET_PAGE_SOURCE, post("/session/:sessionId/execute/sync"));
71+
alias(GET_PAGE_SOURCE, EXECUTE_SCRIPT);
7272

7373
defineCommand(MAXIMIZE_CURRENT_WINDOW, post("/session/:sessionId/window/maximize"));
74-
defineCommand(GET_CURRENT_WINDOW_POSITION, get("/session/:sessionId/execute/sync"));
75-
defineCommand(SET_CURRENT_WINDOW_POSITION, post("/session/:sessionId/execute/sync"));
74+
alias(GET_CURRENT_WINDOW_POSITION, EXECUTE_SCRIPT);
75+
alias(SET_CURRENT_WINDOW_POSITION, EXECUTE_SCRIPT);
7676
defineCommand(GET_CURRENT_WINDOW_SIZE, get("/session/:sessionId/window/size"));
7777
defineCommand(SET_CURRENT_WINDOW_SIZE, post("/session/:sessionId/window/size"));
7878
defineCommand(GET_CURRENT_WINDOW_HANDLE, get("/session/:sessionId/window"));

0 commit comments

Comments
 (0)