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

Add option to disable image loading in WebKit.

This commit is contained in:
Sean Geoghegan 2012-03-30 12:08:34 +10:30 committed by Matthew Mongeau
parent 5c19691720
commit 646eabc68d
11 changed files with 112 additions and 4 deletions

View file

@ -24,7 +24,6 @@ class Capybara::Driver::Webkit
@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 file

@ -77,6 +77,10 @@ class Capybara::Driver::Webkit
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 @@ class Capybara::Driver::Webkit
if result.nil?
raise WebkitNoResponseError, "No response received from the server."
elsif result != 'ok'
elsif result != 'ok'
raise WebkitInvalidResponseError, read_response
end

View file

@ -14,6 +14,11 @@ describe Capybara::Driver::Webkit::Browser do
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 @@ describe Capybara::Driver::Webkit::Browser do
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 file

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

@ -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
src/SetSkipImageLoading.h Normal file
View file

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

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

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

@ -27,3 +27,4 @@ CHECK_COMMAND(RequestedUrl)
CHECK_COMMAND(CurrentUrl)
CHECK_COMMAND(ResizeWindow)
CHECK_COMMAND(IgnoreSslErrors)
CHECK_COMMAND(SetSkipImageLoading)

View file

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

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