otherinbox-capybara-webkit 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/.gitignore +19 -0
  2. data/.rspec +2 -0
  3. data/Appraisals +7 -0
  4. data/CONTRIBUTING.md +47 -0
  5. data/ChangeLog +70 -0
  6. data/Gemfile +3 -0
  7. data/Gemfile.lock +68 -0
  8. data/LICENSE +19 -0
  9. data/NEWS.md +36 -0
  10. data/README.md +114 -0
  11. data/Rakefile +65 -0
  12. data/bin/Info.plist +22 -0
  13. data/capybara-webkit.gemspec +28 -0
  14. data/extconf.rb +2 -0
  15. data/gemfiles/1.0.gemfile +7 -0
  16. data/gemfiles/1.0.gemfile.lock +70 -0
  17. data/gemfiles/1.1.gemfile +7 -0
  18. data/gemfiles/1.1.gemfile.lock +70 -0
  19. data/lib/capybara-webkit.rb +1 -0
  20. data/lib/capybara/driver/webkit.rb +135 -0
  21. data/lib/capybara/driver/webkit/browser.rb +168 -0
  22. data/lib/capybara/driver/webkit/connection.rb +120 -0
  23. data/lib/capybara/driver/webkit/cookie_jar.rb +55 -0
  24. data/lib/capybara/driver/webkit/node.rb +118 -0
  25. data/lib/capybara/driver/webkit/socket_debugger.rb +43 -0
  26. data/lib/capybara/driver/webkit/version.rb +7 -0
  27. data/lib/capybara/webkit.rb +11 -0
  28. data/lib/capybara/webkit/matchers.rb +37 -0
  29. data/lib/capybara_webkit_builder.rb +68 -0
  30. data/spec/browser_spec.rb +248 -0
  31. data/spec/capybara_webkit_builder_spec.rb +37 -0
  32. data/spec/connection_spec.rb +54 -0
  33. data/spec/cookie_jar_spec.rb +48 -0
  34. data/spec/driver_rendering_spec.rb +80 -0
  35. data/spec/driver_resize_window_spec.rb +59 -0
  36. data/spec/driver_spec.rb +1552 -0
  37. data/spec/integration/driver_spec.rb +20 -0
  38. data/spec/integration/session_spec.rb +137 -0
  39. data/spec/self_signed_ssl_cert.rb +42 -0
  40. data/spec/spec_helper.rb +46 -0
  41. data/src/Body.h +12 -0
  42. data/src/ClearCookies.cpp +15 -0
  43. data/src/ClearCookies.h +11 -0
  44. data/src/Command.cpp +19 -0
  45. data/src/Command.h +31 -0
  46. data/src/CommandFactory.cpp +38 -0
  47. data/src/CommandFactory.h +16 -0
  48. data/src/CommandParser.cpp +76 -0
  49. data/src/CommandParser.h +33 -0
  50. data/src/Connection.cpp +71 -0
  51. data/src/Connection.h +37 -0
  52. data/src/ConsoleMessages.cpp +10 -0
  53. data/src/ConsoleMessages.h +12 -0
  54. data/src/CurrentUrl.cpp +68 -0
  55. data/src/CurrentUrl.h +16 -0
  56. data/src/Evaluate.cpp +84 -0
  57. data/src/Evaluate.h +22 -0
  58. data/src/Execute.cpp +16 -0
  59. data/src/Execute.h +12 -0
  60. data/src/Find.cpp +19 -0
  61. data/src/Find.h +13 -0
  62. data/src/FrameFocus.cpp +66 -0
  63. data/src/FrameFocus.h +28 -0
  64. data/src/GetCookies.cpp +20 -0
  65. data/src/GetCookies.h +14 -0
  66. data/src/Header.cpp +18 -0
  67. data/src/Header.h +11 -0
  68. data/src/Headers.cpp +10 -0
  69. data/src/Headers.h +12 -0
  70. data/src/IgnoreSslErrors.cpp +12 -0
  71. data/src/IgnoreSslErrors.h +12 -0
  72. data/src/JavascriptInvocation.cpp +14 -0
  73. data/src/JavascriptInvocation.h +19 -0
  74. data/src/NetworkAccessManager.cpp +29 -0
  75. data/src/NetworkAccessManager.h +19 -0
  76. data/src/NetworkCookieJar.cpp +101 -0
  77. data/src/NetworkCookieJar.h +15 -0
  78. data/src/Node.cpp +14 -0
  79. data/src/Node.h +13 -0
  80. data/src/NullCommand.cpp +10 -0
  81. data/src/NullCommand.h +11 -0
  82. data/src/PageLoadingCommand.cpp +46 -0
  83. data/src/PageLoadingCommand.h +40 -0
  84. data/src/Render.cpp +18 -0
  85. data/src/Render.h +12 -0
  86. data/src/RequestedUrl.cpp +12 -0
  87. data/src/RequestedUrl.h +12 -0
  88. data/src/Reset.cpp +29 -0
  89. data/src/Reset.h +15 -0
  90. data/src/ResizeWindow.cpp +16 -0
  91. data/src/ResizeWindow.h +12 -0
  92. data/src/Response.cpp +24 -0
  93. data/src/Response.h +15 -0
  94. data/src/Server.cpp +24 -0
  95. data/src/Server.h +21 -0
  96. data/src/SetCookie.cpp +16 -0
  97. data/src/SetCookie.h +11 -0
  98. data/src/SetProxy.cpp +22 -0
  99. data/src/SetProxy.h +11 -0
  100. data/src/SetSkipImageLoading.cpp +11 -0
  101. data/src/SetSkipImageLoading.h +11 -0
  102. data/src/Source.cpp +18 -0
  103. data/src/Source.h +19 -0
  104. data/src/Status.cpp +12 -0
  105. data/src/Status.h +12 -0
  106. data/src/UnsupportedContentHandler.cpp +32 -0
  107. data/src/UnsupportedContentHandler.h +18 -0
  108. data/src/Url.cpp +12 -0
  109. data/src/Url.h +12 -0
  110. data/src/Visit.cpp +12 -0
  111. data/src/Visit.h +12 -0
  112. data/src/WebPage.cpp +246 -0
  113. data/src/WebPage.h +58 -0
  114. data/src/body.cpp +10 -0
  115. data/src/capybara.js +315 -0
  116. data/src/find_command.h +30 -0
  117. data/src/main.cpp +31 -0
  118. data/src/webkit_server.pro +87 -0
  119. data/src/webkit_server.qrc +5 -0
  120. data/templates/Command.cpp +10 -0
  121. data/templates/Command.h +12 -0
  122. data/webkit_server.pro +4 -0
  123. metadata +300 -0
@@ -0,0 +1,40 @@
1
+ #include <QObject>
2
+ #include <QStringList>
3
+
4
+ class Command;
5
+ class Response;
6
+ class WebPage;
7
+
8
+ /*
9
+ * Decorates a Command by deferring the finished() signal until any pending
10
+ * page loads are complete.
11
+ *
12
+ * If a Command starts a page load, no signal will be emitted until the page
13
+ * load is finished.
14
+ *
15
+ * If a pending page load fails, the command's response will be discarded and a
16
+ * failure response will be emitted instead.
17
+ */
18
+ class PageLoadingCommand : public QObject {
19
+ Q_OBJECT
20
+
21
+ public:
22
+ PageLoadingCommand(Command *command, WebPage *page, QObject *parent = 0);
23
+ void start();
24
+
25
+ public slots:
26
+ void pageLoadingFromCommand();
27
+ void pendingLoadFinished(bool success);
28
+ void commandFinished(Response *response);
29
+
30
+ signals:
31
+ void finished(Response *response);
32
+
33
+ private:
34
+ WebPage *m_page;
35
+ Command *m_command;
36
+ Response *m_pendingResponse;
37
+ bool m_pageSuccess;
38
+ bool m_pageLoadingFromCommand;
39
+ };
40
+
data/src/Render.cpp ADDED
@@ -0,0 +1,18 @@
1
+ #include "Render.h"
2
+ #include "WebPage.h"
3
+
4
+ Render::Render(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {
5
+ }
6
+
7
+ void Render::start() {
8
+ QString imagePath = arguments()[0];
9
+ int width = arguments()[1].toInt();
10
+ int height = arguments()[2].toInt();
11
+
12
+ QSize size(width, height);
13
+ page()->setViewportSize(size);
14
+
15
+ bool result = page()->render( imagePath );
16
+
17
+ emit finished(new Response(result));
18
+ }
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, QStringList &arguments, QObject *parent = 0);
11
+ virtual void start();
12
+ };
@@ -0,0 +1,12 @@
1
+ #include "RequestedUrl.h"
2
+ #include "WebPage.h"
3
+
4
+ RequestedUrl::RequestedUrl(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {
5
+ }
6
+
7
+ void RequestedUrl::start() {
8
+ QUrl humanUrl = page()->currentFrame()->requestedUrl();
9
+ QByteArray encodedBytes = humanUrl.toEncoded();
10
+ emit finished(new Response(true, encodedBytes));
11
+ }
12
+
@@ -0,0 +1,12 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class RequestedUrl : public Command {
6
+ Q_OBJECT
7
+
8
+ public:
9
+ RequestedUrl(WebPage *page, QStringList &arguments, QObject *parent = 0);
10
+ virtual void start();
11
+ };
12
+
data/src/Reset.cpp ADDED
@@ -0,0 +1,29 @@
1
+ #include "Reset.h"
2
+ #include "WebPage.h"
3
+ #include "NetworkAccessManager.h"
4
+ #include "NetworkCookieJar.h"
5
+
6
+ Reset::Reset(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {
7
+ }
8
+
9
+ void Reset::start() {
10
+ page()->triggerAction(QWebPage::Stop);
11
+
12
+ NetworkAccessManager* networkAccessManager = qobject_cast<NetworkAccessManager*>(page()->networkAccessManager());
13
+ networkAccessManager->setCookieJar(new NetworkCookieJar());
14
+ networkAccessManager->resetHeaders();
15
+
16
+ page()->setUserAgent(NULL);
17
+ page()->resetResponseHeaders();
18
+ page()->resetConsoleMessages();
19
+ page()->resetWindowSize();
20
+ resetHistory();
21
+ emit finished(new Response(true));
22
+ }
23
+
24
+ void Reset::resetHistory() {
25
+ // Clearing the history preserves the current history item, so set it to blank first.
26
+ page()->currentFrame()->setUrl(QUrl("about:blank"));
27
+ page()->history()->clear();
28
+ }
29
+
data/src/Reset.h ADDED
@@ -0,0 +1,15 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class Reset : public Command {
6
+ Q_OBJECT
7
+
8
+ public:
9
+ Reset(WebPage *page, QStringList &arguments, QObject *parent = 0);
10
+ virtual void start();
11
+
12
+ private:
13
+ void resetHistory();
14
+ };
15
+
@@ -0,0 +1,16 @@
1
+ #include "ResizeWindow.h"
2
+ #include "WebPage.h"
3
+
4
+ ResizeWindow::ResizeWindow(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {
5
+ }
6
+
7
+ void ResizeWindow::start() {
8
+ int width = arguments()[0].toInt();
9
+ int height = arguments()[1].toInt();
10
+
11
+ QSize size(width, height);
12
+ page()->setViewportSize(size);
13
+
14
+ emit finished(new Response(true));
15
+ }
16
+
@@ -0,0 +1,12 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class ResizeWindow : public Command {
6
+ Q_OBJECT
7
+
8
+ public:
9
+ ResizeWindow(WebPage *page, QStringList &arguments, QObject *parent = 0);
10
+ virtual void start();
11
+ };
12
+
data/src/Response.cpp ADDED
@@ -0,0 +1,24 @@
1
+ #include "Response.h"
2
+ #include <iostream>
3
+
4
+ Response::Response(bool success, QString message) {
5
+ m_success = success;
6
+ m_message = message.toUtf8();
7
+ }
8
+
9
+ Response::Response(bool success, QByteArray message) {
10
+ m_success = success;
11
+ m_message = message;
12
+ }
13
+
14
+ Response::Response(bool success) {
15
+ m_success = success;
16
+ }
17
+
18
+ bool Response::isSuccess() const {
19
+ return m_success;
20
+ }
21
+
22
+ QByteArray Response::message() const {
23
+ return m_message;
24
+ }
data/src/Response.h ADDED
@@ -0,0 +1,15 @@
1
+ #include <QString>
2
+ #include <QByteArray>
3
+
4
+ class Response {
5
+ public:
6
+ Response(bool success, QString message);
7
+ Response(bool success, QByteArray message);
8
+ Response(bool success);
9
+ bool isSuccess() const;
10
+ QByteArray message() const;
11
+
12
+ private:
13
+ bool m_success;
14
+ QByteArray m_message;
15
+ };
data/src/Server.cpp ADDED
@@ -0,0 +1,24 @@
1
+ #include "Server.h"
2
+ #include "WebPage.h"
3
+ #include "Connection.h"
4
+
5
+ #include <QTcpServer>
6
+
7
+ Server::Server(QObject *parent) : QObject(parent) {
8
+ m_tcp_server = new QTcpServer(this);
9
+ m_page = new WebPage(this);
10
+ }
11
+
12
+ bool Server::start() {
13
+ connect(m_tcp_server, SIGNAL(newConnection()), this, SLOT(handleConnection()));
14
+ return m_tcp_server->listen(QHostAddress::LocalHost, 0);
15
+ }
16
+
17
+ quint16 Server::server_port() const {
18
+ return m_tcp_server->serverPort();
19
+ }
20
+
21
+ void Server::handleConnection() {
22
+ QTcpSocket *socket = m_tcp_server->nextPendingConnection();
23
+ new Connection(socket, m_page, this);
24
+ }
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);
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,16 @@
1
+ #include "SetCookie.h"
2
+ #include "WebPage.h"
3
+ #include "NetworkCookieJar.h"
4
+ #include <QNetworkCookie>
5
+
6
+ SetCookie::SetCookie(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {}
7
+
8
+ void SetCookie::start()
9
+ {
10
+ QList<QNetworkCookie> cookies = QNetworkCookie::parseCookies(arguments()[0].toAscii());
11
+ NetworkCookieJar *jar = qobject_cast<NetworkCookieJar*>(page()
12
+ ->networkAccessManager()
13
+ ->cookieJar());
14
+ jar->overwriteCookies(cookies);
15
+ emit finished(new Response(true));
16
+ }
data/src/SetCookie.h ADDED
@@ -0,0 +1,11 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class SetCookie : public Command {
6
+ Q_OBJECT;
7
+
8
+ public:
9
+ SetCookie(WebPage *page, QStringList &arguments, QObject *parent = 0);
10
+ virtual void start();
11
+ };
data/src/SetProxy.cpp ADDED
@@ -0,0 +1,22 @@
1
+ #include "SetProxy.h"
2
+ #include "WebPage.h"
3
+ #include <QNetworkAccessManager>
4
+ #include <QNetworkProxy>
5
+
6
+ SetProxy::SetProxy(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {}
7
+
8
+ void SetProxy::start()
9
+ {
10
+ // default to empty proxy
11
+ QNetworkProxy proxy;
12
+
13
+ if (arguments().size() > 0)
14
+ proxy = QNetworkProxy(QNetworkProxy::HttpProxy,
15
+ arguments()[0],
16
+ (quint16)(arguments()[1].toInt()),
17
+ arguments()[2],
18
+ arguments()[3]);
19
+
20
+ page()->networkAccessManager()->setProxy(proxy);
21
+ emit finished(new Response(true));
22
+ }
data/src/SetProxy.h ADDED
@@ -0,0 +1,11 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class SetProxy : public Command {
6
+ Q_OBJECT;
7
+
8
+ public:
9
+ SetProxy(WebPage *page, QStringList &arguments, QObject *parent = 0);
10
+ virtual void start();
11
+ };
@@ -0,0 +1,11 @@
1
+ #include "SetSkipImageLoading.h"
2
+ #include "WebPage.h"
3
+
4
+ SetSkipImageLoading::SetSkipImageLoading(WebPage *page, QStringList &arguments, QObject *parent) :
5
+ Command(page, arguments, parent) {
6
+ }
7
+
8
+ void SetSkipImageLoading::start() {
9
+ page()->setSkipImageLoading(arguments().contains("true"));
10
+ emit finished(new Response(true));
11
+ }
@@ -0,0 +1,11 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class SetSkipImageLoading : public Command {
6
+ Q_OBJECT
7
+
8
+ public:
9
+ SetSkipImageLoading(WebPage *page, QStringList &arguments, QObject *parent = 0);
10
+ virtual void start();
11
+ };
data/src/Source.cpp ADDED
@@ -0,0 +1,18 @@
1
+ #include "Source.h"
2
+ #include "WebPage.h"
3
+
4
+ Source::Source(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {
5
+ }
6
+
7
+ void Source::start() {
8
+ QNetworkAccessManager* accessManager = page()->networkAccessManager();
9
+ QNetworkRequest request(page()->currentFrame()->url());
10
+ reply = accessManager->get(request);
11
+
12
+ connect(reply, SIGNAL(finished()), this, SLOT(sourceLoaded()));
13
+ }
14
+
15
+ void Source::sourceLoaded() {
16
+ emit finished(new Response(true, reply->readAll()));
17
+ }
18
+
data/src/Source.h ADDED
@@ -0,0 +1,19 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+ class QNetworkReply;
5
+
6
+ class Source : public Command {
7
+ Q_OBJECT
8
+
9
+ public:
10
+ Source(WebPage *page, QStringList &arguments, QObject *parent = 0);
11
+ virtual void start();
12
+
13
+ public slots:
14
+ void sourceLoaded();
15
+
16
+ private:
17
+ QNetworkReply *reply;
18
+ };
19
+
data/src/Status.cpp ADDED
@@ -0,0 +1,12 @@
1
+ #include "Status.h"
2
+ #include "WebPage.h"
3
+ #include <sstream>
4
+
5
+ Status::Status(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {
6
+ }
7
+
8
+ void Status::start() {
9
+ int status = page()->getLastStatus();
10
+ emit finished(new Response(true, QString::number(status)));
11
+ }
12
+
data/src/Status.h ADDED
@@ -0,0 +1,12 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class Status : public Command {
6
+ Q_OBJECT
7
+
8
+ public:
9
+ Status(WebPage *page, QStringList &arguments, QObject *parent = 0);
10
+ virtual void start();
11
+ };
12
+
@@ -0,0 +1,32 @@
1
+ #include "UnsupportedContentHandler.h"
2
+ #include "WebPage.h"
3
+ #include <QNetworkReply>
4
+
5
+ UnsupportedContentHandler::UnsupportedContentHandler(WebPage *page, QNetworkReply *reply, QObject *parent) : QObject(parent) {
6
+ m_page = page;
7
+ m_reply = reply;
8
+ connect(m_reply, SIGNAL(finished()), this, SLOT(handleUnsupportedContent()));
9
+ disconnect(m_page, SIGNAL(loadFinished(bool)), m_page, SLOT(loadFinished(bool)));
10
+ }
11
+
12
+ void UnsupportedContentHandler::handleUnsupportedContent() {
13
+ QVariant contentMimeType = m_reply->header(QNetworkRequest::ContentTypeHeader);
14
+ if(contentMimeType.isNull()) {
15
+ this->finish(false);
16
+ } else {
17
+ this->loadUnsupportedContent();
18
+ this->finish(true);
19
+ }
20
+ this->deleteLater();
21
+ }
22
+
23
+ void UnsupportedContentHandler::loadUnsupportedContent() {
24
+ QByteArray text = m_reply->readAll();
25
+ m_page->mainFrame()->setContent(text, QString("text/plain"), m_reply->url());
26
+ }
27
+
28
+ void UnsupportedContentHandler::finish(bool success) {
29
+ connect(m_page, SIGNAL(loadFinished(bool)), m_page, SLOT(loadFinished(bool)));
30
+ m_page->replyFinished(m_reply);
31
+ m_page->loadFinished(success);
32
+ }