napybara 0.5.0 → 0.6.0

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: c7b2cbbf480b100b3a1adc606cfa57b05868f860
4
- data.tar.gz: 2a4ec7daa20873ffb807f49eba6302d4aa9171d1
3
+ metadata.gz: dea35db336bc51c0fc90751281e7fc09460ca9d8
4
+ data.tar.gz: 0ad486329f004d1bd180333eb6ff8d109a130089
5
5
  SHA512:
6
- metadata.gz: b5661e2643515c2fa34eacef4702e1aa577f17f84ee4f02a9d0cf7d7929f544613ad183cc6c1ecf1ef6b78a584958376d634997d415819aa3b85a89794901e5f
7
- data.tar.gz: 7eff003cfac47e53c8f476679e123630913f7eb289dc59bf71fbe48063bee022b049b0ddfa805ac35eeaaa05b2ec7d3f72b56735a512eddec629dce7842ca2f6
6
+ metadata.gz: f15a944eda01a2496cbbf76b217139c2e5c75eff72ecc1ef9e1b3febe5dc9998c5ddf9fa0f8832294f14403a4590a8bd9e6828c03a303f0b9df54cd5fe012356
7
+ data.tar.gz: 4856ea54f2bdbb92311ce06daea0ab463312bc09287df95af220972a8b76e676657025f6f261208b8a656828660a38d252c02ccdfa249b49ec3f069e8e82adf6
@@ -1,6 +1,10 @@
1
1
  # Changelog
2
2
 
3
- ## master
3
+ ## 0.6.0
4
+
5
+ Allow Capybara options to be passed to finders.
6
+
7
+ ## 0.5.0
4
8
 
5
9
  Rename `#get` as #node. `#get` may become deprecated soon.
6
10
 
@@ -10,7 +14,6 @@ Add `#selector`.
10
14
 
11
15
  Update README.
12
16
 
13
-
14
17
  ## 0.4.0
15
18
 
16
19
  Require separate record selector for finding records.
data/README.md CHANGED
@@ -6,6 +6,9 @@ So you're writing an integration test for the following page:
6
6
 
7
7
  ```html
8
8
  <html>
9
+ <head>
10
+ <title>Your messages</title>
11
+ </head>
9
12
  <body>
10
13
  <ul class='message-list'>
11
14
  <li class="message" id="message-1">Hello world!</li>
@@ -103,21 +106,20 @@ looking for "#message-1".
103
106
 
104
107
  If the ruby object is identified by a method other than the object's id, you can replace `{id}` with the method e.g. `{name}`, `{to_s}`.
105
108
 
106
-
107
109
  ## Checking if an element exists
108
110
 
109
111
  `Napybara::Element#finder` also adds `has_` and `has_no_` methods to the element.
110
112
  With the Napybara elements above, you can call:
111
113
 
112
114
  ```ruby
113
- expect(messages_page.has_form?).to be_true
115
+ expect(messages_page.has_form?).to eq(true)
114
116
  expect(messages_page).to have_form
115
117
 
116
- expect(messages_page.has_message?(some_message)).to be_true
118
+ expect(messages_page.has_message?(some_message)).to eq(true)
117
119
  expect(messages_page).to have_message(some_message)
118
120
 
119
121
  non_existent_message = Message.find(3)
120
- expect(messages_page.has_no_message?(non_existent_message)).to be_true
122
+ expect(messages_page.has_no_message?(non_existent_message)).to eq(true)
121
123
  expect(messages_page).to have_no_message(non_existent_message)
122
124
  ```
123
125
 
@@ -216,6 +218,23 @@ end
216
218
 
217
219
  It may not sexy, but it gets the job done :)
218
220
 
221
+ ### Passing Capybara options to the finder
222
+
223
+ You can pass Capybara options to the finder:
224
+
225
+ ```ruby
226
+ let(:messages_page) do
227
+ Napybara::Element.new(self) do |page|
228
+ page.finder :title, 'head title', visible: false
229
+ end
230
+ end
231
+
232
+ # ...
233
+
234
+ expect(page.title.node.text).to eq('Your messages')
235
+
236
+ ```
237
+
219
238
  ## Putting it all together
220
239
 
221
240
  Oh yeah, the "N" in Napybara stands for nesting. Here's how you can define the
@@ -5,5 +5,6 @@ end
5
5
 
6
6
  require "napybara/element"
7
7
  require "napybara/element_array"
8
+ require "napybara/finder_methods_appender"
8
9
  require "napybara/selector"
9
10
  require "napybara/version"
@@ -14,33 +14,11 @@ module Napybara
14
14
  block.call(self) if block_given?
15
15
  end
16
16
 
17
- def finder(child_element_name, child_element_selector, record_selector = nil, &block)
18
- self.define_singleton_method(child_element_name) do |record = nil|
19
- selector = Selector.new(child_element_selector, record_selector, record)
20
- selector_string = selector.to_s
17
+ def finder(child_element_name, child_element_selector, *optional_args, &block)
18
+ appender = FinderMethodsAppender.new(
19
+ self, child_element_name, child_element_selector, optional_args, block)
21
20
 
22
- self.class.new(self.get.find(selector_string), self, selector_string, &block)
23
- end
24
-
25
- self.define_singleton_method("has_#{child_element_name}?") do |record = nil|
26
- selector = Selector.new(child_element_selector, record_selector, record)
27
- self.get.has_css?(selector.to_s)
28
- end
29
-
30
- self.define_singleton_method("has_no_#{child_element_name}?") do |record = nil|
31
- selector = Selector.new(child_element_selector, record_selector, record)
32
- self.get.has_no_css?(selector.to_s)
33
- end
34
-
35
- self.define_singleton_method(child_element_name.to_s.pluralize) do
36
- elements = self.get.all(child_element_selector)
37
- .map.with_index do |child_element, i|
38
-
39
- self.class.new(child_element, self, child_element_selector, &block)
40
- end
41
-
42
- ElementArray.new(elements, self, child_element_selector)
43
- end
21
+ appender.execute
44
22
  end
45
23
 
46
24
  def selector
@@ -0,0 +1,94 @@
1
+ require 'napybara'
2
+
3
+ module Napybara
4
+ class FinderMethodsAppender
5
+ attr_reader :element, :child_element_name, :child_element_selector,
6
+ :optional_args, :block
7
+
8
+ def initialize(
9
+ element, child_element_name, child_element_selector, optional_args, block)
10
+
11
+ @element, @child_element_name, @child_element_selector, @optional_args, @block =
12
+ element, child_element_name, child_element_selector, optional_args, block
13
+ end
14
+
15
+ def record_selector
16
+ return if optional_args.empty?
17
+ return if !optional_args[0].is_a?(String)
18
+
19
+ optional_args[0]
20
+ end
21
+
22
+ def capybara_options
23
+ return {} if optional_args.empty?
24
+ return {} if !optional_args.last.is_a?(Hash)
25
+
26
+ optional_args.last
27
+ end
28
+
29
+ def execute
30
+ define_child_element_method
31
+ define_has_child_element_method
32
+ define_has_no_child_element_method
33
+ define_child_elements_method
34
+ end
35
+
36
+ private
37
+
38
+ def define_child_element_method
39
+ appender = self
40
+
41
+ element.define_singleton_method(child_element_name) do |record = nil|
42
+ selector = Selector.new(
43
+ appender.child_element_selector,
44
+ appender.record_selector,
45
+ record)
46
+
47
+ selector_string = selector.to_s
48
+
49
+ capybara_element =
50
+ self.get.find(selector_string, appender.capybara_options)
51
+
52
+ self.class.new(capybara_element, self, selector_string, &appender.block)
53
+ end
54
+ end
55
+
56
+ def define_has_child_element_method
57
+ appender = self
58
+
59
+ element.define_singleton_method("has_#{child_element_name}?") do |record = nil|
60
+ selector = Selector.new(
61
+ appender.child_element_selector,
62
+ appender.record_selector, record)
63
+
64
+ self.get.has_css?(selector.to_s)
65
+ end
66
+ end
67
+
68
+ def define_has_no_child_element_method
69
+ appender = self
70
+
71
+ element.define_singleton_method("has_no_#{child_element_name}?") do |record = nil|
72
+ selector = Selector.new(
73
+ appender.child_element_selector,
74
+ appender.record_selector,
75
+ record)
76
+
77
+ self.get.has_no_css?(selector.to_s)
78
+ end
79
+ end
80
+
81
+ def define_child_elements_method
82
+ appender = self
83
+
84
+ element.define_singleton_method(child_element_name.to_s.pluralize) do
85
+ elements = self.get.all(appender.child_element_selector).map do |child_element|
86
+ self.class.new(
87
+ child_element, self, appender.child_element_selector, &appender.block)
88
+ end
89
+
90
+ ElementArray.new(elements, self, appender.child_element_selector)
91
+ end
92
+ end
93
+ end
94
+ end
@@ -1,3 +1,3 @@
1
1
  module Napybara
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -27,8 +27,9 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "overcommit"
28
28
  spec.add_development_dependency "poltergeist"
29
29
  spec.add_development_dependency "rake"
30
- spec.add_development_dependency "rspec"
30
+ spec.add_development_dependency "rspec", "~> 2.14.0"
31
31
  spec.add_development_dependency "rspec-example_steps"
32
32
  spec.add_development_dependency "rubocop"
33
33
  spec.add_development_dependency "sinatra"
34
+ spec.add_development_dependency "verified_double"
34
35
  end
@@ -5,6 +5,9 @@ describe 'Readme example:' do
5
5
  let(:capybara_page) do
6
6
  Capybara.string <<-HTML
7
7
  <html>
8
+ <head>
9
+ <title>Your messages</title>
10
+ </head>
8
11
  <body>
9
12
  <ul class='messages-list'>
10
13
  <li class="message" id="message-1">Hello world!</li>
@@ -87,15 +90,15 @@ describe 'Readme example:' do
87
90
  end
88
91
 
89
92
  Then "I can check for the existence of elements" do
90
- expect(@messages_page.has_form?).to be_true
93
+ expect(@messages_page.has_form?).to eq(true)
91
94
  expect(@messages_page).to have_form
92
95
 
93
96
  some_message = OpenStruct.new(id: 1)
94
- expect(@messages_page.has_message?(some_message)).to be_true
97
+ expect(@messages_page.has_message?(some_message)).to eq(true)
95
98
  expect(@messages_page).to have_message(some_message)
96
99
 
97
100
  non_existent_message = OpenStruct.new(id: 3)
98
- expect(@messages_page.has_no_message?(non_existent_message)).to be_true
101
+ expect(@messages_page.has_no_message?(non_existent_message)).to eq(true)
99
102
  expect(@messages_page).to have_no_message(non_existent_message)
100
103
  end
101
104
  end
@@ -202,4 +205,20 @@ describe 'Readme example:' do
202
205
  .to eq('.messages-list .message')
203
206
  end
204
207
  end
208
+
209
+ Steps "Passing Capybara options to a finder" do
210
+ Given "I have a capybara page" do
211
+ capybara_page
212
+ end
213
+
214
+ When "I pass Capybara options to the finder" do
215
+ @messages_page = Napybara::Element.new(capybara_page) do |page|
216
+ page.finder :title, 'head title', visible: false
217
+ end
218
+ end
219
+
220
+ Then "I should see that those options were passed to the finder's node" do
221
+ expect(@messages_page.title.node.text).to eq('Your messages')
222
+ end
223
+ end
205
224
  end
@@ -70,7 +70,7 @@ describe Napybara::Element do
70
70
  page = described_class.new(capybara_page)
71
71
  page.finder(:button, 'button')
72
72
 
73
- expect(page.buttons).to have(2).elements
73
+ expect(page.buttons.size).to eq(2)
74
74
  expect(page.buttons).to be_all do |element|
75
75
  element.get.tag_name == 'button'
76
76
  end
@@ -82,7 +82,7 @@ describe Napybara::Element do
82
82
  button.finder :img, 'img'
83
83
  end
84
84
 
85
- expect(page.buttons).to have(2).elements
85
+ expect(page.buttons.size).to eq(2)
86
86
  expect(page.buttons[0].img.get.tag_name).to eq('img')
87
87
  end
88
88
 
@@ -92,7 +92,7 @@ describe Napybara::Element do
92
92
 
93
93
  object = OpenStruct.new(id: 1)
94
94
 
95
- expect(page.has_form?(object)).to be_true
95
+ expect(page.has_form?(object)).to eq(true)
96
96
  end
97
97
 
98
98
  it 'adds a method to check if the element has no sub-element matching a record' do
@@ -101,7 +101,7 @@ describe Napybara::Element do
101
101
 
102
102
  object = OpenStruct.new(id: 1)
103
103
 
104
- expect(page.has_no_form?(object)).to be_false
104
+ expect(page.has_no_form?(object)).to eq(false)
105
105
  end
106
106
  end
107
107
 
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ describe Napybara::FinderMethodsAppender do
4
+ let(:block) { nil }
5
+ let(:capybara_options) { { visible: false } }
6
+ let(:child_element_name) { 'some_element' }
7
+ let(:child_element_selector) { '.some_element' }
8
+ let(:element) { VerifiedDouble.instance_of(Napybara::Element) }
9
+ let(:optional_args) { [] }
10
+ let(:record_selector) { '#some_element_{id}' }
11
+
12
+ subject do
13
+ described_class.new(
14
+ element,
15
+ child_element_name,
16
+ child_element_selector,
17
+ optional_args,
18
+ block
19
+ )
20
+ end
21
+
22
+ describe "#record_selector" do
23
+ context "when there is no optional arg" do
24
+ before do
25
+ expect(optional_args).to be_empty
26
+ end
27
+
28
+ it "should be nil" do
29
+ expect(subject.record_selector).to be_nil
30
+ end
31
+ end
32
+
33
+ context "when there is one optional arg and it is a String" do
34
+ let(:optional_args) { [record_selector] }
35
+
36
+ it "should be the string" do
37
+ expect(subject.record_selector).to eq(record_selector)
38
+ end
39
+ end
40
+
41
+ context "when there is one optional arg and it is a Hash" do
42
+ let(:optional_args) { [capybara_options] }
43
+
44
+ it "should be the nil" do
45
+ expect(subject.record_selector).to be_nil
46
+ end
47
+ end
48
+
49
+ context "when there are multiple optional args" do
50
+ let(:optional_args) { [record_selector, capybara_options] }
51
+
52
+ it "should be the first argument" do
53
+ expect(subject.record_selector).to eq(record_selector)
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "#capybara_options" do
59
+ context "when there is no optional arg" do
60
+ before do
61
+ expect(optional_args).to be_empty
62
+ end
63
+
64
+ it "should be {}" do
65
+ expect(subject.capybara_options).to eq({})
66
+ end
67
+ end
68
+
69
+ context "when there is one optional arg and it is a String" do
70
+ let(:optional_args) { [record_selector] }
71
+
72
+ it "should be the {}" do
73
+ expect(subject.capybara_options).to eq({})
74
+ end
75
+ end
76
+
77
+ context "when there is one optional arg and it is a hash" do
78
+ let(:optional_args) { [capybara_options] }
79
+
80
+ it "should be the hash" do
81
+ expect(subject.capybara_options).to eq(capybara_options)
82
+ end
83
+ end
84
+
85
+ context "when there are multiple optional args" do
86
+ let(:optional_args) { [record_selector, capybara_options] }
87
+
88
+ it "should be the last argument" do
89
+ expect(subject.capybara_options).to eq(capybara_options)
90
+ end
91
+ end
92
+ end
93
+ end
@@ -1,3 +1,5 @@
1
1
  require 'capybara'
2
2
  require 'rspec/example_steps'
3
3
  require 'napybara'
4
+ require 'verified_double'
5
+ require 'verified_double/rspec_configuration'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: napybara
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Mendoza
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-07 00:00:00.000000000 Z
11
+ date: 2014-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -124,6 +124,20 @@ dependencies:
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 2.14.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 2.14.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec-example_steps
127
141
  requirement: !ruby/object:Gem::Requirement
128
142
  requirements:
129
143
  - - ">="
@@ -137,7 +151,7 @@ dependencies:
137
151
  - !ruby/object:Gem::Version
138
152
  version: '0'
139
153
  - !ruby/object:Gem::Dependency
140
- name: rspec-example_steps
154
+ name: rubocop
141
155
  requirement: !ruby/object:Gem::Requirement
142
156
  requirements:
143
157
  - - ">="
@@ -151,7 +165,7 @@ dependencies:
151
165
  - !ruby/object:Gem::Version
152
166
  version: '0'
153
167
  - !ruby/object:Gem::Dependency
154
- name: rubocop
168
+ name: sinatra
155
169
  requirement: !ruby/object:Gem::Requirement
156
170
  requirements:
157
171
  - - ">="
@@ -165,7 +179,7 @@ dependencies:
165
179
  - !ruby/object:Gem::Version
166
180
  version: '0'
167
181
  - !ruby/object:Gem::Dependency
168
- name: sinatra
182
+ name: verified_double
169
183
  requirement: !ruby/object:Gem::Requirement
170
184
  requirements:
171
185
  - - ">="
@@ -199,6 +213,7 @@ files:
199
213
  - lib/napybara.rb
200
214
  - lib/napybara/element.rb
201
215
  - lib/napybara/element_array.rb
216
+ - lib/napybara/finder_methods_appender.rb
202
217
  - lib/napybara/selector.rb
203
218
  - lib/napybara/version.rb
204
219
  - napybara.gemspec
@@ -210,6 +225,7 @@ files:
210
225
  - spec/integration/napybara_spec.rb
211
226
  - spec/integration/readme_spec.rb
212
227
  - spec/napybara/element_spec.rb
228
+ - spec/napybara/finder_methods_appender_spec.rb
213
229
  - spec/spec_helper.rb
214
230
  homepage: https://github.com/gsmendoza/napybara
215
231
  licenses:
@@ -244,4 +260,5 @@ test_files:
244
260
  - spec/integration/napybara_spec.rb
245
261
  - spec/integration/readme_spec.rb
246
262
  - spec/napybara/element_spec.rb
263
+ - spec/napybara/finder_methods_appender_spec.rb
247
264
  - spec/spec_helper.rb