Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow the viewport to be resized #307

Closed
wants to merge 2 commits into from

2 participants

@cwninja

Use case

We're currently migrating our old selenium acceptance tests over, and the one blocker remaining is checking that large window sizes inject an iframe/lightbox into the client's page, while small window sizes navigate to the page normally rendered inside the iframe.

Features

This pull request adds a Capybara::Driver::Webkit#resize_window(width, height) method, to change the size of the browser viewport.

Misc

I've included specs, but could not find where is appropriate to document this feature, so have just left it alone. I hope this is ok.

cwninja added some commits
@cwninja cwninja Add a resize_window method to the driver.
Allow users to resize the webkit viewport via
Capybara::Driver::Webkit#resize_window(width, height).

This can be called before or after page load, and fires resize events as expected.
52c7c99
@cwninja cwninja Set the window to the default size on #reset!
This should prevent state leakage between test cases.
029484e
@jferris
Owner

Thanks. I merged this in.

@jferris jferris closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 29, 2012
  1. @cwninja

    Add a resize_window method to the driver.

    cwninja authored
    Allow users to resize the webkit viewport via
    Capybara::Driver::Webkit#resize_window(width, height).
    
    This can be called before or after page load, and fires resize events as expected.
Commits on Mar 30, 2012
  1. @cwninja

    Set the window to the default size on #reset!

    cwninja authored
    This should prevent state leakage between test cases.
This page is out of date. Refresh to see the latest.
View
4 lib/capybara/driver/webkit.rb
@@ -79,6 +79,10 @@ def status_code
browser.status_code
end
+ def resize_window(width, height)
+ browser.resize_window(width, height)
+ end
+
def within_frame(frame_id_or_index)
browser.frame_focus(frame_id_or_index)
begin
View
4 lib/capybara/driver/webkit/browser.rb
@@ -129,6 +129,10 @@ def clear_proxy
command("SetProxy")
end
+ def resize_window(width, height)
+ command("ResizeWindow", width.to_i, height.to_i)
+ end
+
private
def start_server
View
59 spec/driver_resize_window_spec.rb
@@ -0,0 +1,59 @@
+require 'spec_helper'
+require 'capybara/driver/webkit'
+
+describe Capybara::Driver::Webkit, "#resize_window(width, height)" do
+
+ before(:all) do
+ app = lambda do |env|
+ body = <<-HTML
+ <html>
+ <body>
+ <h1 id="dimentions">UNKNOWN</h1>
+
+ <script>
+ window.onload = window.onresize = function(){
+ document.getElementById("dimentions").innerHTML = "[" + window.innerWidth + "x" + window.innerHeight + "]";
+ };
+ </script>
+
+ </body>
+ </html>
+ HTML
+
+ [
+ 200,
+ { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
+ [body]
+ ]
+ end
+
+ @driver = Capybara::Driver::Webkit.new(app, :browser => $webkit_browser)
+ end
+
+ DEFAULT_DIMENTIONS = "[1680x1050]"
+
+ it "resizes the window to the specified size" do
+ @driver.visit("/")
+
+ @driver.resize_window(800, 600)
+ @driver.body.should include("[800x600]")
+
+ @driver.resize_window(300, 100)
+ @driver.body.should include("[300x100]")
+ end
+
+ it "resizes the window to the specified size even before the document has loaded" do
+ @driver.resize_window(800, 600)
+ @driver.visit("/")
+ @driver.body.should include("[800x600]")
+ end
+
+ it "resets the window to the default size when the driver is reset" do
+ @driver.resize_window(800, 600)
+ @driver.reset!
+ @driver.visit("/")
+ @driver.body.should include(DEFAULT_DIMENTIONS)
+ end
+
+ after(:all) { @driver.reset! }
+end
View
1  src/CommandFactory.cpp
@@ -22,6 +22,7 @@
#include "ConsoleMessages.h"
#include "RequestedUrl.h"
#include "CurrentUrl.h"
+#include "ResizeWindow.h"
CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
m_page = page;
View
1  src/Reset.cpp
@@ -13,6 +13,7 @@ void Reset::start() {
page()->setUserAgent(NULL);
page()->resetResponseHeaders();
page()->resetConsoleMessages();
+ page()->resetWindowSize();
resetHistory();
emit finished(new Response(true));
}
View
16 src/ResizeWindow.cpp
@@ -0,0 +1,16 @@
+#include "ResizeWindow.h"
+#include "WebPage.h"
+
+ResizeWindow::ResizeWindow(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {
+}
+
+void ResizeWindow::start() {
+ int width = arguments()[0].toInt();
+ int height = arguments()[1].toInt();
+
+ QSize size(width, height);
+ page()->setViewportSize(size);
+
+ emit finished(new Response(true));
+}
+
View
12 src/ResizeWindow.h
@@ -0,0 +1,12 @@
+#include "Command.h"
+
+class WebPage;
+
+class ResizeWindow : public Command {
+ Q_OBJECT
+
+ public:
+ ResizeWindow(WebPage *page, QStringList &arguments, QObject *parent = 0);
+ virtual void start();
+};
+
View
4 src/WebPage.cpp
@@ -20,6 +20,10 @@ WebPage::WebPage(QObject *parent) : QWebPage(parent) {
this, SLOT(frameCreated(QWebFrame *)));
connect(this, SIGNAL(unsupportedContent(QNetworkReply*)),
this, SLOT(handleUnsupportedContent(QNetworkReply*)));
+ resetWindowSize();
+}
+
+void WebPage::resetWindowSize() {
this->setViewportSize(QSize(1680, 1050));
}
View
1  src/WebPage.h
@@ -19,6 +19,7 @@ class WebPage : public QWebPage {
bool ignoreSslErrors();
QString consoleMessages();
void resetConsoleMessages();
+ void resetWindowSize();
public slots:
bool shouldInterruptJavaScript();
View
1  src/find_command.h
@@ -25,3 +25,4 @@ CHECK_COMMAND(SetProxy)
CHECK_COMMAND(ConsoleMessages)
CHECK_COMMAND(RequestedUrl)
CHECK_COMMAND(CurrentUrl)
+CHECK_COMMAND(ResizeWindow)
View
2  src/webkit_server.pro
@@ -2,6 +2,7 @@ TEMPLATE = app
TARGET = webkit_server
DESTDIR = .
HEADERS = \
+ ResizeWindow.h \
CurrentUrl.h \
RequestedUrl.h \
ConsoleMessages.h \
@@ -38,6 +39,7 @@ HEADERS = \
PageLoadingCommand.h \
SOURCES = \
+ ResizeWindow.cpp \
CurrentUrl.cpp \
RequestedUrl.cpp \
ConsoleMessages.cpp \
Something went wrong with that request. Please try again.