Skip to content

Commit

Permalink
JariBakken: Beta implementation of window resize/move for Firefox + R…
Browse files Browse the repository at this point in the history
…uby/Java.

This adds two resources to the wire protocol:

  /session/:sessionId/window/:windowHandle/size
  /session/:sessionId/window/:windowHandle/position

Both resources accept GET and POST requests to get and set the values.

Update issue 174
Labels: -Browser-All Browser-IE Browser-Chrome Browser-HtmlUnit Browser-Opera Lang-CSharp Lang-Python
Status: Started

r14578
  • Loading branch information
jarib committed Nov 7, 2011
1 parent 77f1da7 commit f9cfecd
Show file tree
Hide file tree
Showing 18 changed files with 496 additions and 1 deletion.
40 changes: 40 additions & 0 deletions java/client/src/org/openqa/selenium/WebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ interface Options {
* Returns the interface for controlling IME engines to generate complex-script input.
*/
ImeHandler ime();

/**
* Returns the interface for managing the current window.
*/
@Beta
Window window();
}

/**
Expand Down Expand Up @@ -433,4 +439,38 @@ interface ImeHandler {
*/
void activateEngine(String engine);
}

@Beta
interface Window {
/**
* Set the size of the current window. This will change the outer window dimension,
* not just the view port, synonymous to window.resizeTo() in JS.
*
* @param targetSize The target size.
*/
void setSize(Dimension targetSize);

/**
* Set the position of the current window. This is relative to the upper left corner of the
* screen, synonymous to window.moveTo() in JS.
*
* @param targetPosition The target position of the window.
*/
void setPosition(Point targetPosition);

/**
* Get the size of the current window. This will return the outer window dimension, not just
* the view port.
*
* @return The current window size.
*/
Dimension getSize();

/**
* Get the position of the current window, relative to the upper left corner of the screen.
*
* @return The current window position.
*/
Point getPosition();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,11 @@ public Timeouts timeouts() {
public ImeHandler ime() {
throw new UnsupportedOperationException("Cannot input IME using HtmlUnit.");
}

public Window window() {
throw new UnsupportedOperationException("Window handling not yet implemented in HtmlUnit");
}

}

class HtmlUnitTimeouts implements Timeouts {
Expand Down
5 changes: 5 additions & 0 deletions java/client/src/org/openqa/selenium/remote/DriverCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,9 @@ public interface DriverCommand {
String TOUCH_DOUBLE_TAP = "touchDoubleTap";
String TOUCH_LONG_PRESS = "touchLongPress";
String TOUCH_FLICK = "touchFlick";

String SET_WINDOW_SIZE = "setWindowSize";
String SET_WINDOW_POSITION = "setWindowPosition";
String GET_WINDOW_SIZE = "getWindowSize";
String GET_WINDOW_POSITION = "getWindowPosition";
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
import static org.openqa.selenium.remote.DriverCommand.GET_SESSION_STORAGE_SIZE;
import static org.openqa.selenium.remote.DriverCommand.GET_TITLE;
import static org.openqa.selenium.remote.DriverCommand.GET_WINDOW_HANDLES;
import static org.openqa.selenium.remote.DriverCommand.GET_WINDOW_SIZE;
import static org.openqa.selenium.remote.DriverCommand.GET_WINDOW_POSITION;
import static org.openqa.selenium.remote.DriverCommand.GO_BACK;
import static org.openqa.selenium.remote.DriverCommand.GO_FORWARD;
import static org.openqa.selenium.remote.DriverCommand.HOVER_OVER_ELEMENT;
Expand Down Expand Up @@ -138,6 +140,8 @@
import static org.openqa.selenium.remote.DriverCommand.SET_SCREEN_ORIENTATION;
import static org.openqa.selenium.remote.DriverCommand.SET_SCRIPT_TIMEOUT;
import static org.openqa.selenium.remote.DriverCommand.SET_SESSION_STORAGE_ITEM;
import static org.openqa.selenium.remote.DriverCommand.SET_WINDOW_POSITION;
import static org.openqa.selenium.remote.DriverCommand.SET_WINDOW_SIZE;
import static org.openqa.selenium.remote.DriverCommand.SUBMIT_ELEMENT;
import static org.openqa.selenium.remote.DriverCommand.SWITCH_TO_FRAME;
import static org.openqa.selenium.remote.DriverCommand.SWITCH_TO_WINDOW;
Expand Down Expand Up @@ -275,6 +279,10 @@ public HttpCommandExecutor(URL addressOfRemoteServer) {
.put(DELETE_COOKIE, delete("/session/:sessionId/cookie/:name"))
.put(SWITCH_TO_FRAME, post("/session/:sessionId/frame"))
.put(SWITCH_TO_WINDOW, post("/session/:sessionId/window"))
.put(GET_WINDOW_SIZE, get("/session/:sessionId/window/:windowHandle/size"))
.put(GET_WINDOW_POSITION, get("/session/:sessionId/window/:windowHandle/position"))
.put(SET_WINDOW_SIZE, post("/session/:sessionId/window/:windowHandle/size"))
.put(SET_WINDOW_POSITION, post("/session/:sessionId/window/:windowHandle/position"))
.put(CLOSE, delete("/session/:sessionId/window"))
.put(DRAG_ELEMENT, post("/session/:sessionId/element/:id/drag"))
.put(GET_ELEMENT_VALUE_OF_CSS_PROPERTY,
Expand Down
44 changes: 44 additions & 0 deletions java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,13 @@ public ImeHandler ime() {
return new RemoteInputMethodManager();
}

@Beta
public Window window() {
return new RemoteWindow();
}

protected class RemoteInputMethodManager implements WebDriver.ImeHandler {

public List<String> getAvailableEngines() {
Response response = execute(DriverCommand.IME_GET_AVAILABLE_ENGINES);
return (List<String>) response.getValue();
Expand Down Expand Up @@ -544,6 +550,44 @@ public Timeouts setScriptTimeout(long time, TimeUnit unit) {
return this;
}
} // timeouts class.

@Beta
protected class RemoteWindow implements Window {

public void setSize(Dimension targetSize) {
execute(DriverCommand.SET_WINDOW_SIZE,
ImmutableMap.of("windowHandle", "current", "width", targetSize.width, "height", targetSize.height));
}

public void setPosition(Point targetPosition) {
execute(DriverCommand.SET_WINDOW_POSITION,
ImmutableMap.of("windowHandle", "current", "x", targetPosition.x, "y", targetPosition.y));
}

@SuppressWarnings({"unchecked"})
public Dimension getSize() {
Response response = execute(DriverCommand.GET_WINDOW_SIZE,
ImmutableMap.of("windowHandle", "current"));
Map<String, Object> rawSize = (Map<String, Object>) response.getValue();

int width = ((Number) rawSize.get("width")).intValue();
int height = ((Number) rawSize.get("height")).intValue();

return new Dimension(width, height);
}

@SuppressWarnings({"unchecked"})
public Point getPosition() {
Response response = execute(DriverCommand.GET_WINDOW_POSITION,
ImmutableMap.of("windowHandle", "current"));
Map<String, Object> rawPoint = (Map<String, Object>) response.getValue();

int x = ((Number) rawPoint.get("x")).intValue();
int y = ((Number) rawPoint.get("y")).intValue();

return new Point(x, y);
}
}
}

private class RemoteNavigation implements Navigation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.support.events;

import org.openqa.selenium.Alert;
import org.openqa.selenium.Beta;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Dimension;
Expand Down Expand Up @@ -518,6 +519,11 @@ public Timeouts timeouts() {
public ImeHandler ime() {
throw new UnsupportedOperationException("Driver does not support IME interactions");
}

@Beta
public Window window() {
return new EventFiringWindow(options.window());
}
}

private class EventFiringTimeouts implements Timeouts {
Expand Down Expand Up @@ -575,4 +581,29 @@ public Alert alert() {
return targetLocator.alert();
}
}

@Beta
private class EventFiringWindow implements Window {
private final Window window;

EventFiringWindow(Window window) {
this.window = window;
}

public void setSize(Dimension targetSize) {
window.setSize(targetSize);
}

public void setPosition(Point targetLocation) {
window.setPosition(targetLocation);
}

public Dimension getSize() {
return window.getSize();
}

public Point getPosition() {
return window.getPosition();
}
}
}
76 changes: 76 additions & 0 deletions java/client/test/org/openqa/selenium/WindowTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2011 WebDriver committers
Copyright 2011 Software Freedom Conservancy.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.openqa.selenium;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.openqa.selenium.Ignore.Driver.ANDROID;
import static org.openqa.selenium.Ignore.Driver.CHROME;
import static org.openqa.selenium.Ignore.Driver.HTMLUNIT;
import static org.openqa.selenium.Ignore.Driver.IE;
import static org.openqa.selenium.Ignore.Driver.IPHONE;
import static org.openqa.selenium.Ignore.Driver.OPERA;
import static org.openqa.selenium.Ignore.Driver.SELENESE;

@Ignore(value = {ANDROID, CHROME, HTMLUNIT, IE, IPHONE, OPERA, SELENESE},
reason = "Not yet implemented.")
public class WindowTest extends AbstractDriverTestCase {

public void testGetsTheSizeOfTheCurrentWindow() {
Dimension size = driver.manage().window().getSize();

assertThat(size.width, is(greaterThan(0)));
assertThat(size.height, is(greaterThan(0)));
}

public void testSetsTheSizeOfTheCurrentWindow() {
WebDriver.Window window = driver.manage().window();
Dimension size = window.getSize();

// resize relative to the initial size, since we don't know what it is
Dimension targetSize = new Dimension(size.width - 20, size.height - 20);
window.setSize(targetSize);

Dimension newSize = window.getSize();
assertEquals(targetSize.width, newSize.width);
assertEquals(targetSize.height, newSize.height);
}

public void testGetsThePositionOfTheCurrentWindow() {
Point position = driver.manage().window().getPosition();

assertThat(position.x, is(greaterThanOrEqualTo(0)));
assertThat(position.y, is(greaterThanOrEqualTo(0)));
}

public void testSetsThePositionOfTheCurrentWindow() {
WebDriver.Window window = driver.manage().window();
Point position = window.getPosition();

Point targetPosition = new Point(position.x + 10, position.y + 10);
window.setPosition(targetPosition);

Point newLocation = window.getPosition();

assertEquals(targetPosition.x, newLocation.x);
assertEquals(targetPosition.y, newLocation.y);
}

}
65 changes: 65 additions & 0 deletions javascript/atoms/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ goog.require('bot.ErrorCode');
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.math.Size');
goog.require('goog.math.Coordinate');
goog.require('goog.userAgent');


Expand Down Expand Up @@ -142,3 +143,67 @@ bot.window.getInteractableSize = function(opt_win) {

return new goog.math.Size(width, height);
};


/**
* Determine the outer size of the window.
*
* @param {!Window=} opt_win Window to determine the size of. Defaults to
* bot.getWindow().
* @return {!goog.math.Size} The calculated size.
*/
bot.window.getSize = function(opt_win) {
var win = opt_win || bot.getWindow();

var width = win.outerWidth;
var height = win.outerHeight;

return new goog.math.Size(width, height);
};


/** Set the outer size of the window.
*
* @param {!goog.math.Size} size The new window size.
* @param {!Window=} opt_win Window to determine the size of. Defaults to
* bot.getWindow().
*/
bot.window.setSize = function(size, opt_win) {
var win = opt_win || bot.getWindow();

win.resizeTo(size.width, size.height);
};


/** Get the position of the window.
*
* @param {!Window=} opt_win Window to determine the position of. Defaults to
* bot.getWindow().
* @return {!goog.math.Coordinate} The position of the window.
*/
bot.window.getPosition = function(opt_win) {
var win = opt_win || bot.getWindow();
var x, y;

if (goog.userAgent.IE) {
x = win.screenLeft;
y = win.screenTop;
} else {
x = win.screenX;
y = win.screenY;
}

return new goog.math.Coordinate(x, y);
};


/** Set the position of the window.
*
* @param {!goog.math.Coordinate} targetPosition The target position.
* @param {!Window=} opt_win Window to set the position of. Defaults to
* bot.getWindow().
*/
bot.window.setPosition = function(targetPosition, opt_win) {
var win = opt_win || bot.getWindow();
win.moveTo(targetPosition.x, targetPosition.y);
};
8 changes: 8 additions & 0 deletions javascript/firefox-driver/extension/components/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ Dispatcher.prototype.init_ = function() {
on(Request.Method.POST, Dispatcher.executeAs('switchToWindow')).
on(Request.Method.DELETE, Dispatcher.executeAs('close'));

this.bind_('/session/:sessionId/window/:windowHandle/size').
on(Request.Method.POST, Dispatcher.executeAs('setWindowSize')).
on(Request.Method.GET, Dispatcher.executeAs('getWindowSize'));

this.bind_('/session/:sessionId/window/:windowHandle/position').
on(Request.Method.POST, Dispatcher.executeAs('setWindowPosition')).
on(Request.Method.GET, Dispatcher.executeAs('getWindowPosition'));

this.bind_('/session/:sessionId/screenshot').
on(Request.Method.GET, Dispatcher.executeAs('screenshot'));

Expand Down
Loading

0 comments on commit f9cfecd

Please sign in to comment.