intentmedia-capybara-webkit 0.7.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Appraisals +7 -0
- data/CONTRIBUTING.md +38 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +65 -0
- data/LICENSE +19 -0
- data/README.md +67 -0
- data/Rakefile +78 -0
- data/bin/Info.plist +22 -0
- data/capybara-webkit.gemspec +24 -0
- data/extconf.rb +2 -0
- data/gemfiles/1.0.gemfile +7 -0
- data/gemfiles/1.0.gemfile.lock +65 -0
- data/gemfiles/1.1.gemfile +7 -0
- data/gemfiles/1.1.gemfile.lock +65 -0
- data/lib/capybara-webkit.rb +1 -0
- data/lib/capybara/driver/webkit.rb +113 -0
- data/lib/capybara/driver/webkit/browser.rb +216 -0
- data/lib/capybara/driver/webkit/node.rb +118 -0
- data/lib/capybara/driver/webkit/socket_debugger.rb +43 -0
- data/lib/capybara/webkit.rb +11 -0
- data/lib/capybara_webkit_builder.rb +40 -0
- data/spec/browser_spec.rb +178 -0
- data/spec/driver_rendering_spec.rb +80 -0
- data/spec/driver_spec.rb +1048 -0
- data/spec/integration/driver_spec.rb +20 -0
- data/spec/integration/session_spec.rb +137 -0
- data/spec/self_signed_ssl_cert.rb +42 -0
- data/spec/spec_helper.rb +25 -0
- data/src/Body.h +12 -0
- data/src/ClearCookies.cpp +18 -0
- data/src/ClearCookies.h +11 -0
- data/src/Command.cpp +15 -0
- data/src/Command.h +29 -0
- data/src/CommandFactory.cpp +29 -0
- data/src/CommandFactory.h +16 -0
- data/src/CommandParser.cpp +68 -0
- data/src/CommandParser.h +29 -0
- data/src/Connection.cpp +82 -0
- data/src/Connection.h +36 -0
- data/src/Evaluate.cpp +84 -0
- data/src/Evaluate.h +22 -0
- data/src/Execute.cpp +16 -0
- data/src/Execute.h +12 -0
- data/src/Find.cpp +19 -0
- data/src/Find.h +13 -0
- data/src/FrameFocus.cpp +66 -0
- data/src/FrameFocus.h +28 -0
- data/src/GetCookies.cpp +22 -0
- data/src/GetCookies.h +14 -0
- data/src/Header.cpp +18 -0
- data/src/Header.h +11 -0
- data/src/Headers.cpp +11 -0
- data/src/Headers.h +12 -0
- data/src/JavascriptInvocation.cpp +14 -0
- data/src/JavascriptInvocation.h +19 -0
- data/src/NetworkAccessManager.cpp +25 -0
- data/src/NetworkAccessManager.h +18 -0
- data/src/NetworkCookieJar.cpp +101 -0
- data/src/NetworkCookieJar.h +15 -0
- data/src/Node.cpp +14 -0
- data/src/Node.h +13 -0
- data/src/Render.cpp +19 -0
- data/src/Render.h +12 -0
- data/src/Reset.cpp +20 -0
- data/src/Reset.h +12 -0
- data/src/Response.cpp +19 -0
- data/src/Response.h +13 -0
- data/src/Server.cpp +25 -0
- data/src/Server.h +21 -0
- data/src/SetCookie.cpp +18 -0
- data/src/SetCookie.h +11 -0
- data/src/SetProxy.cpp +24 -0
- data/src/SetProxy.h +11 -0
- data/src/Source.cpp +20 -0
- data/src/Source.h +19 -0
- data/src/Status.cpp +13 -0
- data/src/Status.h +12 -0
- data/src/UnsupportedContentHandler.cpp +32 -0
- data/src/UnsupportedContentHandler.h +18 -0
- data/src/Url.cpp +15 -0
- data/src/Url.h +12 -0
- data/src/Visit.cpp +21 -0
- data/src/Visit.h +15 -0
- data/src/WebPage.cpp +226 -0
- data/src/WebPage.h +54 -0
- data/src/body.cpp +11 -0
- data/src/capybara.js +205 -0
- data/src/find_command.h +24 -0
- data/src/main.cpp +34 -0
- data/src/webkit_server.pro +71 -0
- data/src/webkit_server.qrc +5 -0
- data/templates/Command.cpp +10 -0
- data/templates/Command.h +12 -0
- data/webkit_server.pro +4 -0
- metadata +246 -0
data/src/Connection.cpp
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
#include "Connection.h"
|
2
|
+
#include "WebPage.h"
|
3
|
+
#include "UnsupportedContentHandler.h"
|
4
|
+
#include "CommandParser.h"
|
5
|
+
#include "CommandFactory.h"
|
6
|
+
#include "Command.h"
|
7
|
+
|
8
|
+
#include <QTcpSocket>
|
9
|
+
#include <iostream>
|
10
|
+
|
11
|
+
Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
|
12
|
+
QObject(parent) {
|
13
|
+
m_socket = socket;
|
14
|
+
m_page = page;
|
15
|
+
m_commandParser = new CommandParser(socket, this);
|
16
|
+
m_commandFactory = new CommandFactory(page, this);
|
17
|
+
m_command = NULL;
|
18
|
+
m_pageSuccess = true;
|
19
|
+
m_commandWaiting = false;
|
20
|
+
connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
|
21
|
+
connect(m_commandParser, SIGNAL(commandReady(QString, QStringList)), this, SLOT(commandReady(QString, QStringList)));
|
22
|
+
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
23
|
+
}
|
24
|
+
|
25
|
+
|
26
|
+
void Connection::commandReady(QString commandName, QStringList arguments) {
|
27
|
+
m_commandName = commandName;
|
28
|
+
m_arguments = arguments;
|
29
|
+
|
30
|
+
if (m_page->isLoading())
|
31
|
+
m_commandWaiting = true;
|
32
|
+
else
|
33
|
+
startCommand();
|
34
|
+
}
|
35
|
+
|
36
|
+
void Connection::startCommand() {
|
37
|
+
m_commandWaiting = false;
|
38
|
+
if (m_pageSuccess) {
|
39
|
+
m_command = m_commandFactory->createCommand(m_commandName.toAscii().constData());
|
40
|
+
if (m_command) {
|
41
|
+
connect(m_command,
|
42
|
+
SIGNAL(finished(Response *)),
|
43
|
+
this,
|
44
|
+
SLOT(finishCommand(Response *)));
|
45
|
+
m_command->start(m_arguments);
|
46
|
+
} else {
|
47
|
+
QString failure = QString("[Capybara WebKit] Unknown command: ") + m_commandName + "\n";
|
48
|
+
writeResponse(new Response(false, failure));
|
49
|
+
}
|
50
|
+
m_commandName = QString();
|
51
|
+
} else {
|
52
|
+
m_pageSuccess = true;
|
53
|
+
QString message = m_page->failureString();
|
54
|
+
writeResponse(new Response(false, message));
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
void Connection::pendingLoadFinished(bool success) {
|
59
|
+
m_pageSuccess = success;
|
60
|
+
if (m_commandWaiting)
|
61
|
+
startCommand();
|
62
|
+
}
|
63
|
+
|
64
|
+
void Connection::finishCommand(Response *response) {
|
65
|
+
m_command->deleteLater();
|
66
|
+
m_command = NULL;
|
67
|
+
writeResponse(response);
|
68
|
+
}
|
69
|
+
|
70
|
+
void Connection::writeResponse(Response *response) {
|
71
|
+
if (response->isSuccess())
|
72
|
+
m_socket->write("ok\n");
|
73
|
+
else
|
74
|
+
m_socket->write("failure\n");
|
75
|
+
|
76
|
+
QByteArray messageUtf8 = response->message().toUtf8();
|
77
|
+
QString messageLength = QString::number(messageUtf8.size()) + "\n";
|
78
|
+
m_socket->write(messageLength.toAscii());
|
79
|
+
m_socket->write(messageUtf8);
|
80
|
+
delete response;
|
81
|
+
}
|
82
|
+
|
data/src/Connection.h
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#include <QObject>
|
2
|
+
#include <QStringList>
|
3
|
+
|
4
|
+
class QTcpSocket;
|
5
|
+
class WebPage;
|
6
|
+
class Command;
|
7
|
+
class Response;
|
8
|
+
class CommandParser;
|
9
|
+
class CommandFactory;
|
10
|
+
|
11
|
+
class Connection : public QObject {
|
12
|
+
Q_OBJECT
|
13
|
+
|
14
|
+
public:
|
15
|
+
Connection(QTcpSocket *socket, WebPage *page, QObject *parent = 0);
|
16
|
+
|
17
|
+
public slots:
|
18
|
+
void commandReady(QString commandName, QStringList arguments);
|
19
|
+
void finishCommand(Response *response);
|
20
|
+
void pendingLoadFinished(bool success);
|
21
|
+
|
22
|
+
private:
|
23
|
+
void startCommand();
|
24
|
+
void writeResponse(Response *response);
|
25
|
+
|
26
|
+
QTcpSocket *m_socket;
|
27
|
+
QString m_commandName;
|
28
|
+
Command *m_command;
|
29
|
+
QStringList m_arguments;
|
30
|
+
WebPage *m_page;
|
31
|
+
CommandParser *m_commandParser;
|
32
|
+
CommandFactory *m_commandFactory;
|
33
|
+
bool m_pageSuccess;
|
34
|
+
bool m_commandWaiting;
|
35
|
+
};
|
36
|
+
|
data/src/Evaluate.cpp
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
#include "Evaluate.h"
|
2
|
+
#include "WebPage.h"
|
3
|
+
#include <iostream>
|
4
|
+
|
5
|
+
Evaluate::Evaluate(WebPage *page, QObject *parent) : Command(page, parent) {
|
6
|
+
m_buffer = "";
|
7
|
+
}
|
8
|
+
|
9
|
+
void Evaluate::start(QStringList &arguments) {
|
10
|
+
QVariant result = page()->currentFrame()->evaluateJavaScript(arguments[0]);
|
11
|
+
addVariant(result);
|
12
|
+
emit finished(new Response(true, m_buffer));
|
13
|
+
}
|
14
|
+
|
15
|
+
void Evaluate::addVariant(QVariant &object) {
|
16
|
+
if (object.isValid()) {
|
17
|
+
switch(object.type()) {
|
18
|
+
case QMetaType::QString:
|
19
|
+
{
|
20
|
+
QString string = object.toString();
|
21
|
+
addString(string);
|
22
|
+
}
|
23
|
+
break;
|
24
|
+
case QMetaType::QVariantList:
|
25
|
+
{
|
26
|
+
QVariantList list = object.toList();
|
27
|
+
addArray(list);
|
28
|
+
}
|
29
|
+
break;
|
30
|
+
case QMetaType::Double:
|
31
|
+
m_buffer.append(object.toString());
|
32
|
+
break;
|
33
|
+
case QMetaType::QVariantMap:
|
34
|
+
{
|
35
|
+
QVariantMap map = object.toMap();
|
36
|
+
addMap(map);
|
37
|
+
break;
|
38
|
+
}
|
39
|
+
case QMetaType::Bool:
|
40
|
+
{
|
41
|
+
m_buffer.append(object.toString());
|
42
|
+
break;
|
43
|
+
}
|
44
|
+
default:
|
45
|
+
m_buffer.append("null");
|
46
|
+
}
|
47
|
+
} else {
|
48
|
+
m_buffer.append("null");
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
void Evaluate::addString(QString &string) {
|
53
|
+
QString escapedString(string);
|
54
|
+
escapedString.replace("\"", "\\\"");
|
55
|
+
m_buffer.append("\"");
|
56
|
+
m_buffer.append(escapedString);
|
57
|
+
m_buffer.append("\"");
|
58
|
+
}
|
59
|
+
|
60
|
+
void Evaluate::addArray(QVariantList &list) {
|
61
|
+
m_buffer.append("[");
|
62
|
+
for (int i = 0; i < list.length(); i++) {
|
63
|
+
if (i > 0)
|
64
|
+
m_buffer.append(",");
|
65
|
+
addVariant(list[i]);
|
66
|
+
}
|
67
|
+
m_buffer.append("]");
|
68
|
+
}
|
69
|
+
|
70
|
+
void Evaluate::addMap(QVariantMap &map) {
|
71
|
+
m_buffer.append("{");
|
72
|
+
QMapIterator<QString, QVariant> iterator(map);
|
73
|
+
while (iterator.hasNext()) {
|
74
|
+
iterator.next();
|
75
|
+
QString key = iterator.key();
|
76
|
+
QVariant value = iterator.value();
|
77
|
+
addString(key);
|
78
|
+
m_buffer.append(":");
|
79
|
+
addVariant(value);
|
80
|
+
if (iterator.hasNext())
|
81
|
+
m_buffer.append(",");
|
82
|
+
}
|
83
|
+
m_buffer.append("}");
|
84
|
+
}
|
data/src/Evaluate.h
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#include "Command.h"
|
2
|
+
|
3
|
+
#include <QVariantList>
|
4
|
+
|
5
|
+
class WebPage;
|
6
|
+
|
7
|
+
class Evaluate : public Command {
|
8
|
+
Q_OBJECT
|
9
|
+
|
10
|
+
public:
|
11
|
+
Evaluate(WebPage *page, QObject *parent = 0);
|
12
|
+
virtual void start(QStringList &arguments);
|
13
|
+
|
14
|
+
private:
|
15
|
+
void addVariant(QVariant &object);
|
16
|
+
void addString(QString &string);
|
17
|
+
void addArray(QVariantList &list);
|
18
|
+
void addMap(QVariantMap &map);
|
19
|
+
|
20
|
+
QString m_buffer;
|
21
|
+
};
|
22
|
+
|
data/src/Execute.cpp
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#include "Execute.h"
|
2
|
+
#include "WebPage.h"
|
3
|
+
|
4
|
+
Execute::Execute(WebPage *page, QObject *parent) : Command(page, parent) {
|
5
|
+
}
|
6
|
+
|
7
|
+
void Execute::start(QStringList &arguments) {
|
8
|
+
QString script = arguments[0] + QString("; 'success'");
|
9
|
+
QVariant result = page()->currentFrame()->evaluateJavaScript(script);
|
10
|
+
if (result.isValid()) {
|
11
|
+
emit finished(new Response(true));
|
12
|
+
} else {
|
13
|
+
emit finished(new Response(false, "Javascript failed to execute"));
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
data/src/Execute.h
ADDED
data/src/Find.cpp
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#include "Find.h"
|
2
|
+
#include "Command.h"
|
3
|
+
#include "WebPage.h"
|
4
|
+
|
5
|
+
Find::Find(WebPage *page, QObject *parent) : Command(page, parent) {
|
6
|
+
}
|
7
|
+
|
8
|
+
void Find::start(QStringList &arguments) {
|
9
|
+
QString message;
|
10
|
+
QVariant result = page()->invokeCapybaraFunction("find", arguments);
|
11
|
+
|
12
|
+
if (result.isValid()) {
|
13
|
+
message = result.toString();
|
14
|
+
emit finished(new Response(true, message));
|
15
|
+
} else {
|
16
|
+
emit finished(new Response(false, "Invalid XPath expression"));
|
17
|
+
}
|
18
|
+
}
|
19
|
+
|
data/src/Find.h
ADDED
data/src/FrameFocus.cpp
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
#include "FrameFocus.h"
|
2
|
+
#include "Command.h"
|
3
|
+
#include "WebPage.h"
|
4
|
+
|
5
|
+
FrameFocus::FrameFocus(WebPage *page, QObject *parent) : Command(page, parent) {
|
6
|
+
}
|
7
|
+
|
8
|
+
void FrameFocus::start(QStringList &arguments) {
|
9
|
+
findFrames();
|
10
|
+
switch(arguments.length()) {
|
11
|
+
case 1:
|
12
|
+
focusId(arguments[0]);
|
13
|
+
break;
|
14
|
+
case 2:
|
15
|
+
focusIndex(arguments[1].toInt());
|
16
|
+
break;
|
17
|
+
default:
|
18
|
+
focusParent();
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
void FrameFocus::findFrames() {
|
23
|
+
frames = page()->currentFrame()->childFrames();
|
24
|
+
}
|
25
|
+
|
26
|
+
void FrameFocus::focusIndex(int index) {
|
27
|
+
if (isFrameAtIndex(index)) {
|
28
|
+
frames[index]->setFocus();
|
29
|
+
success();
|
30
|
+
} else {
|
31
|
+
frameNotFound();
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
bool FrameFocus::isFrameAtIndex(int index) {
|
36
|
+
return 0 <= index && index < frames.length();
|
37
|
+
}
|
38
|
+
|
39
|
+
void FrameFocus::focusId(QString name) {
|
40
|
+
for (int i = 0; i < frames.length(); i++) {
|
41
|
+
if (frames[i]->frameName().compare(name) == 0) {
|
42
|
+
frames[i]->setFocus();
|
43
|
+
success();
|
44
|
+
return;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
frameNotFound();
|
49
|
+
}
|
50
|
+
|
51
|
+
void FrameFocus::focusParent() {
|
52
|
+
if (page()->currentFrame()->parentFrame() == 0) {
|
53
|
+
emit finished(new Response(false, "Already at parent frame."));
|
54
|
+
} else {
|
55
|
+
page()->currentFrame()->parentFrame()->setFocus();
|
56
|
+
success();
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
void FrameFocus::frameNotFound() {
|
61
|
+
emit finished(new Response(false, "Unable to locate frame. "));
|
62
|
+
}
|
63
|
+
|
64
|
+
void FrameFocus::success() {
|
65
|
+
emit finished(new Response(true));
|
66
|
+
}
|
data/src/FrameFocus.h
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#include "Command.h"
|
2
|
+
|
3
|
+
class WebPage;
|
4
|
+
class QWebFrame;
|
5
|
+
|
6
|
+
class FrameFocus : public Command {
|
7
|
+
Q_OBJECT
|
8
|
+
|
9
|
+
public:
|
10
|
+
FrameFocus(WebPage *page, QObject *parent = 0);
|
11
|
+
virtual void start(QStringList &arguments);
|
12
|
+
|
13
|
+
private:
|
14
|
+
void findFrames();
|
15
|
+
|
16
|
+
void focusParent();
|
17
|
+
|
18
|
+
void focusIndex(int index);
|
19
|
+
bool isFrameAtIndex(int index);
|
20
|
+
|
21
|
+
void focusId(QString id);
|
22
|
+
|
23
|
+
void success();
|
24
|
+
void frameNotFound();
|
25
|
+
|
26
|
+
QList<QWebFrame *> frames;
|
27
|
+
};
|
28
|
+
|
data/src/GetCookies.cpp
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#include "GetCookies.h"
|
2
|
+
#include "WebPage.h"
|
3
|
+
#include "NetworkCookieJar.h"
|
4
|
+
|
5
|
+
GetCookies::GetCookies(WebPage *page, QObject *parent)
|
6
|
+
: Command(page, parent)
|
7
|
+
{
|
8
|
+
m_buffer = "";
|
9
|
+
}
|
10
|
+
|
11
|
+
void GetCookies::start(QStringList &arguments)
|
12
|
+
{
|
13
|
+
Q_UNUSED(arguments);
|
14
|
+
NetworkCookieJar *jar = qobject_cast<NetworkCookieJar*>(page()
|
15
|
+
->networkAccessManager()
|
16
|
+
->cookieJar());
|
17
|
+
foreach (QNetworkCookie cookie, jar->getAllCookies()) {
|
18
|
+
m_buffer.append(cookie.toRawForm());
|
19
|
+
m_buffer.append("\n");
|
20
|
+
}
|
21
|
+
emit finished(new Response(true, m_buffer));
|
22
|
+
}
|
data/src/GetCookies.h
ADDED
data/src/Header.cpp
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#include "Header.h"
|
2
|
+
#include "WebPage.h"
|
3
|
+
#include "NetworkAccessManager.h"
|
4
|
+
|
5
|
+
Header::Header(WebPage *page, QObject *parent) : Command(page, parent) {
|
6
|
+
}
|
7
|
+
|
8
|
+
void Header::start(QStringList &arguments) {
|
9
|
+
QString key = arguments[0];
|
10
|
+
QString value = arguments[1];
|
11
|
+
NetworkAccessManager* networkAccessManager = qobject_cast<NetworkAccessManager*>(page()->networkAccessManager());
|
12
|
+
if (key.toLower().replace("-", "_") == "user_agent") {
|
13
|
+
page()->setUserAgent(value);
|
14
|
+
} else {
|
15
|
+
networkAccessManager->addHeader(key, value);
|
16
|
+
}
|
17
|
+
emit finished(new Response(true));
|
18
|
+
}
|