Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Set custom HTML #170

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/capybara/driver/webkit/browser.rb
Expand Up @@ -86,6 +86,14 @@ def render(path, width, height)
command "Render", path, width, height
end

def set_html(html, url=nil)
if url
command("SetHtml", html, url)
else
command("SetHtml", html)
end
end

private

def start_server
Expand Down
71 changes: 71 additions & 0 deletions spec/driver_spec.rb
Expand Up @@ -884,6 +884,77 @@ def make_the_server_go_away
end
end

context "custom HTML" do
before(:all) do
@requested = []
@app = lambda do |env|
params = ::Rack::Utils.parse_query(env['QUERY_STRING'])

type = params["get"]
@requested << type
if type == "javascript"
js = <<-JS
onload_handler = function() {
document.getElementById("url").innerHTML = document.location;
}
JS
[200,
{ 'Content-Type' => 'text/javascript; charset=UTF-8',
'Content-Length' => js.size.to_s,
}, [js]]
else
[200,
{ 'Content-Type' => 'text/%s; charset=UTF-8' % params["get"],
'Content-Length' => "0",
}, [""]]
end
end
end

before do
@requested.clear
@root_url = subject.send(:url, "/")
end

def set_html(url = @root_url)
subject.browser.set_html <<-HTML, url
<html>
<head>
<title>Test</title>
<script type="text/javascript" src="?get=javascript"></script>
<link rel="stylesheet" type="text/css" href="?get=css" />
</head>
<body onload="onload_handler();">
<p id="welcome">Hello</p>
<p id="url">n/a</p>
</body>
</html>
HTML
subject.find("id('welcome')").first.text.should == "Hello"
end

it "allows setting custom HTML without URL" do
set_html nil
end

it "allows setting custom HTML with URL" do
set_html
subject.browser.url.should == @root_url
end

it "loads resources from right location" do
set_html
@requested.size.should == 2
@requested.should include "javascript"
@requested.should include "css"
end

it "makes fake URL accessible through document.location" do
set_html
subject.find("id('url')").first.text.should == @root_url
end
end

context "with socket debugger" do
let(:socket_debugger_class){ Capybara::Driver::Webkit::SocketDebugger }
let(:browser_with_debugger){
Expand Down
1 change: 1 addition & 0 deletions src/Connection.cpp
Expand Up @@ -16,6 +16,7 @@
#include "Body.h"
#include "Status.h"
#include "Headers.h"
#include "SetHtml.h"

#include <QTcpSocket>
#include <iostream>
Expand Down
25 changes: 25 additions & 0 deletions src/SetHtml.cpp
@@ -0,0 +1,25 @@
#include "SetHtml.h"
#include "WebPage.h"
#include <QUrl>

SetHtml::SetHtml(WebPage *page, QObject *parent)
: Command(page, parent)
{ }

void SetHtml::start(QStringList &arguments) {
if (arguments.size() > 1)
page()->currentFrame()->setHtml(arguments[0], QUrl(arguments[1]));
else
page()->currentFrame()->setHtml(arguments[0]);
emit finished(new Response(true));
}

void SetHtml::loadFinished(bool success) {
QString message;
if (!success)
message = page()->failureString();

disconnect(page(), SIGNAL(pageFinished(bool)), this,
SLOT(loadFinished(bool)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same fix as for the Visit command. Also needs a test?

emit finished(new Response(success, message));
}
14 changes: 14 additions & 0 deletions src/SetHtml.h
@@ -0,0 +1,14 @@
#include "Command.h"

class WebPage;

class SetHtml : public Command {
Q_OBJECT;

public:
SetHtml(WebPage *page, QObject *parent = 0);
virtual void start(QStringList &arguments);

private slots:
void loadFinished(bool success);
};
3 changes: 2 additions & 1 deletion src/find_command.h
Expand Up @@ -16,4 +16,5 @@ CHECK_COMMAND(Header)
CHECK_COMMAND(Render)
CHECK_COMMAND(Body)
CHECK_COMMAND(Status)
CHECK_COMMAND(Headers)
CHECK_COMMAND(Headers)
CHECK_COMMAND(SetHtml)
53 changes: 51 additions & 2 deletions src/webkit_server.pro
@@ -1,8 +1,57 @@
TEMPLATE = app
TARGET = webkit_server
DESTDIR = .
HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Node.h JavascriptInvocation.h Url.h Source.h Evaluate.h Execute.h FrameFocus.h Response.h NetworkAccessManager.h Header.h Render.h body.h Status.h Headers.h UnsupportedContentHandler.h
SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Node.cpp JavascriptInvocation.cpp Url.cpp Source.cpp Evaluate.cpp Execute.cpp FrameFocus.cpp Response.cpp NetworkAccessManager.cpp Header.cpp Render.cpp body.cpp Status.cpp Headers.cpp UnsupportedContentHandler.cpp
HEADERS = \
WebPage.h \
Server.h \
Connection.h \
Command.h \
Visit.h \
Find.h \
Reset.h \
Node.h \
JavascriptInvocation.h \
Url.h \
Source.h \
Evaluate.h \
Execute.h \
FrameFocus.h \
Response.h \
NetworkAccessManager.h \
Header.h \
Render.h \
body.h \
Status.h \
Headers.h \
UnsupportedContentHandler.h \
SetHtml.h \

SOURCES = \
main.cpp \
WebPage.cpp \
Server.cpp \
Connection.cpp \
Command.cpp \
Visit.cpp \
Find.cpp \
Reset.cpp \
Node.cpp \
JavascriptInvocation.cpp \
Url.cpp \
Source.cpp \
Evaluate.cpp \
Execute.cpp \
FrameFocus.cpp \
Response.cpp \
NetworkAccessManager.cpp \
Header.cpp \
Render.cpp \
body.cpp \
Status.cpp \
Headers.cpp \
UnsupportedContentHandler.cpp \
SetHtml.cpp \

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