1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00

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.
This commit is contained in:
Tom Lea 2012-03-29 17:59:43 +01:00 committed by Joe Ferris
parent f190f51652
commit 04fde02ad7
8 changed files with 92 additions and 0 deletions

View file

@ -79,6 +79,10 @@ class Capybara::Driver::Webkit
browser.status_code browser.status_code
end end
def resize_window(width, height)
browser.resize_window(width, height)
end
def within_frame(frame_id_or_index) def within_frame(frame_id_or_index)
browser.frame_focus(frame_id_or_index) browser.frame_focus(frame_id_or_index)
begin begin

View file

@ -129,6 +129,10 @@ class Capybara::Driver::Webkit
command("SetProxy") command("SetProxy")
end end
def resize_window(width, height)
command("ResizeWindow", width.to_i, height.to_i)
end
private private
def start_server def start_server

View file

@ -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
<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
after(:all) { @driver.reset! }
end

View file

@ -22,6 +22,7 @@
#include "ConsoleMessages.h" #include "ConsoleMessages.h"
#include "RequestedUrl.h" #include "RequestedUrl.h"
#include "CurrentUrl.h" #include "CurrentUrl.h"
#include "ResizeWindow.h"
CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) { CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
m_page = page; m_page = page;

16
src/ResizeWindow.cpp Normal file
View file

@ -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));
}

12
src/ResizeWindow.h Normal file
View file

@ -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 file

@ -25,3 +25,4 @@ CHECK_COMMAND(SetProxy)
CHECK_COMMAND(ConsoleMessages) CHECK_COMMAND(ConsoleMessages)
CHECK_COMMAND(RequestedUrl) CHECK_COMMAND(RequestedUrl)
CHECK_COMMAND(CurrentUrl) CHECK_COMMAND(CurrentUrl)
CHECK_COMMAND(ResizeWindow)

View file

@ -2,6 +2,7 @@ TEMPLATE = app
TARGET = webkit_server TARGET = webkit_server
DESTDIR = . DESTDIR = .
HEADERS = \ HEADERS = \
ResizeWindow.h \
CurrentUrl.h \ CurrentUrl.h \
RequestedUrl.h \ RequestedUrl.h \
ConsoleMessages.h \ ConsoleMessages.h \
@ -38,6 +39,7 @@ HEADERS = \
PageLoadingCommand.h \ PageLoadingCommand.h \
SOURCES = \ SOURCES = \
ResizeWindow.cpp \
CurrentUrl.cpp \ CurrentUrl.cpp \
RequestedUrl.cpp \ RequestedUrl.cpp \
ConsoleMessages.cpp \ ConsoleMessages.cpp \