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
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'capybara/driver/webkit'
3
+
4
+ describe Capybara::Driver::Webkit do
5
+ before do
6
+ @driver = Capybara::Driver::Webkit.new(TestApp, :browser => $webkit_browser)
7
+ end
8
+
9
+ it_should_behave_like "driver"
10
+ it_should_behave_like "driver with javascript support"
11
+ it_should_behave_like "driver with cookies support"
12
+ it_should_behave_like "driver with header support"
13
+ it_should_behave_like "driver with status code support"
14
+ it_should_behave_like "driver with frame support"
15
+
16
+ it "returns the rack server port" do
17
+ @driver.server_port.should eq(@driver.instance_variable_get(:@rack_server).port)
18
+ end
19
+
20
+ end
@@ -0,0 +1,137 @@
1
+ # -*- encoding: UTF-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'capybara/webkit'
5
+
6
+ describe Capybara::Session do
7
+ subject { Capybara::Session.new(:reusable_webkit, @app) }
8
+ after { subject.reset! }
9
+
10
+ context "slow javascript app" do
11
+ before(:all) do
12
+ @app = lambda do |env|
13
+ body = <<-HTML
14
+ <html><body>
15
+ <form action="/next" id="submit_me"><input type="submit" value="Submit" /></form>
16
+ <p id="change_me">Hello</p>
17
+
18
+ <script type="text/javascript">
19
+ var form = document.getElementById('submit_me');
20
+ form.addEventListener("submit", function (event) {
21
+ event.preventDefault();
22
+ setTimeout(function () {
23
+ document.getElementById("change_me").innerHTML = 'Good' + 'bye';
24
+ }, 500);
25
+ });
26
+ </script>
27
+ </body></html>
28
+ HTML
29
+ [200,
30
+ { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
31
+ [body]]
32
+ end
33
+ end
34
+
35
+ before do
36
+ @default_wait_time = Capybara.default_wait_time
37
+ Capybara.default_wait_time = 1
38
+ end
39
+
40
+ after { Capybara.default_wait_time = @default_wait_time }
41
+
42
+ it "waits for a request to load" do
43
+ subject.visit("/")
44
+ subject.find_button("Submit").click
45
+ subject.should have_content("Goodbye");
46
+ end
47
+ end
48
+
49
+ context "simple app" do
50
+ before(:all) do
51
+ @app = lambda do |env|
52
+ body = <<-HTML
53
+ <html><body>
54
+ <strong>Hello</strong>
55
+ <span>UTF8文字列</span>
56
+ <input type="button" value="ボタン" />
57
+ </body></html>
58
+ HTML
59
+ [200,
60
+ { 'Content-Type' => 'text/html; charset=UTF-8', 'Content-Length' => body.length.to_s },
61
+ [body]]
62
+ end
63
+ end
64
+
65
+ before do
66
+ subject.visit("/")
67
+ end
68
+
69
+ it "inspects nodes" do
70
+ subject.all(:xpath, "//strong").first.inspect.should include("strong")
71
+ end
72
+
73
+ it "can read utf8 string" do
74
+ utf8str = subject.all(:xpath, "//span").first.text
75
+ utf8str.should eq('UTF8文字列')
76
+ end
77
+
78
+ it "can click utf8 string" do
79
+ subject.click_button('ボタン')
80
+ end
81
+ end
82
+
83
+ context "response headers with status code" do
84
+ before(:all) do
85
+ @app = lambda do |env|
86
+ params = ::Rack::Utils.parse_query(env['QUERY_STRING'])
87
+ if params["img"] == "true"
88
+ body = 'not found'
89
+ return [404, { 'Content-Type' => 'image/gif', 'Content-Length' => body.length.to_s }, [body]]
90
+ end
91
+ body = <<-HTML
92
+ <html>
93
+ <body>
94
+ <img src="?img=true">
95
+ </body>
96
+ </html>
97
+ HTML
98
+ [200,
99
+ { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s, 'X-Capybara' => 'WebKit'},
100
+ [body]]
101
+ end
102
+ end
103
+
104
+ it "should get status code" do
105
+ subject.visit '/'
106
+ subject.status_code.should == 200
107
+ end
108
+
109
+ it "should reset status code" do
110
+ subject.visit '/'
111
+ subject.status_code.should == 200
112
+ subject.reset!
113
+ subject.status_code.should == 0
114
+ end
115
+
116
+ it "should get response headers" do
117
+ subject.visit '/'
118
+ subject.response_headers['X-Capybara'].should == 'WebKit'
119
+ end
120
+
121
+ it "should reset response headers" do
122
+ subject.visit '/'
123
+ subject.response_headers['X-Capybara'].should == 'WebKit'
124
+ subject.reset!
125
+ subject.response_headers['X-Capybara'].should == nil
126
+ end
127
+ end
128
+ end
129
+
130
+ describe Capybara::Session, "with TestApp" do
131
+ before do
132
+ @session = Capybara::Session.new(:reusable_webkit, TestApp)
133
+ end
134
+
135
+ it_should_behave_like "session"
136
+ it_should_behave_like "session with javascript support"
137
+ end
@@ -0,0 +1,42 @@
1
+ require 'openssl'
2
+
3
+ pem = <<-PEM_ENCODED
4
+ -----BEGIN PRIVATE KEY-----
5
+ MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKW+grT6YW3gv79y
6
+ P9JkQDtm3cDSUhAhd/TEyRBt/pSKz3pNSygsleBJl2d7g8k0fteec95a7YnYRKGH
7
+ XhIpUOvl/3uaV2NVipqxwB+Z+0M+7HegxL3e4unaRFy9kf9/UXJzmuA9BTMLrm/w
8
+ IoW17f+dz7BIFZhCvRurkrmvzraNAgMBAAECgYBv4uB3bYJx20N16Jk+3OAjeXh/
9
+ Hzu4me9Rc7pLdgVinyYaaK0wrJBsfSFRASdgnyh1RAjx9K3f3PfPlwMg/XUbA6Yd
10
+ YOYlMnBUwCJX09TH8RFFCzJzbBylpk/sTF1geICln2O2BloT2cM24PlEPvyz1xLa
11
+ XvxCOnJJfgNU1K6kvQJBANcEVyOMJ9RBfI8gj1o7S70J9yJRI4jvqxuvcOxJBCi6
12
+ CDatkh/getHswsE3sLj25VhrNsi6UQcN8Bjm8Yjt8BsCQQDFVe0uCwobseprMOuW
13
+ dPU4+saN1cFnIT5Gp0iwYRPinjUlkh6H/AuUtUulKFXVmxk1KElpp1E3bxpCDgCp
14
+ oe53AkArO1Mt8Ys8kSIzQO+xy8RRsQRAoSHM8atsuJyy1YeBjM4D+GguApuPQ9Rw
15
+ tvrQZcv9OCleuJ98FKBW0XB1AKpLAkEAmOR3bJofDdAuWTjA/4TEzo32MsRwIZBv
16
+ KNzJg+bjOkzrzp1EzIVrD5/b6S20O1j9EeOR5as+UN3jEVS6DLQrBwJAe5OTrDiQ
17
+ vKtUaAwquC4f4Ia05KwJw+v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M_ENCODED
37
+
38
+ key = OpenSSL::PKey::RSA.new(pem)
39
+ cert = OpenSSL::X509::Certificate.new(pem)
40
+ $openssl_self_signed_ctx = OpenSSL::SSL::SSLContext.new
41
+ $openssl_self_signed_ctx.key = key
42
+ $openssl_self_signed_ctx.cert = cert
@@ -0,0 +1,25 @@
1
+ require 'rspec'
2
+ require 'rspec/autorun'
3
+
4
+ PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
5
+
6
+ $LOAD_PATH << File.join(PROJECT_ROOT, 'lib')
7
+
8
+ Dir[File.join(PROJECT_ROOT, 'spec', 'support', '**', '*.rb')].each { |file| require(file) }
9
+
10
+ spec_dir = nil
11
+ $:.detect do |dir|
12
+ if File.exists? File.join(dir, "capybara.rb")
13
+ spec_dir = File.expand_path(File.join(dir,"..","spec"))
14
+ $:.unshift( spec_dir )
15
+ end
16
+ end
17
+
18
+ require File.join(spec_dir,"spec_helper")
19
+
20
+ require 'capybara/driver/webkit/browser'
21
+ $webkit_browser = Capybara::Driver::Webkit::Browser.new(:socket_class => TCPSocket, :stdout => nil)
22
+
23
+ Capybara.register_driver :reusable_webkit do |app|
24
+ Capybara::Driver::Webkit.new(app, :browser => $webkit_browser)
25
+ end
data/src/Body.h ADDED
@@ -0,0 +1,12 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class Body : public Command {
6
+ Q_OBJECT
7
+
8
+ public:
9
+ Body(WebPage *page, QObject *parent = 0);
10
+ virtual void start(QStringList &arguments);
11
+ };
12
+
@@ -0,0 +1,18 @@
1
+ #include "ClearCookies.h"
2
+ #include "WebPage.h"
3
+ #include "NetworkCookieJar.h"
4
+ #include <QNetworkCookie>
5
+
6
+ ClearCookies::ClearCookies(WebPage *page, QObject *parent)
7
+ : Command(page, parent)
8
+ { }
9
+
10
+ void ClearCookies::start(QStringList &arguments)
11
+ {
12
+ Q_UNUSED(arguments);
13
+ NetworkCookieJar *jar = qobject_cast<NetworkCookieJar*>(page()
14
+ ->networkAccessManager()
15
+ ->cookieJar());
16
+ jar->clearCookies();
17
+ emit finished(new Response(true));
18
+ }
@@ -0,0 +1,11 @@
1
+ #include "Command.h"
2
+
3
+ class WebPage;
4
+
5
+ class ClearCookies : public Command {
6
+ Q_OBJECT;
7
+
8
+ public:
9
+ ClearCookies(WebPage *page, QObject *parent = 0);
10
+ virtual void start(QStringList &arguments);
11
+ };
data/src/Command.cpp ADDED
@@ -0,0 +1,15 @@
1
+ #include "Command.h"
2
+ #include "WebPage.h"
3
+
4
+ Command::Command(WebPage *page, QObject *parent) : QObject(parent) {
5
+ m_page = page;
6
+ }
7
+
8
+ void Command::start(QStringList &arguments) {
9
+ Q_UNUSED(arguments);
10
+ }
11
+
12
+ WebPage *Command::page() {
13
+ return m_page;
14
+ }
15
+
data/src/Command.h ADDED
@@ -0,0 +1,29 @@
1
+ #ifndef COMMAND_H
2
+ #define COMMAND_H
3
+
4
+ #include <QObject>
5
+ #include <QStringList>
6
+ #include "Response.h"
7
+
8
+ class WebPage;
9
+
10
+ class Command : public QObject {
11
+ Q_OBJECT
12
+
13
+ public:
14
+ Command(WebPage *page, QObject *parent = 0);
15
+ virtual void start(QStringList &arguments);
16
+
17
+ signals:
18
+ void finished(Response *response);
19
+
20
+ protected:
21
+ WebPage *page();
22
+
23
+ private:
24
+ WebPage *m_page;
25
+
26
+ };
27
+
28
+ #endif
29
+
@@ -0,0 +1,29 @@
1
+ #include "CommandFactory.h"
2
+ #include "Visit.h"
3
+ #include "Find.h"
4
+ #include "Command.h"
5
+ #include "Reset.h"
6
+ #include "Node.h"
7
+ #include "Url.h"
8
+ #include "Source.h"
9
+ #include "Evaluate.h"
10
+ #include "Execute.h"
11
+ #include "FrameFocus.h"
12
+ #include "Header.h"
13
+ #include "Render.h"
14
+ #include "Body.h"
15
+ #include "Status.h"
16
+ #include "Headers.h"
17
+ #include "SetCookie.h"
18
+ #include "ClearCookies.h"
19
+ #include "GetCookies.h"
20
+ #include "SetProxy.h"
21
+
22
+ CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
23
+ m_page = page;
24
+ }
25
+
26
+ Command *CommandFactory::createCommand(const char *name) {
27
+ #include "find_command.h"
28
+ return NULL;
29
+ }
@@ -0,0 +1,16 @@
1
+ #include <QObject>
2
+
3
+ class Command;
4
+ class WebPage;
5
+
6
+ class CommandFactory : public QObject {
7
+ Q_OBJECT
8
+
9
+ public:
10
+ CommandFactory(WebPage *page, QObject *parent = 0);
11
+ Command *createCommand(const char *name);
12
+
13
+ private:
14
+ WebPage *m_page;
15
+ };
16
+
@@ -0,0 +1,68 @@
1
+ #include "CommandParser.h"
2
+
3
+ #include <QIODevice>
4
+
5
+ CommandParser::CommandParser(QIODevice *device, QObject *parent) :
6
+ QObject(parent) {
7
+ m_device = device;
8
+ m_expectingDataSize = -1;
9
+ connect(m_device, SIGNAL(readyRead()), this, SLOT(checkNext()));
10
+ }
11
+
12
+ void CommandParser::checkNext() {
13
+ if (m_expectingDataSize == -1) {
14
+ if (m_device->canReadLine()) {
15
+ readLine();
16
+ checkNext();
17
+ }
18
+ } else {
19
+ if (m_device->bytesAvailable() >= m_expectingDataSize) {
20
+ readDataBlock();
21
+ checkNext();
22
+ }
23
+ }
24
+ }
25
+
26
+ void CommandParser::readLine() {
27
+ char buffer[128];
28
+ qint64 lineLength = m_device->readLine(buffer, 128);
29
+ if (lineLength != -1) {
30
+ buffer[lineLength - 1] = 0;
31
+ processNext(buffer);
32
+ }
33
+ }
34
+
35
+ void CommandParser::readDataBlock() {
36
+ char *buffer = new char[m_expectingDataSize + 1];
37
+ m_device->read(buffer, m_expectingDataSize);
38
+ buffer[m_expectingDataSize] = 0;
39
+ processNext(buffer);
40
+ m_expectingDataSize = -1;
41
+ delete[] buffer;
42
+ }
43
+
44
+ void CommandParser::processNext(const char *data) {
45
+ if (m_commandName.isNull()) {
46
+ m_commandName = data;
47
+ m_argumentsExpected = -1;
48
+ } else {
49
+ processArgument(data);
50
+ }
51
+ }
52
+
53
+ void CommandParser::processArgument(const char *data) {
54
+ if (m_argumentsExpected == -1) {
55
+ m_argumentsExpected = QString(data).toInt();
56
+ } else if (m_expectingDataSize == -1) {
57
+ m_expectingDataSize = QString(data).toInt();
58
+ } else {
59
+ m_arguments.append(QString::fromUtf8(data));
60
+ }
61
+
62
+ if (m_arguments.length() == m_argumentsExpected) {
63
+ emit commandReady(m_commandName, m_arguments);
64
+ m_commandName = QString();
65
+ m_arguments.clear();
66
+ m_argumentsExpected = -1;
67
+ }
68
+ }
@@ -0,0 +1,29 @@
1
+ #include <QObject>
2
+ #include <QStringList>
3
+
4
+ class QIODevice;
5
+
6
+ class CommandParser : public QObject {
7
+ Q_OBJECT
8
+
9
+ public:
10
+ CommandParser(QIODevice *device, QObject *parent = 0);
11
+
12
+ public slots:
13
+ void checkNext();
14
+
15
+ signals:
16
+ void commandReady(QString commandName, QStringList arguments);
17
+
18
+ private:
19
+ void readLine();
20
+ void readDataBlock();
21
+ void processNext(const char *line);
22
+ void processArgument(const char *data);
23
+ QIODevice *m_device;
24
+ QString m_commandName;
25
+ QStringList m_arguments;
26
+ int m_argumentsExpected;
27
+ int m_expectingDataSize;
28
+ };
29
+