Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add option to disable image loading in WebKit.

  • Loading branch information...
commit 646eabc68d5c6fe5bc0515492ee9ba04c5f49b8f 1 parent 5c19691
@seangeo seangeo authored halogenandtoast committed
View
1  lib/capybara/driver/webkit.rb
@@ -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
View
6 lib/capybara/driver/webkit/browser.rb
@@ -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
@@ -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
View
75 spec/browser_spec.rb
@@ -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
@@ -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!
View
1  src/CommandFactory.cpp
@@ -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;
View
11 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));
+}
View
11 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();
+};
View
5 src/WebPage.cpp
@@ -5,6 +5,7 @@
#include "UnsupportedContentHandler.h"
#include <QResource>
#include <iostream>
+#include <QWebSettings>
WebPage::WebPage(QObject *parent) : QWebPage(parent) {
setForwardUnsupportedContent(true);
@@ -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;
}
View
1  src/WebPage.h
@@ -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();
View
1  src/find_command.h
@@ -27,3 +27,4 @@ CHECK_COMMAND(RequestedUrl)
CHECK_COMMAND(CurrentUrl)
CHECK_COMMAND(ResizeWindow)
CHECK_COMMAND(IgnoreSslErrors)
+CHECK_COMMAND(SetSkipImageLoading)
View
2  src/main.cpp
@@ -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()) {
View
2  src/webkit_server.pro
@@ -38,6 +38,7 @@ HEADERS = \
SetProxy.h \
NullCommand.h \
PageLoadingCommand.h \
+ SetSkipImageLoading.h \
SOURCES = \
IgnoreSslErrors.cpp \
@@ -77,6 +78,7 @@ SOURCES = \
SetProxy.cpp \
NullCommand.cpp \
PageLoadingCommand.cpp \
+ SetSkipImageLoading.cpp \
RESOURCES = webkit_server.qrc
QT += network webkit
Please sign in to comment.
Something went wrong with that request. Please try again.