Skip to content

Commit

Permalink
DanielWagnerHall for ZacCampbell: Implementing window.maximise/restor…
Browse files Browse the repository at this point in the history
…e for firefox. Update issue SeleniumHQ#3489 Patch applied

r16287
  • Loading branch information
illicitonion committed Mar 19, 2012
1 parent f82bd93 commit f0cf95d
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 1 deletion.
10 changes: 10 additions & 0 deletions java/client/src/org/openqa/selenium/WebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -498,5 +498,15 @@ interface Window {
* @return The current window position.
*/
Point getPosition();

/**
* Maximizes the current window if it is not already maximized
*/
void maximize();

/**
* Restores the current window if it is maximized
*/
void restore();
}
}
3 changes: 3 additions & 0 deletions java/client/src/org/openqa/selenium/remote/DriverCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,13 @@ public interface DriverCommand {
String TOUCH_LONG_PRESS = "touchLongPress";
String TOUCH_FLICK = "touchFlick";

// Window API (beta)
String SET_WINDOW_SIZE = "setWindowSize";
String SET_WINDOW_POSITION = "setWindowPosition";
String GET_WINDOW_SIZE = "getWindowSize";
String GET_WINDOW_POSITION = "getWindowPosition";
String MAXIMIZE_WINDOW = "maximizeWindow";
String RESTORE_WINDOW = "restoreWindow";

String GET_LOGS = "getLogs";
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ public HttpCommandExecutor(URL addressOfRemoteServer) {
.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(MAXIMIZE_WINDOW, post("/session/:sessionId/window/:windowHandle/maximize"))
.put(RESTORE_WINDOW, post("/session/:sessionId/window/:windowHandle/restore"))
.put(CLOSE, delete("/session/:sessionId/window"))
.put(DRAG_ELEMENT, post("/session/:sessionId/element/:id/drag"))
.put(GET_ELEMENT_VALUE_OF_CSS_PROPERTY,
Expand Down
10 changes: 10 additions & 0 deletions java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,16 @@ public Point getPosition() {

return new Point(x, y);
}

public void maximize() {
execute(DriverCommand.MAXIMIZE_WINDOW,
ImmutableMap.of("windowHandle", "current"));
}

public void restore() {
execute(DriverCommand.RESTORE_WINDOW,
ImmutableMap.of("windowHandle", "current"));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,5 +617,13 @@ public Dimension getSize() {
public Point getPosition() {
return window.getPosition();
}

public void maximize() {
window.maximize();
}

public void restore() {
window.restore();
}
}
}
43 changes: 43 additions & 0 deletions java/client/test/org/openqa/selenium/WindowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@

package org.openqa.selenium;

import java.util.concurrent.Callable;

import org.hamcrest.Matcher;
import org.junit.Test;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -83,4 +87,43 @@ public void testSetsThePositionOfTheCurrentWindow() {
assertEquals(targetPosition.y, newLocation.y);
}

@Ignore({ANDROID, CHROME, HTMLUNIT, IE, IPHONE, OPERA, SELENESE})
@Test
public void testCanMaximizeTheWindow() throws InterruptedException {
WebDriver.Window window = driver.manage().window();

window.restore();
// TODO convert to WebDriverWait
Thread.sleep(500);

Dimension size = window.getSize();

window.maximize();
// TODO convert to WebDriverWait
Thread.sleep(500);

Dimension newSize = window.getSize();
assertThat(newSize.width, greaterThan(size.width));
assertThat(newSize.height, greaterThan(size.height));
}

@Ignore({ANDROID, CHROME, HTMLUNIT, IE, IPHONE, OPERA, SELENESE})
@Test
public void testCanRestoreTheWindow() throws InterruptedException {
WebDriver.Window window = driver.manage().window();
window.maximize();

// TODO convert to WebDriverWait
Thread.sleep(500);

Dimension size = window.getSize();

window.restore();
// TODO convert to WebDriverWait
Thread.sleep(500);

Dimension newSize = window.getSize();
assertThat(newSize.width, lessThan(size.width));
assertThat(newSize.height, lessThan(size.height));
}
}
6 changes: 6 additions & 0 deletions javascript/firefox-driver/js/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ Dispatcher.prototype.init_ = function() {
on(Request.Method.POST, Dispatcher.executeAs('setWindowPosition')).
on(Request.Method.GET, Dispatcher.executeAs('getWindowPosition'));

this.bind_('/session/:sessionId/window/:windowHandle/maximize').
on(Request.Method.POST, Dispatcher.executeAs('maximizeWindow'));

this.bind_('/session/:sessionId/window/:windowHandle/restore').
on(Request.Method.POST, Dispatcher.executeAs('restoreWindow'));

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

Expand Down
42 changes: 41 additions & 1 deletion javascript/firefox-driver/js/firefoxDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1441,10 +1441,50 @@ FirefoxDriver.prototype.setWindowPosition = function(respond, parameters) {
respond.send();
};

// TODO(jari): could this be made into a precondition?
FirefoxDriver.prototype.maximizeWindow = function(respond, parameters) {
this.assertTargetsCurrentWindow_(parameters);

var documentWindow = respond.session.getWindow();
var chromeWindow = getChromeWindowFromDocumentWindow(documentWindow);

chromeWindow.maximize();

respond.send();
};


FirefoxDriver.prototype.restoreWindow = function(respond, parameters) {
this.assertTargetsCurrentWindow_(parameters);

var documentWindow = respond.session.getWindow();
var chromeWindow = getChromeWindowFromDocumentWindow(documentWindow);

chromeWindow.restore();

respond.send();
};

function getChromeWindowFromDocumentWindow(documentWindow){
// Find the chrome window for the requested document window.
// This will ignore unfocused tabs
var wm = fxdriver.moz.getService(
"@mozilla.org/appshell/window-mediator;1", "nsIWindowMediator");
var allWindows = wm.getEnumerator("navigator:browser");

while (allWindows.hasMoreElements()) {
var chromeWindow = allWindows.getNext()

if (chromeWindow.gBrowser.contentWindow == documentWindow) {
return chromeWindow;
}
}
}

//TODO(jari): could this be made into a precondition?
FirefoxDriver.prototype.assertTargetsCurrentWindow_ = function(parameters) {
if (parameters.windowHandle != "current") {
throw new WebDriverError(bot.ErrorCode.UNSUPPORTED_OPERATION,
'Window operations are only supported for the currently focused window.');
}
};

10 changes: 10 additions & 0 deletions wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,16 @@ def main():
SetReturnType('{x: number, y: number}', 'The X and Y coordinates for the window, \
relative to the upper left corner of the screen.').
AddError('NoSuchWindow', 'If the specified window cannot be found.'))

resources.append(
SessionResource('/session/:sessionId/window/:windowHandle/maximize').
Post('''Maximize the currently selected window if not already maximized.''').
AddError('NoSuchWindow', 'If the specified window cannot be found.'))

resources.append(
SessionResource('/session/:sessionId/window/:windowHandle/restore').
Post('''Restore currently selected window if maximized.''').
AddError('NoSuchWindow', 'If the specified window cannot be found.'))

resources.append(
SessionResource('/session/:sessionId/cookie').
Expand Down

0 comments on commit f0cf95d

Please sign in to comment.