Skip to content

Commit

Permalink
Add option to disable image loading in WebKit.
Browse files Browse the repository at this point in the history
  • Loading branch information
seangeo authored and halogenandtoast committed May 11, 2012
1 parent 5c19691 commit 646eabc
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 4 deletions.
1 change: 0 additions & 1 deletion lib/capybara/driver/webkit.rb
Expand Up @@ -24,7 +24,6 @@ def initialize(app, options={})
@rack_server = Capybara::Server.new(@app)
@rack_server.boot if Capybara.run_server
@browser = options[:browser] || Browser.new(Connection.new(options))
@browser.ignore_ssl_errors if options[:ignore_ssl_errors]
end

def current_url
Expand Down
6 changes: 5 additions & 1 deletion lib/capybara/driver/webkit/browser.rb
Expand Up @@ -77,6 +77,10 @@ def ignore_ssl_errors
command("IgnoreSslErrors")
end

def set_skip_image_loading(skip_image_loading)
command("SetSkipImageLoading", skip_image_loading)
end

def command(name, *args)
@connection.puts name
@connection.puts args.size
Expand Down Expand Up @@ -134,7 +138,7 @@ def check

if result.nil?
raise WebkitNoResponseError, "No response received from the server."
elsif result != 'ok'
elsif result != 'ok'
raise WebkitInvalidResponseError, read_response
end

Expand Down
75 changes: 75 additions & 0 deletions spec/browser_spec.rb
Expand Up @@ -14,6 +14,11 @@
browser.ignore_ssl_errors
end
end
let(:browser_skip_images) do
Capybara::Driver::Webkit::Browser.new(Capybara::Driver::Webkit::Connection.new).tap do |browser|
browser.set_skip_image_loading(true)
end
end

context 'handling of SSL validation errors' do
before do
Expand Down Expand Up @@ -59,6 +64,76 @@
end
end

context "skip image loading" do
before(:each) do
# set up minimal HTTP server
@host = "127.0.0.1"
@server = TCPServer.new(@host, 0)
@port = @server.addr[1]
@received_requests = []

@server_thread = Thread.new(@server) do |serv|
while conn = serv.accept do
# read request
request = []
until (line = conn.readline.strip).empty?
request << line
end

@received_requests << request.join("\n")

# write response
html = <<-HTML
<html>
<head>
<style>
body {
background-image: url(/path/to/bgimage);
}
</style>
</head>
<body>
<img src="/path/to/image"/>
</body>
</html>
HTML
conn.write "HTTP/1.1 200 OK\r\n"
conn.write "Content-Type:text/html\r\n"
conn.write "Content-Length: %i\r\n" % html.size
conn.write "\r\n"
conn.write html
conn.write("\r\n\r\n")
conn.close
end
end
end

after(:each) do
@server_thread.kill
@server.close
end

it "should load images in image tags by default" do
browser.visit("http://#{@host}:#{@port}/")
@received_requests.find {|r| r =~ %r{/path/to/image} }.should_not be_nil
end

it "should load images in css by default" do
browser.visit("http://#{@host}:#{@port}/")
@received_requests.find {|r| r =~ %r{/path/to/image} }.should_not be_nil
end

it "should not load images in image tags when skip_image_loading is true" do
browser_skip_images.visit("http://#{@host}:#{@port}/")
@received_requests.find {|r| r =~ %r{/path/to/image} }.should be_nil
end

it "should not load images in css when skip_image_loading is true" do
browser_skip_images.visit("http://#{@host}:#{@port}/")
@received_requests.find {|r| r =~ %r{/path/to/bgimage} }.should be_nil
end
end

describe "forking", :skip_on_windows => true do
it "only shuts down the server from the main process" do
browser.reset!
Expand Down
1 change: 1 addition & 0 deletions src/CommandFactory.cpp
Expand Up @@ -24,6 +24,7 @@
#include "CurrentUrl.h"
#include "ResizeWindow.h"
#include "IgnoreSslErrors.h"
#include "SetSkipImageLoading.h"

CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
m_page = page;
Expand Down
11 changes: 11 additions & 0 deletions src/SetSkipImageLoading.cpp
@@ -0,0 +1,11 @@
#include "SetSkipImageLoading.h"
#include "WebPage.h"

SetSkipImageLoading::SetSkipImageLoading(WebPage *page, QStringList &arguments, QObject *parent) :
Command(page, arguments, parent) {
}

void SetSkipImageLoading::start() {
page()->setSkipImageLoading(arguments().contains("true"));
emit finished(new Response(true));
}
11 changes: 11 additions & 0 deletions src/SetSkipImageLoading.h
@@ -0,0 +1,11 @@
#include "Command.h"

class WebPage;

class SetSkipImageLoading : public Command {
Q_OBJECT

public:
SetSkipImageLoading(WebPage *page, QStringList &arguments, QObject *parent = 0);
virtual void start();
};
5 changes: 5 additions & 0 deletions src/WebPage.cpp
Expand Up @@ -5,6 +5,7 @@
#include "UnsupportedContentHandler.h"
#include <QResource>
#include <iostream>
#include <QWebSettings>

WebPage::WebPage(QObject *parent) : QWebPage(parent) {
setForwardUnsupportedContent(true);
Expand Down Expand Up @@ -216,6 +217,10 @@ void WebPage::ignoreSslErrors() {
m_ignoreSslErrors = true;
}

void WebPage::setSkipImageLoading(bool skip) {
settings()->setAttribute(QWebSettings::AutoLoadImages, !skip);
}

int WebPage::getLastStatus() {
return m_lastStatus;
}
Expand Down
1 change: 1 addition & 0 deletions src/WebPage.h
Expand Up @@ -16,6 +16,7 @@ class WebPage : public QWebPage {
bool render(const QString &fileName);
virtual bool extension (Extension extension, const ExtensionOption *option=0, ExtensionReturn *output=0);
void ignoreSslErrors();
void setSkipImageLoading(bool skip);
QString consoleMessages();
void resetConsoleMessages();
void resetWindowSize();
Expand Down
1 change: 1 addition & 0 deletions src/find_command.h
Expand Up @@ -27,3 +27,4 @@ CHECK_COMMAND(RequestedUrl)
CHECK_COMMAND(CurrentUrl)
CHECK_COMMAND(ResizeWindow)
CHECK_COMMAND(IgnoreSslErrors)
CHECK_COMMAND(SetSkipImageLoading)
2 changes: 0 additions & 2 deletions src/main.cpp
Expand Up @@ -18,8 +18,6 @@ int main(int argc, char **argv) {
app.setOrganizationName("thoughtbot, inc");
app.setOrganizationDomain("thoughtbot.com");

QStringList args = app.arguments();

Server server(0);

if (server.start()) {
Expand Down
2 changes: 2 additions & 0 deletions src/webkit_server.pro
Expand Up @@ -38,6 +38,7 @@ HEADERS = \
SetProxy.h \
NullCommand.h \
PageLoadingCommand.h \
SetSkipImageLoading.h \

SOURCES = \
IgnoreSslErrors.cpp \
Expand Down Expand Up @@ -77,6 +78,7 @@ SOURCES = \
SetProxy.cpp \
NullCommand.cpp \
PageLoadingCommand.cpp \
SetSkipImageLoading.cpp \

RESOURCES = webkit_server.qrc
QT += network webkit
Expand Down

0 comments on commit 646eabc

Please sign in to comment.