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:
parent
f190f51652
commit
04fde02ad7
8 changed files with 92 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
52
spec/driver_resize_window_spec.rb
Normal file
52
spec/driver_resize_window_spec.rb
Normal 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
|
|
@ -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;
|
||||
|
|
16
src/ResizeWindow.cpp
Normal file
16
src/ResizeWindow.cpp
Normal 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
12
src/ResizeWindow.h
Normal 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();
|
||||
};
|
||||
|
|
@ -25,3 +25,4 @@ CHECK_COMMAND(SetProxy)
|
|||
CHECK_COMMAND(ConsoleMessages)
|
||||
CHECK_COMMAND(RequestedUrl)
|
||||
CHECK_COMMAND(CurrentUrl)
|
||||
CHECK_COMMAND(ResizeWindow)
|
||||
|
|
|
@ -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 \
|
||||
|
|
Loading…
Reference in a new issue