capybara-ng 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fe60760b896ba2c87ac3f698db240f5a249c302
4
- data.tar.gz: e293bf655ff886c0b5ef48fc569f5f11f30309f5
3
+ metadata.gz: 52f9e96dfa5ce32dfc53c66148d301d82e20dbc1
4
+ data.tar.gz: 0827939aa362e10eedea420ecee2baa077aa98c2
5
5
  SHA512:
6
- metadata.gz: 2584ef8d0dceeb3b578eeb23c6e0620d6ccb449a8af4cd68bf4fa4d01d4148b132e1876f29ce86ba3c10578a680e28217307d822cb205c50bfdb79fd9b5a084d
7
- data.tar.gz: c441a769921abad480c6ac4c619417fb8426db4be0b42fc10da2aa3060bf95682ae98e1571d1f94449c02b36adb14384fdf78bec6e69dcb6e29efc3cea2db59b
6
+ metadata.gz: 48174a887a6f82642bd390f24383e2c8f360104a89904fc42c6398b47189f0dbf5bd0773c62b35ba0e5a1f5868eb8079f62d69fd231505cdb63fc5f2b4fbb517
7
+ data.tar.gz: 1fc2e8382d0d81355b214cbcdf69b7bd8b285b253d486beff3dbc65df881073ac1b7000faf1a5164d2439deb4304b5c0b937aea84c72040ef05fba193dd468bc
data/README.md CHANGED
@@ -1,7 +1,18 @@
1
1
  # Angular
2
2
 
3
- Basic bindings for AngularJS to be used with Capybara. Logic is based into Protractor.
3
+ Basic bindings for AngularJS to be used with Capybara. Implementation is based into
4
+ logic copied from Protractor.
4
5
 
6
+ NOTE: At this point of development, I would classify DSL API "unstable". This means that
7
+ DSL language may change in incompatible ways.
8
+
9
+ ## Related Projects
10
+
11
+ - https://github.com/jnicklas/capybara
12
+ - https://github.com/angular/protractor
13
+ - https://code.google.com/p/selenium/wiki/RubyBindings
14
+ - https://github.com/teampoltergeist/poltergeist
15
+ - https://github.com/colszowka/phantomjs-gem
5
16
 
6
17
  ## Installation
7
18
 
@@ -20,12 +31,22 @@ Or install it yourself as:
20
31
 
21
32
  ## Usage
22
33
 
34
+ ### Setup
35
+
36
+ spec/spec_helper.rb
37
+ ````ruby
38
+ RSpec.configure do |config|
39
+ config.include Angular::DSL
40
+ ...
41
+ end
42
+ ```
43
+
44
+ ### Experimenting features
45
+
23
46
  ````ruby
24
47
  require 'spec_helper'
25
48
 
26
49
  describe 'capybara', type: :feature do
27
- include Angular::DSL
28
-
29
50
  it 'test' do
30
51
  visit 'http://localhost:4000/sampler'
31
52
 
@@ -76,6 +97,70 @@ describe 'capybara', type: :feature do
76
97
  end
77
98
  ```
78
99
 
100
+ ### Example test
101
+ Example taken from https://github.com/kikonen/sampler/blob/master/dummy/spec/request/task_spec.rb
102
+
103
+ #### Test file
104
+ ````ruby
105
+ require 'spec_helper'
106
+
107
+ describe 'capybara', type: :feature do
108
+ include Angular::DSL
109
+
110
+ it 'create task' do
111
+ visit 'http://localhost:4000/sampler'
112
+
113
+ init_task_count = 2
114
+
115
+ # login
116
+ ng_repeater_row('view in views', 0).click
117
+ ng_model('user.username').set('admin')
118
+ ng_model('user.password').set('password')
119
+ click_button 'Login'
120
+
121
+ # check initial state
122
+ ng_repeater_row('view in views', 2).click
123
+ expect(ng_repeater_rows('task in $data').length).to eq init_task_count
124
+
125
+ # create new task
126
+ click_link 'New Task'
127
+ ng_model('task.name').set('Some')
128
+ ng_model('task.message').set('Thing')
129
+ ng_model('task.done').set('true')
130
+ click_button 'Save'
131
+
132
+ # check task is created
133
+ rows = ng_repeater_columns('task in $data', 'task.name')
134
+ expect(rows.length).to eq init_task_count + 1
135
+
136
+ # delete created task
137
+ cell = rows.select { |row| row.visible_text == 'Some' }.first
138
+ cell.click
139
+ accept_alert do
140
+ click_button 'Delete'
141
+ end
142
+ expect(ng_repeater_rows('task in $data').length).to eq init_task_count
143
+ end
144
+ end
145
+ ```
146
+
147
+ #### Running Example Test
148
+ ````bash
149
+ # download and install chrome-driver
150
+ # http://chromedriver.storage.googleapis.com/index.html
151
+
152
+ git clone git@github.com:kikonen/sampler.git
153
+ cd sampler
154
+ cd dummy
155
+ bundle
156
+ rails s -p 4000&
157
+ bundle exec rspec spec/request/test_spec.rb
158
+ bundle exec rspec spec/request/task_spec.rb
159
+ fg
160
+ CTRL^C
161
+ ```
162
+
163
+
79
164
  ## Contributing
80
165
 
81
166
  1. Fork it ( https://github.com/[my-github-username]/capybara/fork )
@@ -9,3 +9,5 @@ require 'angular/client_script'
9
9
  require 'angular/dsl'
10
10
  require 'angular/setup'
11
11
  require 'angular/waiter'
12
+
13
+ require 'angular/capybara_setup'
@@ -0,0 +1,4 @@
1
+ # bind logic to capybara
2
+ class Capybara::Session
3
+ include Angular::DSL
4
+ end
@@ -15,129 +15,267 @@ module DSL
15
15
  #
16
16
  # @return current location absolute url
17
17
  #
18
- def ng_location_abs(using = 'body')
19
- ng.make_call :getLocationAbsUrl, [using], false
18
+ def ng_location_abs(opt = {})
19
+ selector = opt.delete(:rootSelector) || 'body'
20
+ ng.make_call :getLocationAbsUrl, [selector], opt
20
21
  end
21
22
 
22
23
  # @return current location absolute url
23
24
  #
24
- def ng_location(using = 'body')
25
- ng.make_call :getLocation, [using], false
25
+ def ng_location(opt = {})
26
+ selector = opt.delete(:rootSelector) || 'body'
27
+ ng.make_call :getLocation, [selector], opt
26
28
  end
27
29
 
28
30
  #
29
31
  # @return current location
30
32
  #
31
- def ng_set_location(url, using = 'body')
32
- ng.make_call :setLocation, [using, url], false
33
+ def ng_set_location(url, opt = {})
34
+ selector = opt.delete(:rootSelector) || 'body'
35
+ ng.make_call :setLocation, [selector, url], opt
33
36
  end
34
37
 
35
38
  #
39
+ # @param opt
40
+ # - :rootSelector
41
+ # - :wait
36
42
  # @return eval result
37
43
  #
38
- def ng_eval(selector, expr)
39
- ng.make_call :evaluate, [selector, expr], false
44
+ def ng_eval(expr, opt = {})
45
+ selector = opt.delete(:rootSelector) || 'body'
46
+ ng.make_call :evaluate, [selector, expr], opt
47
+ end
48
+
49
+ #
50
+ # Does binding exist
51
+ #
52
+ # @param opt
53
+ # - :exact
54
+ # - :using
55
+ # - :rootSelector
56
+ # - :wait
57
+ # @return true | false
58
+ #
59
+ def has_ng_binding?(binding, opt = {})
60
+ ng_bindings(model, opt)
61
+ true
62
+ rescue NotFound
63
+ false
40
64
  end
41
65
 
42
66
  #
43
67
  # Node for nth binding match
68
+ #
69
+ # @param opt
70
+ # - :row
71
+ # - :exact
72
+ # - :using
73
+ # - :rootSelector
74
+ # - :wait
44
75
  # @return nth node
45
76
  #
46
- def ng_binding(binding, exact = false, row = 0, using = nil , rootSelector = 'body')
47
- ng_bindings(binding, exact, using , rootSelector)[row]
77
+ def ng_binding(binding, opt = {})
78
+ row = ng.row(opt)
79
+ ng_bindings(binding, opt)[row]
48
80
  end
49
81
 
50
82
  #
51
83
  # All nodes matching binding
52
84
  #
85
+ # @param opt
86
+ # - :exact
87
+ # - :using
88
+ # - :rootSelector
89
+ # - :wait
53
90
  # @return [node, ...]
54
91
  #
55
- def ng_bindings(binding, exact = false, using = nil , rootSelector = 'body')
56
- ng.make_call :findBindings, [binding, exact, using, rootSelector], true
92
+ def ng_bindings(binding, opt = {})
93
+ ng.get_nodes :findBindings, [binding, opt[:exact]], opt
94
+ end
95
+
96
+ #
97
+ # Does model exist
98
+ #
99
+ # @param opt
100
+ # - :using
101
+ # - :rootSelector
102
+ # - :wait
103
+ # @return true | false
104
+ #
105
+ def has_ng_model?(model, opt = {})
106
+ ng_models(model, opt)
107
+ true
108
+ rescue NotFound
109
+ false
57
110
  end
58
111
 
59
112
  #
60
113
  # Node for nth model match
61
114
  #
115
+ # @param opt
116
+ # - :row
117
+ # - :using
118
+ # - :rootSelector
119
+ # - :wait
62
120
  # @return nth node
63
121
  #
64
- def ng_model(model, row = 0, using = nil , rootSelector = 'body')
65
- ng_models(model, using, rootSelector)[row]
122
+ def ng_model(model, opt = {})
123
+ row = ng.row(opt)
124
+ ng_models(model, opt)[row]
66
125
  end
67
126
 
68
127
  #
69
128
  # All nodes matching model
70
129
  #
130
+ # @param opt
131
+ # - :using
132
+ # - :rootSelector
133
+ # - :wait
71
134
  # @return [node, ...]
72
135
  #
73
- def ng_models(model, using = nil , rootSelector = 'body')
74
- ng.make_call :findByModel, [model, using, rootSelector], true
136
+ def ng_models(model, opt = {})
137
+ ng.get_nodes :findByModel, [model], opt
138
+ end
139
+
140
+ #
141
+ # Does option exist
142
+ #
143
+ # @param opt
144
+ # - :using
145
+ # - :rootSelector
146
+ # - :wait
147
+ # @return true | false
148
+ #
149
+ def has_ng_option?(options, opt = {})
150
+ ng_options(options, opt)
151
+ true
152
+ rescue NotFound
153
+ false
75
154
  end
76
155
 
77
156
  #
78
157
  # Node for nth option
79
158
  #
159
+ # @param opt
160
+ # - :row
161
+ # - :using
162
+ # - :rootSelector
163
+ # - :wait
80
164
  # @return nth node
81
165
  #
82
- def ng_option(options, row = 0, using = nil , rootSelector = 'body')
83
- ng_options(options, using, rootSelector)[row]
166
+ def ng_option(options, opt = {})
167
+ row = ng.row(opt)
168
+ ng_options(options, opt)[row]
84
169
  end
85
170
 
86
171
  #
87
172
  # All option values matching option
173
+ #
174
+ # @param opt
175
+ # - :using
176
+ # - :rootSelector
177
+ # - :wait
88
178
  # @return [node, ...]
89
179
  #
90
- def ng_options(options, using = nil , rootSelector = 'body')
91
- ng.make_call :findByOptions, [options, using, rootSelector], true
180
+ def ng_options(options, opt = {})
181
+ ng.get_nodes :findByOptions, [options], opt
182
+ end
183
+
184
+ #
185
+ # Does row exist
186
+ #
187
+ # @param opt
188
+ # - :using
189
+ # - :rootSelector
190
+ # - :wait
191
+ # @return true | false
192
+ #
193
+ def has_ng_repeater_row?(repeater, opt = {})
194
+ ng.get_nodes(:findRepeaterRows, [repeater, 0], opt)
195
+ true
196
+ rescue NotFound
197
+ false
92
198
  end
93
199
 
94
200
  #
95
201
  # Node for nth repeater row
202
+ #
203
+ # @param opt
204
+ # - :row
205
+ # - :using
206
+ # - :rootSelector
207
+ # - :wait
96
208
  # @return nth node
97
209
  #
98
- def ng_repeater_row(repeater, row = 0, using = nil , rootSelector = 'body')
99
- ng.make_call(:findRepeaterRows, [repeater, row, using, rootSelector], true).first
210
+ def ng_repeater_row(repeater, opt = {})
211
+ row = ng.row(opt)
212
+ data = ng.get_nodes(:findRepeaterRows, [repeater, row], opt)
213
+ data.first
100
214
  end
101
215
 
102
216
  #
103
217
  # All nodes matching repeater
104
218
  #
219
+ # @param opt
220
+ # - :using
221
+ # - :rootSelector
222
+ # - :wait
105
223
  # @return [node, ...]
106
224
  #
107
- def ng_repeater_rows(repeater, using = nil , rootSelector = 'body')
108
- ng.make_call :findAllRepeaterRows, [repeater, using, rootSelector], true
225
+ def ng_repeater_rows(repeater, opt = {})
226
+ ng.get_nodes :findAllRepeaterRows, [repeater], opt
109
227
  end
110
228
 
111
229
  #
112
230
  # Node for column binding value in row
113
231
  #
232
+ # @param opt
233
+ # - :row
234
+ # - :using
235
+ # - :rootSelector
236
+ # - :wait
114
237
  # @return nth node
115
238
  #
116
- def ng_repeater_column(repeater, binding, row = 0, using = nil , rootSelector = 'body')
117
- ng_repeater_columns(repeater, binding, using, rootSelector)[row]
239
+ def ng_repeater_column(repeater, binding, opt = {})
240
+ row = ng.row(opt)
241
+ ng_repeater_columns(repeater, binding, opt)[row]
118
242
  end
119
243
 
120
244
  #
121
245
  # Node for column binding value in all rows
122
246
  #
247
+ # @param opt
248
+ # - :using
249
+ # - :rootSelector
250
+ # - :wait
123
251
  # @return [node, ...]
124
252
  #
125
- def ng_repeater_columns(repeater, binding, using = nil , rootSelector = 'body')
126
- ng.make_call :findRepeaterColumn, [repeater, binding, using, rootSelector], true
253
+ def ng_repeater_columns(repeater, binding, opt = {})
254
+ ng.get_nodes :findRepeaterColumn, [repeater, binding], opt
127
255
  end
128
256
 
129
257
  #
258
+ # @param opt
259
+ # - :row
260
+ # - :using
261
+ # - :rootSelector
262
+ # - :wait
130
263
  # @return nth node
131
264
  #
132
- def ng_repeater_element(repeater, index, binding, row = 0, using = nil, rootSelector = 'body')
133
- ng_repeater_elements(repeater, index, binding, using, rootSelector)[row]
265
+ def ng_repeater_element(repeater, index, binding, opt = {})
266
+ row = ng.row(opt)
267
+ ng_repeater_elements(repeater, index, binding, opt)[row]
134
268
  end
135
269
 
136
270
  #
271
+ # @param opt
272
+ # - :using
273
+ # - :rootSelector
274
+ # - :wait
137
275
  # @return [node, ...]
138
276
  #
139
- def ng_repeater_elements(repeater, index, binding, using = nil, rootSelector = 'body')
140
- ng.make_call :findRepeaterElement, [repeater, index, binding, using, rootSelector], true
277
+ def ng_repeater_elements(repeater, index, binding, opt = {})
278
+ ng.get_nodes :findRepeaterElement, [repeater, index, binding], opt
141
279
  end
142
280
  end
143
281
  end
@@ -11,9 +11,40 @@ module Angular
11
11
  Waiter.new(self).wait_until_ready
12
12
  end
13
13
 
14
- def make_call(method, params, nodes)
15
- ng_wait
14
+ def row(opt)
15
+ opt.has_key?(:row) ? opt[:row] : 0
16
+ end
17
+
18
+ #
19
+ # @param opt
20
+ # - :using
21
+ # - :rootSelector
22
+ # - :wait
23
+ #
24
+ def get_nodes(method, params, opt = {})
25
+ opt = {
26
+ nodes: true,
27
+ using: nil,
28
+ rootSelector: 'body',
29
+ }.merge(opt)
30
+ make_call(method, params, opt)
31
+ end
32
+
33
+ #
34
+ # @param opt
35
+ # - :nodes
36
+ # - :wait
37
+ #
38
+ def make_call(method, params, opt = {})
39
+ opt = {
40
+ nodes: false,
41
+ wait: true,
42
+ }.merge(opt)
43
+
44
+ ng_wait if opt[:wait]
16
45
 
46
+ params << opt[:using] if opt.has_key?(:using)
47
+ params << opt[:rootSelector] if opt.has_key?(:rootSelector)
17
48
  js_params = params.map do |p|
18
49
  if p.nil?
19
50
  'null'
@@ -26,12 +57,12 @@ module Angular
26
57
  end
27
58
 
28
59
  js = "#{method}(#{js_params.join(', ')});"
29
- logger.info js
60
+ logger.debug js
30
61
 
31
62
  js_result = page.evaluate_script(js);
32
- logger.info js_result
63
+ # logger.debug js_result
33
64
 
34
- if nodes
65
+ if opt[:nodes]
35
66
  make_result method, params, js_result
36
67
  else
37
68
  js_result
@@ -1,3 +1,3 @@
1
1
  module Angular
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-ng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - kari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-01 00:00:00.000000000 Z
11
+ date: 2014-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,6 +68,7 @@ files:
68
68
  - Rakefile
69
69
  - capybara-ng.gemspec
70
70
  - lib/angular.rb
71
+ - lib/angular/capybara_setup.rb
71
72
  - lib/angular/client_script.rb
72
73
  - lib/angular/driver.rb
73
74
  - lib/angular/dsl.rb