From 04fde02ad789735a3a6db9a92fe133d702b5ae7b Mon Sep 17 00:00:00 2001 From: Tom Lea Date: Thu, 29 Mar 2012 17:59:43 +0100 Subject: [PATCH] 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. --- lib/capybara/driver/webkit.rb | 4 +++ lib/capybara/driver/webkit/browser.rb | 4 +++ spec/driver_resize_window_spec.rb | 52 +++++++++++++++++++++++++++ src/CommandFactory.cpp | 1 + src/ResizeWindow.cpp | 16 +++++++++ src/ResizeWindow.h | 12 +++++++ src/find_command.h | 1 + src/webkit_server.pro | 2 ++ 8 files changed, 92 insertions(+) create mode 100644 spec/driver_resize_window_spec.rb create mode 100644 src/ResizeWindow.cpp create mode 100644 src/ResizeWindow.h diff --git a/lib/capybara/driver/webkit.rb b/lib/capybara/driver/webkit.rb index 06e9399..6faef67 100644 --- a/lib/capybara/driver/webkit.rb +++ b/lib/capybara/driver/webkit.rb @@ -79,6 +79,10 @@ class Capybara::Driver::Webkit 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 diff --git a/lib/capybara/driver/webkit/browser.rb b/lib/capybara/driver/webkit/browser.rb index 9d0c2fc..ec43ba1 100644 --- a/lib/capybara/driver/webkit/browser.rb +++ b/lib/capybara/driver/webkit/browser.rb @@ -129,6 +129,10 @@ class Capybara::Driver::Webkit command("SetProxy") end + def resize_window(width, height) + command("ResizeWindow", width.to_i, height.to_i) + end + private def start_server diff --git a/spec/driver_resize_window_spec.rb b/spec/driver_resize_window_spec.rb new file mode 100644 index 0000000..acdc135 --- /dev/null +++ b/spec/driver_resize_window_spec.rb @@ -0,0 +1,52 @@ +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 + + +

UNKNOWN

+ + + + + + 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 + + after(:all) { @driver.reset! } +end diff --git a/src/CommandFactory.cpp b/src/CommandFactory.cpp index 78d6b61..9e2ec24 100644 --- a/src/CommandFactory.cpp +++ b/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; diff --git a/src/ResizeWindow.cpp b/src/ResizeWindow.cpp new file mode 100644 index 0000000..c630744 --- /dev/null +++ b/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)); +} + diff --git a/src/ResizeWindow.h b/src/ResizeWindow.h new file mode 100644 index 0000000..f52e473 --- /dev/null +++ b/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(); +}; + diff --git a/src/find_command.h b/src/find_command.h index 8f35e07..63f37cc 100644 --- a/src/find_command.h +++ b/src/find_command.h @@ -25,3 +25,4 @@ CHECK_COMMAND(SetProxy) CHECK_COMMAND(ConsoleMessages) CHECK_COMMAND(RequestedUrl) CHECK_COMMAND(CurrentUrl) +CHECK_COMMAND(ResizeWindow) diff --git a/src/webkit_server.pro b/src/webkit_server.pro index c7e59cc..0b74e43 100644 --- a/src/webkit_server.pro +++ b/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 \