intentmedia-capybara-webkit 0.7.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +2 -0
  3. data/Appraisals +7 -0
  4. data/CONTRIBUTING.md +38 -0
  5. data/Gemfile +8 -0
  6. data/Gemfile.lock +65 -0
  7. data/LICENSE +19 -0
  8. data/README.md +67 -0
  9. data/Rakefile +78 -0
  10. data/bin/Info.plist +22 -0
  11. data/capybara-webkit.gemspec +24 -0
  12. data/extconf.rb +2 -0
  13. data/gemfiles/1.0.gemfile +7 -0
  14. data/gemfiles/1.0.gemfile.lock +65 -0
  15. data/gemfiles/1.1.gemfile +7 -0
  16. data/gemfiles/1.1.gemfile.lock +65 -0
  17. data/lib/capybara-webkit.rb +1 -0
  18. data/lib/capybara/driver/webkit.rb +113 -0
  19. data/lib/capybara/driver/webkit/browser.rb +216 -0
  20. data/lib/capybara/driver/webkit/node.rb +118 -0
  21. data/lib/capybara/driver/webkit/socket_debugger.rb +43 -0
  22. data/lib/capybara/webkit.rb +11 -0
  23. data/lib/capybara_webkit_builder.rb +40 -0
  24. data/spec/browser_spec.rb +178 -0
  25. data/spec/driver_rendering_spec.rb +80 -0
  26. data/spec/driver_spec.rb +1048 -0
  27. data/spec/integration/driver_spec.rb +20 -0
  28. data/spec/integration/session_spec.rb +137 -0
  29. data/spec/self_signed_ssl_cert.rb +42 -0
  30. data/spec/spec_helper.rb +25 -0
  31. data/src/Body.h +12 -0
  32. data/src/ClearCookies.cpp +18 -0
  33. data/src/ClearCookies.h +11 -0
  34. data/src/Command.cpp +15 -0
  35. data/src/Command.h +29 -0
  36. data/src/CommandFactory.cpp +29 -0
  37. data/src/CommandFactory.h +16 -0
  38. data/src/CommandParser.cpp +68 -0
  39. data/src/CommandParser.h +29 -0
  40. data/src/Connection.cpp +82 -0
  41. data/src/Connection.h +36 -0
  42. data/src/Evaluate.cpp +84 -0
  43. data/src/Evaluate.h +22 -0
  44. data/src/Execute.cpp +16 -0
  45. data/src/Execute.h +12 -0
  46. data/src/Find.cpp +19 -0
  47. data/src/Find.h +13 -0
  48. data/src/FrameFocus.cpp +66 -0
  49. data/src/FrameFocus.h +28 -0
  50. data/src/GetCookies.cpp +22 -0
  51. data/src/GetCookies.h +14 -0
  52. data/src/Header.cpp +18 -0
  53. data/src/Header.h +11 -0
  54. data/src/Headers.cpp +11 -0
  55. data/src/Headers.h +12 -0
  56. data/src/JavascriptInvocation.cpp +14 -0
  57. data/src/JavascriptInvocation.h +19 -0
  58. data/src/NetworkAccessManager.cpp +25 -0
  59. data/src/NetworkAccessManager.h +18 -0
  60. data/src/NetworkCookieJar.cpp +101 -0
  61. data/src/NetworkCookieJar.h +15 -0
  62. data/src/Node.cpp +14 -0
  63. data/src/Node.h +13 -0
  64. data/src/Render.cpp +19 -0
  65. data/src/Render.h +12 -0
  66. data/src/Reset.cpp +20 -0
  67. data/src/Reset.h +12 -0
  68. data/src/Response.cpp +19 -0
  69. data/src/Response.h +13 -0
  70. data/src/Server.cpp +25 -0
  71. data/src/Server.h +21 -0
  72. data/src/SetCookie.cpp +18 -0
  73. data/src/SetCookie.h +11 -0
  74. data/src/SetProxy.cpp +24 -0
  75. data/src/SetProxy.h +11 -0
  76. data/src/Source.cpp +20 -0
  77. data/src/Source.h +19 -0
  78. data/src/Status.cpp +13 -0
  79. data/src/Status.h +12 -0
  80. data/src/UnsupportedContentHandler.cpp +32 -0
  81. data/src/UnsupportedContentHandler.h +18 -0
  82. data/src/Url.cpp +15 -0
  83. data/src/Url.h +12 -0
  84. data/src/Visit.cpp +21 -0
  85. data/src/Visit.h +15 -0
  86. data/src/WebPage.cpp +226 -0
  87. data/src/WebPage.h +54 -0
  88. data/src/body.cpp +11 -0
  89. data/src/capybara.js +205 -0
  90. data/src/find_command.h +24 -0
  91. data/src/main.cpp +34 -0
  92. data/src/webkit_server.pro +71 -0
  93. data/src/webkit_server.qrc +5 -0
  94. data/templates/Command.cpp +10 -0
  95. data/templates/Command.h +12 -0
  96. data/webkit_server.pro +4 -0
  97. metadata +246 -0
data/src/Header.h ADDED
@@ -0,0 +1,11 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class Header : public Command {
6
+ Q_OBJECT
7
+
8
+ public:
9
+ Header(WebPage *page, QObject *parent = 0);
10
+ virtual void start(QStringList &arguments);
11
+ };
data/src/Headers.cpp ADDED
@@ -0,0 +1,11 @@
1
+ #include "Headers.h"
2
+ #include "WebPage.h"
3
+
4
+ Headers::Headers(WebPage *page, QObject *parent) : Command(page, parent) {
5
+ }
6
+
7
+ void Headers::start(QStringList &arguments) {
8
+ Q_UNUSED(arguments);
9
+ emit finished(new Response(true, page()->pageHeaders()));
10
+ }
11
+
data/src/Headers.h ADDED
@@ -0,0 +1,12 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class Headers : public Command {
6
+ Q_OBJECT
7
+
8
+ public:
9
+ Headers(WebPage *page, QObject *parent = 0);
10
+ virtual void start(QStringList &arguments);
11
+ };
12
+
@@ -0,0 +1,14 @@
1
+ #include "JavascriptInvocation.h"
2
+
3
+ JavascriptInvocation::JavascriptInvocation(QString &functionName, QStringList &arguments, QObject *parent) : QObject(parent) {
4
+ m_functionName = functionName;
5
+ m_arguments = arguments;
6
+ }
7
+
8
+ QString &JavascriptInvocation::functionName() {
9
+ return m_functionName;
10
+ }
11
+
12
+ QStringList &JavascriptInvocation::arguments() {
13
+ return m_arguments;
14
+ }
@@ -0,0 +1,19 @@
1
+ #include <QObject>
2
+ #include <QString>
3
+ #include <QStringList>
4
+
5
+ class JavascriptInvocation : public QObject {
6
+ Q_OBJECT
7
+ Q_PROPERTY(QString functionName READ functionName)
8
+ Q_PROPERTY(QStringList arguments READ arguments)
9
+
10
+ public:
11
+ JavascriptInvocation(QString &functionName, QStringList &arguments, QObject *parent = 0);
12
+ QString &functionName();
13
+ QStringList &arguments();
14
+
15
+ private:
16
+ QString m_functionName;
17
+ QStringList m_arguments;
18
+ };
19
+
@@ -0,0 +1,25 @@
1
+ #include "NetworkAccessManager.h"
2
+ #include "WebPage.h"
3
+ #include <iostream>
4
+
5
+
6
+ NetworkAccessManager::NetworkAccessManager(QObject *parent):QNetworkAccessManager(parent) {
7
+ }
8
+
9
+ QNetworkReply* NetworkAccessManager::createRequest(QNetworkAccessManager::Operation operation, const QNetworkRequest &request, QIODevice * outgoingData = 0) {
10
+ QNetworkRequest new_request(request);
11
+ if (operation != QNetworkAccessManager::PostOperation && operation != QNetworkAccessManager::PutOperation) {
12
+ new_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant());
13
+ }
14
+ QHashIterator<QString, QString> item(m_headers);
15
+ while (item.hasNext()) {
16
+ item.next();
17
+ new_request.setRawHeader(item.key().toAscii(), item.value().toAscii());
18
+ }
19
+ return QNetworkAccessManager::createRequest(operation, new_request, outgoingData);
20
+ };
21
+
22
+ void NetworkAccessManager::addHeader(QString key, QString value) {
23
+ m_headers.insert(key, value);
24
+ };
25
+
@@ -0,0 +1,18 @@
1
+ #include <QtNetwork/QNetworkAccessManager>
2
+ #include <QtNetwork/QNetworkRequest>
3
+ #include <QtNetwork/QNetworkReply>
4
+
5
+ class NetworkAccessManager : public QNetworkAccessManager {
6
+
7
+ Q_OBJECT
8
+
9
+ public:
10
+ NetworkAccessManager(QObject *parent = 0);
11
+ void addHeader(QString key, QString value);
12
+
13
+ protected:
14
+ QNetworkReply* createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice * outgoingData);
15
+
16
+ private:
17
+ QHash<QString, QString> m_headers;
18
+ };
@@ -0,0 +1,101 @@
1
+ #include "NetworkCookieJar.h"
2
+ #include "QtCore/qdatetime.h"
3
+
4
+ NetworkCookieJar::NetworkCookieJar(QObject *parent)
5
+ : QNetworkCookieJar(parent)
6
+ { }
7
+
8
+ QList<QNetworkCookie> NetworkCookieJar::getAllCookies() const
9
+ {
10
+ return allCookies();
11
+ }
12
+
13
+ void NetworkCookieJar::clearCookies()
14
+ {
15
+ setAllCookies(QList<QNetworkCookie>());
16
+ }
17
+
18
+ static inline bool isParentDomain(QString domain, QString reference)
19
+ {
20
+ if (!reference.startsWith(QLatin1Char('.')))
21
+ return domain == reference;
22
+
23
+ return domain.endsWith(reference) || domain == reference.mid(1);
24
+ }
25
+
26
+ void NetworkCookieJar::overwriteCookies(const QList<QNetworkCookie>& cookieList)
27
+ {
28
+ /* this function is basically a copy-and-paste of the original
29
+ QNetworkCookieJar::setCookiesFromUrl with the domain and
30
+ path validations removed */
31
+
32
+ QString defaultPath(QLatin1Char('/'));
33
+ QDateTime now = QDateTime::currentDateTime();
34
+ QList<QNetworkCookie> newCookies = allCookies();
35
+
36
+ foreach (QNetworkCookie cookie, cookieList) {
37
+ bool isDeletion = (!cookie.isSessionCookie() &&
38
+ cookie.expirationDate() < now);
39
+
40
+ // validate the cookie & set the defaults if unset
41
+ if (cookie.path().isEmpty())
42
+ cookie.setPath(defaultPath);
43
+
44
+ // don't do path checking. See http://bugreports.qt.nokia.com/browse/QTBUG-5815
45
+ // else if (!isParentPath(pathAndFileName, cookie.path())) {
46
+ // continue; // not accepted
47
+ // }
48
+
49
+ if (cookie.domain().isEmpty()) {
50
+ continue;
51
+ } else {
52
+ // Ensure the domain starts with a dot if its field was not empty
53
+ // in the HTTP header. There are some servers that forget the
54
+ // leading dot and this is actually forbidden according to RFC 2109,
55
+ // but all browsers accept it anyway so we do that as well.
56
+ if (!cookie.domain().startsWith(QLatin1Char('.')))
57
+ cookie.setDomain(QLatin1Char('.') + cookie.domain());
58
+
59
+ QString domain = cookie.domain();
60
+
61
+ // the check for effective TLDs makes the "embedded dot" rule from RFC 2109 section 4.3.2
62
+ // redundant; the "leading dot" rule has been relaxed anyway, see above
63
+ // we remove the leading dot for this check
64
+ /*
65
+ if (QNetworkCookieJarPrivate::isEffectiveTLD(domain.remove(0, 1)))
66
+ continue; // not accepted
67
+ */
68
+ }
69
+
70
+ for (int i = 0; i < newCookies.size(); ++i) {
71
+ // does this cookie already exist?
72
+ const QNetworkCookie &current = newCookies.at(i);
73
+ if (cookie.name() == current.name() &&
74
+ cookie.domain() == current.domain() &&
75
+ cookie.path() == current.path()) {
76
+ // found a match
77
+ newCookies.removeAt(i);
78
+ break;
79
+ }
80
+ }
81
+
82
+ // did not find a match
83
+ if (!isDeletion) {
84
+ int countForDomain = 0;
85
+ for (int i = newCookies.size() - 1; i >= 0; --i) {
86
+ // Start from the end and delete the oldest cookies to keep a maximum count of 50.
87
+ const QNetworkCookie &current = newCookies.at(i);
88
+ if (isParentDomain(cookie.domain(), current.domain())
89
+ || isParentDomain(current.domain(), cookie.domain())) {
90
+ if (countForDomain >= 49)
91
+ newCookies.removeAt(i);
92
+ else
93
+ ++countForDomain;
94
+ }
95
+ }
96
+
97
+ newCookies += cookie;
98
+ }
99
+ }
100
+ setAllCookies(newCookies);
101
+ }
@@ -0,0 +1,15 @@
1
+ #include <QtNetwork/QNetworkCookieJar>
2
+ #include <QtNetwork/QNetworkCookie>
3
+
4
+ class NetworkCookieJar : public QNetworkCookieJar {
5
+
6
+ Q_OBJECT;
7
+
8
+ public:
9
+
10
+ NetworkCookieJar(QObject *parent = 0);
11
+
12
+ QList<QNetworkCookie> getAllCookies() const;
13
+ void clearCookies();
14
+ void overwriteCookies(const QList<QNetworkCookie>& cookieList);
15
+ };
data/src/Node.cpp ADDED
@@ -0,0 +1,14 @@
1
+ #include "Node.h"
2
+ #include "WebPage.h"
3
+
4
+ Node::Node(WebPage *page, QObject *parent) : Command(page, parent) {
5
+ }
6
+
7
+ void Node::start(QStringList &arguments) {
8
+ QStringList functionArguments(arguments);
9
+ QString functionName = functionArguments.takeFirst();
10
+ QVariant result = page()->invokeCapybaraFunction(functionName, functionArguments);
11
+ QString attributeValue = result.toString();
12
+ emit finished(new Response(true, attributeValue));
13
+ }
14
+
data/src/Node.h ADDED
@@ -0,0 +1,13 @@
1
+ #include "Command.h"
2
+ #include <QStringList>
3
+
4
+ class WebPage;
5
+
6
+ class Node : public Command {
7
+ Q_OBJECT
8
+
9
+ public:
10
+ Node(WebPage *page, QObject *parent = 0);
11
+ virtual void start(QStringList &arguments);
12
+ };
13
+
data/src/Render.cpp ADDED
@@ -0,0 +1,19 @@
1
+ #include "Render.h"
2
+ #include "WebPage.h"
3
+
4
+ Render::Render(WebPage *page, QObject *parent) : Command(page, parent) {
5
+ }
6
+
7
+ void Render::start(QStringList &arguments) {
8
+ QStringList functionArguments(arguments);
9
+ QString imagePath = functionArguments.takeFirst();
10
+ int width = functionArguments.takeFirst().toInt();
11
+ int height = functionArguments.takeFirst().toInt();
12
+
13
+ QSize size(width, height);
14
+ page()->setViewportSize(size);
15
+
16
+ bool result = page()->render( imagePath );
17
+
18
+ emit finished(new Response(result));
19
+ }
data/src/Render.h ADDED
@@ -0,0 +1,12 @@
1
+ #include "Command.h"
2
+ #include <QStringList>
3
+
4
+ class WebPage;
5
+
6
+ class Render : public Command {
7
+ Q_OBJECT
8
+
9
+ public:
10
+ Render(WebPage *page, QObject *parent = 0);
11
+ virtual void start(QStringList &arguments);
12
+ };
data/src/Reset.cpp ADDED
@@ -0,0 +1,20 @@
1
+ #include "Reset.h"
2
+ #include "WebPage.h"
3
+ #include "NetworkAccessManager.h"
4
+ #include "NetworkCookieJar.h"
5
+
6
+ Reset::Reset(WebPage *page, QObject *parent) : Command(page, parent) {
7
+ }
8
+
9
+ void Reset::start(QStringList &arguments) {
10
+ Q_UNUSED(arguments);
11
+
12
+ page()->triggerAction(QWebPage::Stop);
13
+ page()->currentFrame()->setHtml("<html><body></body></html>");
14
+ page()->networkAccessManager()->setCookieJar(new NetworkCookieJar());
15
+ page()->setCustomNetworkAccessManager();
16
+ page()->setUserAgent(NULL);
17
+ page()->resetResponseHeaders();
18
+ emit finished(new Response(true));
19
+ }
20
+
data/src/Reset.h ADDED
@@ -0,0 +1,12 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class Reset : public Command {
6
+ Q_OBJECT
7
+
8
+ public:
9
+ Reset(WebPage *page, QObject *parent = 0);
10
+ virtual void start(QStringList &arguments);
11
+ };
12
+
data/src/Response.cpp ADDED
@@ -0,0 +1,19 @@
1
+ #include "Response.h"
2
+ #include <iostream>
3
+
4
+ Response::Response(bool success, QString message) {
5
+ m_success = success;
6
+ m_message = message;
7
+ }
8
+
9
+ Response::Response(bool success) {
10
+ m_success = success;
11
+ }
12
+
13
+ bool Response::isSuccess() const {
14
+ return m_success;
15
+ }
16
+
17
+ QString Response::message() const {
18
+ return m_message;
19
+ }
data/src/Response.h ADDED
@@ -0,0 +1,13 @@
1
+ #include <QString>
2
+
3
+ class Response {
4
+ public:
5
+ Response(bool success, QString message);
6
+ Response(bool success);
7
+ bool isSuccess() const;
8
+ QString message() const;
9
+
10
+ private:
11
+ bool m_success;
12
+ QString m_message;
13
+ };
data/src/Server.cpp ADDED
@@ -0,0 +1,25 @@
1
+ #include "Server.h"
2
+ #include "WebPage.h"
3
+ #include "Connection.h"
4
+
5
+ #include <QTcpServer>
6
+
7
+ Server::Server(QObject *parent, bool ignoreSslErrors) : QObject(parent) {
8
+ m_tcp_server = new QTcpServer(this);
9
+ m_page = new WebPage(this);
10
+ m_page->setIgnoreSslErrors(ignoreSslErrors);
11
+ }
12
+
13
+ bool Server::start() {
14
+ connect(m_tcp_server, SIGNAL(newConnection()), this, SLOT(handleConnection()));
15
+ return m_tcp_server->listen(QHostAddress::Any, 0);
16
+ }
17
+
18
+ quint16 Server::server_port() const {
19
+ return m_tcp_server->serverPort();
20
+ }
21
+
22
+ void Server::handleConnection() {
23
+ QTcpSocket *socket = m_tcp_server->nextPendingConnection();
24
+ new Connection(socket, m_page, this);
25
+ }
data/src/Server.h ADDED
@@ -0,0 +1,21 @@
1
+ #include <QObject>
2
+
3
+ class QTcpServer;
4
+ class WebPage;
5
+
6
+ class Server : public QObject {
7
+ Q_OBJECT
8
+
9
+ public:
10
+ Server(QObject *parent, bool ignoreSslErrors);
11
+ bool start();
12
+ quint16 server_port() const;
13
+
14
+ public slots:
15
+ void handleConnection();
16
+
17
+ private:
18
+ QTcpServer *m_tcp_server;
19
+ WebPage *m_page;
20
+ };
21
+
data/src/SetCookie.cpp ADDED
@@ -0,0 +1,18 @@
1
+ #include "SetCookie.h"
2
+ #include "WebPage.h"
3
+ #include "NetworkCookieJar.h"
4
+ #include <QNetworkCookie>
5
+
6
+ SetCookie::SetCookie(WebPage *page, QObject *parent)
7
+ : Command(page, parent)
8
+ { }
9
+
10
+ void SetCookie::start(QStringList &arguments)
11
+ {
12
+ QList<QNetworkCookie> cookies = QNetworkCookie::parseCookies(arguments[0].toAscii());
13
+ NetworkCookieJar *jar = qobject_cast<NetworkCookieJar*>(page()
14
+ ->networkAccessManager()
15
+ ->cookieJar());
16
+ jar->overwriteCookies(cookies);
17
+ emit finished(new Response(true));
18
+ }