napybara 0.1.0 → 0.2.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: fa59fce2f9581c8e41e8edc54ee3fa89a0bd02b1
4
- data.tar.gz: a4925b50cec5a311d8be910dfce72056a48146f2
3
+ metadata.gz: 136cf0b7cb6fe430469f8f62716b881f13cbd930
4
+ data.tar.gz: 2eced35383167624d76137d9c55b7ac5ad8cc1b0
5
5
  SHA512:
6
- metadata.gz: 7f4a0a7ec822c42f780342ef824d0c3d59542799b4e8ea16e231cbffd732c541a9212fc36d0c8727d17d5c61a840fd715141504734df98ae44340ab406a9d0dd
7
- data.tar.gz: b444e875a37e7a71056cef7e639e8a87acb23cdff566f587a9fc7d0e092496951b261d5b70b318e51a8fe2e3896afebe97aa82f44125081ff5b3fe52325e8efe
6
+ metadata.gz: 815eda0334f497bb7b085654777577a20f86e49448989c769dd9718436c31c980028b70ab960ac8f4a8078bf972f5e223d51aa2d992692562f82f515eb4a82c0
7
+ data.tar.gz: b0ad69b36d4e99febc8590ea2309940ae5cf4b35c4c7b45208b94d0fea8ec36896b3859a972bc0848d8a68118b06c9088a99aac2df3e53e9b6d4e22acf80a2ac
data/README.md CHANGED
@@ -5,6 +5,10 @@ So you're writing an integration test for the following page:
5
5
  ```html
6
6
  <html>
7
7
  <body>
8
+ <ul class='messages-list'>
9
+ <li class="message">Hello world!</li>
10
+ <li class="message">Kamusta mundo!</li>
11
+ </ul>
8
12
  <form class='new-message'>
9
13
  <div class="message" />
10
14
  <label for='message'>Message</label>
@@ -18,24 +22,28 @@ So you're writing an integration test for the following page:
18
22
  Wouldn't it be nice if your test helpers followed the structure of the page?
19
23
 
20
24
  ```ruby
21
- new_message_page.visit!
22
- new_message_page.form.message.text_field.set 'Hello World!'
23
- new_message_page.form.submit!
25
+ messages_page.visit!
26
+ messages_page.form.message.text_field.set 'Hello World!'
27
+ messages_page.form.submit!
28
+ expect(messages_page.messages[0]).to have_content('Hello world!')
29
+ expect(messages_page.messages[1]).to have_content('Kamusta mundo!')
24
30
  ```
25
31
 
26
- With Napybara, now they can! All you need is to define the structure of the page with Napybara's simply awesome DSL:
32
+ With Napybara, now they can! All you need is to define the structure of the page with Napybara's DSL:
27
33
 
28
34
  ```ruby
29
35
  # spec/features/messages_spec.rb
30
36
 
31
- let(:new_message_page) do
37
+ :new_messsage_page) do
32
38
  Napybara::DSL.build(self) do
33
- form 'form.new-message' do
34
- message '.message' do
35
- text_field 'input#message'
39
+ all :messages, '.messages-list .message'
40
+
41
+ find :form, 'form.new-message' do
42
+ find :message, '.message' do
43
+ find :text_field, 'input#message'
36
44
  end
37
45
 
38
- submit_button 'input[type=submit]'
46
+ find :submit_button, 'input[type=submit]'
39
47
  end
40
48
  end
41
49
  end
@@ -43,19 +51,21 @@ end
43
51
 
44
52
  In the integration test above, the `self` in `Napybara::DSL.build(self)` points to the current test session. In Rails integration tests which include `Capybara::DSL`, `self` would already have access to `Capybara::DSL`'s methods.
45
53
 
46
- What about the custom `new_message_page.visit!` and `form.submit!` methods? With Napybara, you can access each element in the structure through the dsl's `element` method. So if you want to add a `visit!` method to the `new_message_page element`, you can write
54
+ What about the custom `messages_page.visit!` and `form.submit!` methods? With Napybara, you can extend each element in the structure with the dsl's `extend_element` method. So if you want to add a `visit!` method to the `messages_page` element, you can write
47
55
 
48
56
  ```ruby
49
57
  Napybara::DSL.build(self) do
50
- def element.visit!
51
- visit '/messages/new'
58
+ extend_element do
59
+ def visit!
60
+ visit '/messages/index'
61
+ end
52
62
  end
53
63
 
54
64
  # ...
55
65
  end
56
66
  ```
57
67
 
58
- You can also reuse helpers by extending the `element` with a module. For example, with `form.submit!`:
68
+ You can also reuse helpers by calling the `extend_element` with a module. For example, with `form.submit!`:
59
69
 
60
70
  ```ruby
61
71
  module FormExtensions
@@ -65,15 +75,13 @@ module FormExtensions
65
75
  end
66
76
 
67
77
  Napybara::DSL.build(self) do
68
- form 'form.new-message' do
69
- element.extend FormExtensions
78
+ find :form, 'form.new-message' do
79
+ extend_element FormExtensions
70
80
  # ...
71
81
  end
72
82
  end
73
83
  ```
74
84
 
75
- The `element` method just points to the element object that was created, so you can extend the object with the magic of plain Ruby.
76
-
77
85
  ## Installation
78
86
 
79
87
  Add this line to your application's Gemfile:
data/lib/napybara/dsl.rb CHANGED
@@ -8,14 +8,23 @@ module Napybara
8
8
  end
9
9
  end
10
10
 
11
- def find(child_element_name, child_element_selector, &block)
11
+ def all(child_element_name, child_element_selector, &block)
12
12
  element.define_singleton_method(child_element_name) do
13
- Napybara::DSL.build(self.find(child_element_selector), &block)
13
+ self.all(child_element_selector).each do |child_element|
14
+ Napybara::DSL.build(child_element, &block)
15
+ end
14
16
  end
15
17
  end
16
18
 
17
- def method_missing(method, *args, &block)
18
- find(method, args[0], &block)
19
+ def extend_element(a_module = Module.new, &block)
20
+ block_module = Module.new(&block)
21
+ element.extend(block_module, a_module)
22
+ end
23
+
24
+ def find(child_element_name, child_element_selector, &block)
25
+ element.define_singleton_method(child_element_name) do
26
+ Napybara::DSL.build(self.find(child_element_selector), &block)
27
+ end
19
28
  end
20
29
  end
21
30
  end
@@ -1,3 +1,3 @@
1
1
  module Napybara
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -11,13 +11,15 @@ describe Napybara do
11
11
 
12
12
  let(:test_page) do
13
13
  Napybara::DSL.build(session) do
14
- def element.visit!
15
- visit '/test.html'
14
+ extend_element do
15
+ def visit!
16
+ visit '/test.html'
17
+ end
16
18
  end
17
19
 
18
- form 'form' do
19
- notice_updater 'button.update'
20
- notice '.notice'
20
+ find :form, 'form' do
21
+ find :notice_updater, 'button.update'
22
+ find :notice, '.notice'
21
23
  end
22
24
  end
23
25
  end
@@ -4,7 +4,13 @@ describe Napybara::DSL do
4
4
  let(:capybara_page) do
5
5
  Capybara.string <<-HTML
6
6
  <form class='some-form'>
7
- <button class='some-button'></button>
7
+ <button class='some-button'>
8
+ <img />
9
+ </button>
10
+
11
+ <button class='another-button'>
12
+ <img />
13
+ </button>
8
14
  </form>
9
15
  HTML
10
16
  end
@@ -17,7 +23,7 @@ describe Napybara::DSL do
17
23
 
18
24
  it 'adds child elements from the block to the return element' do
19
25
  element = described_class.build(capybara_page) do
20
- form '.some-form'
26
+ find :form, '.some-form'
21
27
  end
22
28
 
23
29
  expect(element.form['class']).to eq('some-form')
@@ -41,4 +47,75 @@ describe Napybara::DSL do
41
47
  expect(dsl.element.form.button['class']).to eq('some-button')
42
48
  end
43
49
  end
50
+
51
+ describe '#all' do
52
+ it 'adds a method to get all the elements matching the selector' do
53
+ dsl = described_class.new(capybara_page)
54
+ dsl.all(:buttons, 'button')
55
+
56
+ expect(dsl.element.buttons).to have(2).elements
57
+ expect(dsl.element.buttons).to be_all do |element|
58
+ element.tag_name == 'button'
59
+ end
60
+ end
61
+
62
+ it 'adds child elements from the block to each element returned' do
63
+ dsl = described_class.new(capybara_page)
64
+ dsl.all(:buttons, 'button') do
65
+ find :img, 'img'
66
+ end
67
+
68
+ expect(dsl.element.buttons).to have(2).elements
69
+ expect(dsl.element.buttons[0].img.tag_name).to eq('img')
70
+ end
71
+ end
72
+
73
+ describe '#extend_element' do
74
+ it 'can accept a module' do
75
+ some_module = Module.new do
76
+ def responds_to_this_method?
77
+ true
78
+ end
79
+ end
80
+
81
+ dsl = described_class.new(capybara_page)
82
+ dsl.extend_element(some_module)
83
+
84
+ expect(dsl.element.responds_to_this_method?).to be_true
85
+ end
86
+
87
+ it 'can accept a block' do
88
+ dsl = described_class.new(capybara_page)
89
+ dsl.extend_element do
90
+ def responds_to_this_method?
91
+ true
92
+ end
93
+ end
94
+
95
+ expect(dsl.element.responds_to_this_method?).to be_true
96
+ end
97
+
98
+ it 'prefers the passed module with the block' do
99
+ some_module = Module.new do
100
+ def responds_to_module_method?
101
+ true
102
+ end
103
+
104
+ def responds_to_block_method?
105
+ false
106
+ end
107
+ end
108
+
109
+ dsl = described_class.new(capybara_page)
110
+
111
+ dsl.extend_element(some_module) do
112
+ def responds_to_block_method?
113
+ true
114
+ end
115
+ end
116
+
117
+ expect(dsl.element.responds_to_module_method?).to be_true
118
+ expect(dsl.element.responds_to_block_method?).to be_true
119
+ end
120
+ end
44
121
  end
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.1.0
4
+ version: 0.2.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-03-16 00:00:00.000000000 Z
11
+ date: 2014-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara