phantom-ruby-browser 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +40 -0
- data/LICENSE +21 -0
- data/README.md +24 -0
- data/Rakefile +12 -0
- data/lib/phantom_ruby_browser.rb +58 -0
- data/phantom-ruby-browser.gemspec +19 -0
- data/spec/lib/phantom_ruby_browser_spec.rb +27 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/support/test_page_1.html +17 -0
- data/spec/support/test_page_2.html +17 -0
- data/spec/support/teststeps.js +38 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3d2f7cee9dc862cae17865bec78286a4b985b65a
|
4
|
+
data.tar.gz: 6e7867cc2b17ef6d49fd7154aeefad30b3de2c82
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a824b0bb63832c383eb8547f5c6bb1ee5bbc329292386525769260cbc9475439d75ef49f54c0671ed21e350afeda4d4a17411440a4e1e631791a4a62338c3664
|
7
|
+
data.tar.gz: 91a02f110a317f707b1c687b56923dd48028e4943f7e40c42066e4c943b8a4df9c354e171a4004c547f79ec1e543c84d3ea9894a861966805229402e08b5e3f3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
phantom-ruby-browser (0.0.1)
|
5
|
+
phantomjs (~> 1.9.7.1, >= 1.9.7.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
coderay (1.1.0)
|
11
|
+
diff-lcs (1.2.5)
|
12
|
+
method_source (0.8.2)
|
13
|
+
phantomjs (1.9.7.1)
|
14
|
+
pry (0.9.12.6)
|
15
|
+
coderay (~> 1.0)
|
16
|
+
method_source (~> 0.8)
|
17
|
+
slop (~> 3.4)
|
18
|
+
rake (10.1.1)
|
19
|
+
rspec (3.1.0)
|
20
|
+
rspec-core (~> 3.1.0)
|
21
|
+
rspec-expectations (~> 3.1.0)
|
22
|
+
rspec-mocks (~> 3.1.0)
|
23
|
+
rspec-core (3.1.5)
|
24
|
+
rspec-support (~> 3.1.0)
|
25
|
+
rspec-expectations (3.1.2)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.1.0)
|
28
|
+
rspec-mocks (3.1.2)
|
29
|
+
rspec-support (~> 3.1.0)
|
30
|
+
rspec-support (3.1.1)
|
31
|
+
slop (3.6.0)
|
32
|
+
|
33
|
+
PLATFORMS
|
34
|
+
ruby
|
35
|
+
|
36
|
+
DEPENDENCIES
|
37
|
+
phantom-ruby-browser!
|
38
|
+
pry (~> 0.9.12.2, >= 0.9.12.2)
|
39
|
+
rake (~> 10.1.0, >= 10.1.0)
|
40
|
+
rspec (~> 3.1.0, >= 3.1.0)
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Marek Aufart
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PhantomRubyBrowser
|
2
|
+
===============
|
3
|
+
|
4
|
+
Wraps PhantomJS headless browser into Ruby code and provides basic DOM operations like click and get the content.
|
5
|
+
|
6
|
+
**Under development (October 2014)**
|
7
|
+
|
8
|
+
Example
|
9
|
+
-------
|
10
|
+
E.g. print DOM of javascript rendered page
|
11
|
+
```
|
12
|
+
browser = PhantomRubyBrowser.new("http://google.com/analytics")
|
13
|
+
puts browser.content
|
14
|
+
```
|
15
|
+
|
16
|
+
How does it work
|
17
|
+
----------------
|
18
|
+
* PhantomJS binary is provided by https://github.com/colszowka/phantomjs-gem
|
19
|
+
* Ruby command is translated into javascript steps and passed to PhantomJS
|
20
|
+
* Output is returned
|
21
|
+
|
22
|
+
License
|
23
|
+
-------
|
24
|
+
Released under [MIT license](https://github.com/aufi/photo_geoloader/blob/master/LICENSE)
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'phantomjs'
|
2
|
+
|
3
|
+
class PhantomRubyBrowser
|
4
|
+
|
5
|
+
attr_reader :phantomjs, :location
|
6
|
+
|
7
|
+
def initialize(location)
|
8
|
+
@location = location
|
9
|
+
@phantomjs = Phantomjs
|
10
|
+
end
|
11
|
+
|
12
|
+
def content
|
13
|
+
execute "var page = require('webpage').create();
|
14
|
+
page.open('#{@location}', function() {
|
15
|
+
setTimeout(function(){var js = page.evaluate(function () {
|
16
|
+
return document;
|
17
|
+
});
|
18
|
+
console.log(js.all[0].outerHTML);
|
19
|
+
phantom.exit();}, 1000);
|
20
|
+
});"
|
21
|
+
end
|
22
|
+
|
23
|
+
def content_after_click(link_selector = 'a')
|
24
|
+
execute "var page = require('webpage').create();
|
25
|
+
page.open('#{@location}', function() {
|
26
|
+
var coords = page.evaluate(function(){
|
27
|
+
return document.querySelector('#{link_selector}').getBoundingClientRect();
|
28
|
+
});
|
29
|
+
page.sendEvent('click', coords.left, coords.top);
|
30
|
+
var js = page.evaluate(function () {
|
31
|
+
return document;
|
32
|
+
});
|
33
|
+
console.log(js.all[0].outerHTML);
|
34
|
+
phantom.exit();
|
35
|
+
});"
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def execute(commands)
|
41
|
+
result = ""
|
42
|
+
steps_file = Tempfile.new('prb_steps')
|
43
|
+
begin
|
44
|
+
steps_file.write(commands)
|
45
|
+
steps_file.flush
|
46
|
+
result = @phantomjs.run(steps_file.path)
|
47
|
+
ensure
|
48
|
+
steps_file.close
|
49
|
+
steps_file.unlink
|
50
|
+
end
|
51
|
+
result
|
52
|
+
end
|
53
|
+
|
54
|
+
def prepare_steps
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'phantom-ruby-browser'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2014-10-03'
|
5
|
+
s.summary = "Wraps PhantomJS into Ruby code and provides basic DOM operations."
|
6
|
+
s.description = "Wraps PhantomJS headless browser into Ruby code and provides basic DOM operations like click and get the content."
|
7
|
+
s.authors = ["Marek Aufart"]
|
8
|
+
s.email = 'aufi.cz@gmail.com'
|
9
|
+
s.files = `git ls-files`.split($\)
|
10
|
+
s.require_paths = ["lib"]
|
11
|
+
s.homepage = 'https://github.com/aufi/phantom-ruby-browser'
|
12
|
+
s.license = 'MIT'
|
13
|
+
|
14
|
+
s.add_dependency 'phantomjs', "~> 1.9.7.1", '>= 1.9.7.1'
|
15
|
+
|
16
|
+
s.add_development_dependency "rake", "~> 10.1.0", ">= 10.1.0"
|
17
|
+
s.add_development_dependency "rspec", "~> 3.1.0", ">= 3.1.0"
|
18
|
+
s.add_development_dependency "pry", "~> 0.9.12.2", ">= 0.9.12.2"
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe PhantomRubyBrowser do
|
4
|
+
|
5
|
+
let(:browser){ PhantomRubyBrowser.new("file://#{File.expand_path('spec/support/test_page_1.html')}") }
|
6
|
+
|
7
|
+
describe 'open page' do
|
8
|
+
it 'loads phantomjs' do
|
9
|
+
expect(browser.phantomjs.path).not_to be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'html' do
|
13
|
+
expect(browser.content).not_to be_nil
|
14
|
+
expect(browser.content).to include 'First test page'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'with javascript' do
|
18
|
+
expect(browser.content).to include 'javascript_loaded'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'click' do
|
23
|
+
it 'follow normal link' do
|
24
|
+
expect(browser.content_after_click('p > a')).to include 'Second test page'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>test page</title>
|
5
|
+
</head>
|
6
|
+
<body>
|
7
|
+
<h1>Welcome!<h1>
|
8
|
+
<h3>First test page</h3>
|
9
|
+
<script>
|
10
|
+
document.write("javascript" + "_loaded");
|
11
|
+
</script>
|
12
|
+
<p>
|
13
|
+
Lorem ipsum
|
14
|
+
<a href="test_page_2.html">test page 2</a>
|
15
|
+
</p>
|
16
|
+
</body>
|
17
|
+
</html>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>test page</title>
|
5
|
+
</head>
|
6
|
+
<body>
|
7
|
+
<h1>Welcome!<h1>
|
8
|
+
<h3>Second test page</h3>
|
9
|
+
<script>
|
10
|
+
document.write("javascript" + "_loaded");
|
11
|
+
</script>
|
12
|
+
<p>
|
13
|
+
Lorem ipsum
|
14
|
+
<a href="#" class="js_link" onclick="document.write('javascript' + '_clicked');">js test</a>
|
15
|
+
</p>
|
16
|
+
</body>
|
17
|
+
</html>
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
phantom.onError = function(msg, trace) {
|
3
|
+
var msgStack = ['PHANTOM ERROR: ' + msg];
|
4
|
+
if (trace && trace.length) {
|
5
|
+
msgStack.push('TRACE:');
|
6
|
+
trace.forEach(function(t) {
|
7
|
+
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
|
8
|
+
});
|
9
|
+
}
|
10
|
+
console.error(msgStack.join('\n'));
|
11
|
+
phantom.exit(1);
|
12
|
+
};
|
13
|
+
|
14
|
+
|
15
|
+
var page = require('webpage').create();
|
16
|
+
|
17
|
+
page.onResourceRequested = function (request) {
|
18
|
+
console.log('[PRB] loading:', request.url, JSON.stringify(request));
|
19
|
+
};
|
20
|
+
|
21
|
+
page.onResourceReceived = function (response) {
|
22
|
+
console.log('[PRB] loaded:', response.status, response.url, JSON.stringify(response));
|
23
|
+
};
|
24
|
+
|
25
|
+
page.open('http://google.com/analytics', function(status) {
|
26
|
+
setTimeout(function(){
|
27
|
+
/*var coords = page.evaluate(function(){
|
28
|
+
return document.querySelector('a').getBoundingClientRect();
|
29
|
+
});
|
30
|
+
page.sendEvent('click', coords.left, coords.top);*/
|
31
|
+
var js = page.evaluate(function () {
|
32
|
+
return document;
|
33
|
+
});
|
34
|
+
console.log(js.all[0].outerHTML);
|
35
|
+
console.log("[PRB] status:", status);
|
36
|
+
phantom.exit();
|
37
|
+
}, 2000);
|
38
|
+
});
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: phantom-ruby-browser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marek Aufart
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: phantomjs
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.9.7.1
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.9.7.1
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.9.7.1
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.9.7.1
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 10.1.0
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 10.1.0
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 10.1.0
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 10.1.0
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rspec
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 3.1.0
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 3.1.0
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 3.1.0
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 3.1.0
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: pry
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 0.9.12.2
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.9.12.2
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.9.12.2
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 0.9.12.2
|
93
|
+
description: Wraps PhantomJS headless browser into Ruby code and provides basic DOM
|
94
|
+
operations like click and get the content.
|
95
|
+
email: aufi.cz@gmail.com
|
96
|
+
executables: []
|
97
|
+
extensions: []
|
98
|
+
extra_rdoc_files: []
|
99
|
+
files:
|
100
|
+
- ".gitignore"
|
101
|
+
- Gemfile
|
102
|
+
- Gemfile.lock
|
103
|
+
- LICENSE
|
104
|
+
- README.md
|
105
|
+
- Rakefile
|
106
|
+
- lib/phantom_ruby_browser.rb
|
107
|
+
- phantom-ruby-browser.gemspec
|
108
|
+
- spec/lib/phantom_ruby_browser_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
- spec/support/test_page_1.html
|
111
|
+
- spec/support/test_page_2.html
|
112
|
+
- spec/support/teststeps.js
|
113
|
+
homepage: https://github.com/aufi/phantom-ruby-browser
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.2.2
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Wraps PhantomJS into Ruby code and provides basic DOM operations.
|
137
|
+
test_files: []
|