capybara-envjs 0.1.0.pre0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +5 -0
- data/Manifest.txt +9 -0
- data/README.rdoc +49 -0
- data/Rakefile +25 -0
- data/lib/capybara/driver/envjs_driver.rb +244 -0
- data/lib/capybara/envjs.rb +2 -0
- data/spec/driver/envjs_driver_spec.rb +17 -0
- data/spec/session/envjs_session_spec.rb +28 -0
- data/spec/spec_helper.rb +12 -0
- metadata +149 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
= capybara-envjs
|
2
|
+
|
3
|
+
* http://github.com/smparkes/capybara-envjs
|
4
|
+
|
5
|
+
== Description:
|
6
|
+
|
7
|
+
capybara-envjs is a {Capybara}[http://github.com/jnicklas/capybara] driver for the envjs gem ({GitHub}[http://github.com/smparkes/env-js],
|
8
|
+
{rubygems.org}[http://rubygems.org/gems/envjs].
|
9
|
+
It is similar to Capybara's rack-test driver in that it runs tests against your rack aplplication directly but fully supports javascript in your appliction.
|
10
|
+
|
11
|
+
== Install:
|
12
|
+
|
13
|
+
Install as a gem:
|
14
|
+
|
15
|
+
gem install capybara-envjs
|
16
|
+
|
17
|
+
== Development:
|
18
|
+
|
19
|
+
* Source hosted at {GitHub}[http://github.com/smparkes/capybara-envjs].
|
20
|
+
* Report issues on {GitHub Issues}[http://github.com/smparkes/capybara-envjs/issues]
|
21
|
+
|
22
|
+
== Using with Capybara
|
23
|
+
|
24
|
+
See the Capybara docs. Use the symbol :envjs for the driver/default_driver as appropriate.
|
25
|
+
|
26
|
+
== License:
|
27
|
+
|
28
|
+
(The MIT License)
|
29
|
+
|
30
|
+
Copyright (c) 2010 Steven Parkes
|
31
|
+
|
32
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
33
|
+
a copy of this software and associated documentation files (the
|
34
|
+
'Software'), to deal in the Software without restriction, including
|
35
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
36
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
37
|
+
permit persons to whom the Software is furnished to do so, subject to
|
38
|
+
the following conditions:
|
39
|
+
|
40
|
+
The above copyright notice and this permission notice shall be
|
41
|
+
included in all copies or substantial portions of the Software.
|
42
|
+
|
43
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
44
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
45
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
46
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
47
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
48
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
49
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
gem 'hoe', '>= 2.5'
|
4
|
+
require 'hoe'
|
5
|
+
|
6
|
+
Hoe.plugin
|
7
|
+
Hoe.plugin :newgem, :debugging, :doofus, :git
|
8
|
+
|
9
|
+
Hoe.spec 'capybara-envjs' do
|
10
|
+
developer 'Steven Parkes', 'smparkes@smparkes.net'
|
11
|
+
self.version = "0.1.0.pre0"
|
12
|
+
|
13
|
+
self.readme_file = 'README.rdoc'
|
14
|
+
self.extra_rdoc_files = Dir['*.rdoc']
|
15
|
+
|
16
|
+
self.extra_deps = [
|
17
|
+
['capybara', '>= 0.3.0'],
|
18
|
+
['envjs', '>= 0.1.4']
|
19
|
+
]
|
20
|
+
|
21
|
+
self.extra_dev_deps = [
|
22
|
+
['rack-test', '>= 0.5.3'],
|
23
|
+
['rspec', '>= 1.3.0']
|
24
|
+
]
|
25
|
+
end
|
@@ -0,0 +1,244 @@
|
|
1
|
+
require 'rack/test'
|
2
|
+
|
3
|
+
class Capybara::Driver::Envjs < Capybara::Driver::Base
|
4
|
+
class Node < Capybara::Node
|
5
|
+
def text
|
6
|
+
node.innerText
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](name)
|
10
|
+
value = if name.to_sym == :class
|
11
|
+
node.className
|
12
|
+
else
|
13
|
+
node.getAttribute(name.to_s)
|
14
|
+
end
|
15
|
+
return value if value and not value.to_s.empty?
|
16
|
+
end
|
17
|
+
|
18
|
+
def [](name)
|
19
|
+
attr_name = name.to_s
|
20
|
+
attr_name == "class" and attr_name = "className"
|
21
|
+
case
|
22
|
+
when 'select' == tag_name && 'value' == attr_name
|
23
|
+
if node['multiple']
|
24
|
+
all_unfiltered(".//option[@selected='selected']").map { |option| option.node.innerText }
|
25
|
+
else
|
26
|
+
node.value
|
27
|
+
end
|
28
|
+
# when 'input' == tag_name && 'checkbox' == type && 'checked' == attr_name
|
29
|
+
# node[attr_name] == 'checked' ? true : false
|
30
|
+
else
|
31
|
+
node[attr_name]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def set(value)
|
36
|
+
case node.tagName
|
37
|
+
when "TEXTAREA"
|
38
|
+
node.innerText = value
|
39
|
+
else
|
40
|
+
case node.getAttribute("type")
|
41
|
+
when "checkbox", "radio"; node.checked = value
|
42
|
+
else; node.setAttribute("value",value)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def select(option)
|
48
|
+
option_node = all_unfiltered("//option[text()='#{option}']") || all_unfiltered("//option[contains(.,'#{option}')]")
|
49
|
+
option_node[0].node.selected = true
|
50
|
+
rescue Exception => e
|
51
|
+
# print e
|
52
|
+
options = all_unfiltered(".//option").map { |o| "'#{o.text}'" }.join(', ')
|
53
|
+
raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def unselect(option)
|
57
|
+
if !node['multiple']
|
58
|
+
raise Capybara::UnselectNotAllowed, "Cannot unselect option '#{option}' from single select box."
|
59
|
+
end
|
60
|
+
|
61
|
+
begin
|
62
|
+
option_node = (all_unfiltered("//option[text()='#{option}']") || all_unfiltered("//option[contains(.,'#{option}')]")).first
|
63
|
+
option_node.node.selected = false
|
64
|
+
rescue Exception => e
|
65
|
+
# print e
|
66
|
+
options = all_unfiltered(".//option").map { |o| "'#{o.text}'" }.join(', ')
|
67
|
+
raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def click
|
72
|
+
_event(self,"MouseEvent",'click',true,true)
|
73
|
+
end
|
74
|
+
|
75
|
+
def drag_to(element)
|
76
|
+
_event(self,"MouseEvent",'mousedown',true,true)
|
77
|
+
_event(element,"MouseEvent",'mousemove',true,true)
|
78
|
+
_event(element,"MouseEvent",'mouseup',true,true)
|
79
|
+
end
|
80
|
+
|
81
|
+
def tag_name
|
82
|
+
node.tagName.downcase
|
83
|
+
end
|
84
|
+
|
85
|
+
def visible?
|
86
|
+
all_unfiltered("./ancestor-or-self::*[contains(@style, 'display:none') or contains(@style, 'display: none')]").empty?
|
87
|
+
end
|
88
|
+
|
89
|
+
def all_unfiltered selector
|
90
|
+
window = @driver.browser["window"]
|
91
|
+
null = @driver.browser["null"]
|
92
|
+
type = window["XPathResult"]["ANY_TYPE"]
|
93
|
+
result_set = window.document.evaluate selector, node, null, type, null
|
94
|
+
nodes = []
|
95
|
+
while n = result_set.iterateNext()
|
96
|
+
nodes << Node.new(@driver, n)
|
97
|
+
end
|
98
|
+
nodes
|
99
|
+
end
|
100
|
+
|
101
|
+
def trigger event
|
102
|
+
# FIX: look up class and attributes
|
103
|
+
_event(self, "", event.to_s, true, true )
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
def _event(target,cls,type,bubbles,cancelable)
|
109
|
+
e = @driver.browser["document"].createEvent(cls);
|
110
|
+
e.initEvent(type,bubbles,cancelable);
|
111
|
+
target.node.dispatchEvent(e);
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
include ::Rack::Test::Methods
|
117
|
+
attr_reader :app
|
118
|
+
|
119
|
+
alias_method :response, :last_response
|
120
|
+
alias_method :request, :last_request
|
121
|
+
|
122
|
+
def initialize(app)
|
123
|
+
@app = app
|
124
|
+
|
125
|
+
master_load = browser.master["load"]
|
126
|
+
|
127
|
+
browser.master["load"] = proc do |*args|
|
128
|
+
if args.size == 2 and args[1].to_s != "[object split_global]"
|
129
|
+
file, window = *args
|
130
|
+
get(file, {}, env)
|
131
|
+
window["evaluate"].call response.body
|
132
|
+
else
|
133
|
+
master_load.call *args
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
browser["window"]["$envx"]["connection"] =
|
138
|
+
browser.master["connection"] = @connection = proc do |*args|
|
139
|
+
xhr, responseHandler, data = *args
|
140
|
+
url = xhr.url.sub %r{^file://}, ""
|
141
|
+
params = data || {}
|
142
|
+
method = xhr["method"].downcase.to_sym
|
143
|
+
e = env;
|
144
|
+
if method == :post or method == :put
|
145
|
+
e.merge! "CONTENT_TYPE" => xhr.headers["Content-Type"]
|
146
|
+
end
|
147
|
+
if e["CONTENT_TYPE"] =~ %r{^multipart/form-data;}
|
148
|
+
e["CONTENT_LENGTH"] ||= params.length
|
149
|
+
end
|
150
|
+
# print "send #{method} #{url}\n"
|
151
|
+
send method, url, params, e
|
152
|
+
while response.status == 302
|
153
|
+
method = :get
|
154
|
+
# p response.nil?, (response and response.status)
|
155
|
+
url = response.location
|
156
|
+
# print "send #{method} #{url}\n"
|
157
|
+
send method, url, params, e
|
158
|
+
end
|
159
|
+
@source = response.body
|
160
|
+
response.headers.each do |k,v|
|
161
|
+
xhr.responseHeaders[k] = v
|
162
|
+
end
|
163
|
+
xhr.status = response.status
|
164
|
+
xhr.responseText = response.body
|
165
|
+
responseHandler.call
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def visit(path)
|
170
|
+
browser["window"].location.href = path
|
171
|
+
end
|
172
|
+
|
173
|
+
def current_url
|
174
|
+
browser["window"].location.href
|
175
|
+
end
|
176
|
+
|
177
|
+
def source
|
178
|
+
@source
|
179
|
+
end
|
180
|
+
|
181
|
+
def body
|
182
|
+
browser["window"].document.xml
|
183
|
+
end
|
184
|
+
|
185
|
+
def response_headers
|
186
|
+
response.headers
|
187
|
+
end
|
188
|
+
|
189
|
+
def find(selector)
|
190
|
+
window = browser["window"]
|
191
|
+
null = browser["null"]
|
192
|
+
type = window["XPathResult"]["ANY_TYPE"]
|
193
|
+
# print window.document.xml
|
194
|
+
result_set = window.document.evaluate selector, window.document, null, type, null
|
195
|
+
nodes = []
|
196
|
+
while n = result_set.iterateNext()
|
197
|
+
nodes << Node.new(self, n)
|
198
|
+
end
|
199
|
+
nodes
|
200
|
+
end
|
201
|
+
|
202
|
+
def wait?; true; end
|
203
|
+
|
204
|
+
def wait_until max
|
205
|
+
fired, wait = *browser["Envjs"].wait(-max*1000)
|
206
|
+
raise Capybara::TimeoutError if !fired && wait.nil?
|
207
|
+
end
|
208
|
+
|
209
|
+
def evaluate_script(script)
|
210
|
+
browser["window"]["evaluate"].call(script)
|
211
|
+
end
|
212
|
+
|
213
|
+
def browser
|
214
|
+
unless @_browser
|
215
|
+
require 'johnson/tracemonkey'
|
216
|
+
require 'envjs/runtime'
|
217
|
+
@_browser = Johnson::Runtime.new :size => 0x4000000
|
218
|
+
@_browser.extend Envjs::Runtime
|
219
|
+
end
|
220
|
+
|
221
|
+
@_browser
|
222
|
+
end
|
223
|
+
|
224
|
+
def obeys_absolute_xpath
|
225
|
+
true
|
226
|
+
end
|
227
|
+
|
228
|
+
def has_shortcircuit_timeout
|
229
|
+
true
|
230
|
+
end
|
231
|
+
|
232
|
+
private
|
233
|
+
|
234
|
+
def env
|
235
|
+
env = {}
|
236
|
+
begin
|
237
|
+
env["HTTP_REFERER"] = request.url
|
238
|
+
rescue Rack::Test::Error
|
239
|
+
# no request yet
|
240
|
+
end
|
241
|
+
env
|
242
|
+
end
|
243
|
+
|
244
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
describe Capybara::Driver::Envjs do
|
4
|
+
driver = nil
|
5
|
+
before do
|
6
|
+
@driver = (driver ||= Capybara::Driver::Envjs.new(TestApp))
|
7
|
+
end
|
8
|
+
after do
|
9
|
+
@driver.browser["window"].location = "about:blank"
|
10
|
+
end
|
11
|
+
|
12
|
+
it_should_behave_like "driver"
|
13
|
+
it_should_behave_like "driver with javascript support"
|
14
|
+
it_should_behave_like "driver with header support"
|
15
|
+
it_should_behave_like "driver with node path support"
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
describe Capybara::Driver::Envjs do
|
4
|
+
session = nil
|
5
|
+
before do
|
6
|
+
@session = (session ||= Capybara::Session.new(:envjs, TestApp))
|
7
|
+
end
|
8
|
+
after do
|
9
|
+
@session.driver.browser["window"].location = "about:blank"
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#driver' do
|
13
|
+
it "should be an envjs driver" do
|
14
|
+
@session.driver.should be_an_instance_of(Capybara::Driver::Envjs)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#mode' do
|
19
|
+
it "should remember the mode" do
|
20
|
+
@session.mode.should == :envjs
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it_should_behave_like "session"
|
25
|
+
it_should_behave_like "session with javascript support"
|
26
|
+
it_should_behave_like "session with headers support"
|
27
|
+
|
28
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'capybara/envjs'
|
3
|
+
|
4
|
+
spec_dir = nil
|
5
|
+
$:.detect do |dir|
|
6
|
+
if File.exists? File.join(dir, "capybara.rb")
|
7
|
+
spec_dir = File.expand_path(File.join(dir,"..","spec"))
|
8
|
+
$:.unshift( spec_dir )
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require File.join(spec_dir,"spec_helper")
|
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: capybara-envjs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: true
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- pre0
|
10
|
+
version: 0.1.0.pre0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Steven Parkes
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-02-25 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: capybara
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 3
|
31
|
+
- 0
|
32
|
+
version: 0.3.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: envjs
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
- 1
|
45
|
+
- 4
|
46
|
+
version: 0.1.4
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rack-test
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
- 5
|
59
|
+
- 3
|
60
|
+
version: 0.5.3
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rspec
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 1
|
72
|
+
- 3
|
73
|
+
- 0
|
74
|
+
version: 1.3.0
|
75
|
+
type: :development
|
76
|
+
version_requirements: *id004
|
77
|
+
- !ruby/object:Gem::Dependency
|
78
|
+
name: hoe
|
79
|
+
prerelease: false
|
80
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 2
|
86
|
+
- 5
|
87
|
+
- 0
|
88
|
+
version: 2.5.0
|
89
|
+
type: :development
|
90
|
+
version_requirements: *id005
|
91
|
+
description: |-
|
92
|
+
capybara-envjs is a {Capybara}[http://github.com/jnicklas/capybara] driver for the envjs gem ({GitHub}[http://github.com/smparkes/env-js],
|
93
|
+
{rubygems.org}[http://rubygems.org/gems/envjs].
|
94
|
+
It is similar to Capybara's rack-test driver in that it runs tests against your rack aplplication directly but fully supports javascript in your appliction.
|
95
|
+
email:
|
96
|
+
- smparkes@smparkes.net
|
97
|
+
executables: []
|
98
|
+
|
99
|
+
extensions: []
|
100
|
+
|
101
|
+
extra_rdoc_files:
|
102
|
+
- History.txt
|
103
|
+
- Manifest.txt
|
104
|
+
- README.rdoc
|
105
|
+
files:
|
106
|
+
- History.txt
|
107
|
+
- Manifest.txt
|
108
|
+
- README.rdoc
|
109
|
+
- Rakefile
|
110
|
+
- lib/capybara/driver/envjs_driver.rb
|
111
|
+
- lib/capybara/envjs.rb
|
112
|
+
- spec/driver/envjs_driver_spec.rb
|
113
|
+
- spec/session/envjs_session_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
has_rdoc: true
|
116
|
+
homepage: http://github.com/smparkes/capybara-envjs
|
117
|
+
licenses: []
|
118
|
+
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options:
|
121
|
+
- --main
|
122
|
+
- README.rdoc
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
segments:
|
130
|
+
- 0
|
131
|
+
version: "0"
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">"
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
segments:
|
137
|
+
- 1
|
138
|
+
- 3
|
139
|
+
- 1
|
140
|
+
version: 1.3.1
|
141
|
+
requirements: []
|
142
|
+
|
143
|
+
rubyforge_project: capybara-envjs
|
144
|
+
rubygems_version: 1.3.6
|
145
|
+
signing_key:
|
146
|
+
specification_version: 3
|
147
|
+
summary: capybara-envjs is a {Capybara}[http://github.com/jnicklas/capybara] driver for the envjs gem ({GitHub}[http://github.com/smparkes/env-js], {rubygems.org}[http://rubygems.org/gems/envjs]
|
148
|
+
test_files: []
|
149
|
+
|