domino 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +61 -0
  3. data/lib/domino.rb +144 -0
  4. metadata +114 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Domino
2
+
3
+ View abstraction for integration testing
4
+
5
+ ## Usage
6
+
7
+ To create a basic Domino class, inherit from Domino and
8
+ define a selector and attributes:
9
+
10
+ module Dom
11
+ class Post < Domino
12
+ selector '#posts .post'
13
+ attribute :title # selector defaults to .title
14
+ attribute :body, '.post-body' # example of selector override
15
+ end
16
+ end
17
+
18
+ Now in your integration test you can use some of Domino's methods:
19
+
20
+ assert_equal 4, Dom::Post.count
21
+ refute_nil Dom::Post.find_by_title('First Post')
22
+
23
+ What makes it really powerful is defining scoped actions:
24
+
25
+ module Dom
26
+ class Post < Domino
27
+ def delete
28
+ within(id) { click_button 'Delete' }
29
+ end
30
+ end
31
+ end
32
+
33
+ refute_nil Dom::Post.find_by_title('First Post')
34
+ Dom::Post.find_by_title('First Post').delete
35
+ assert_nil Dom::Post.find_by_title('First Post')
36
+
37
+ ## Integration with Cucumber
38
+
39
+ Add a features/support/dominos.rb file, in which you define your dominos.
40
+
41
+ Use them in your steps.
42
+
43
+ ## Integration with Test::Unit
44
+
45
+ Include "domino" in your Gemfile if using bundler, or simply
46
+
47
+ require 'domino'
48
+
49
+ If you're not using Bundler.
50
+
51
+ Now, define your Dominos anywhere you want. The easiest place to start is
52
+ in your test\_helper.rb (doesn't have to be inside a Rails test class).
53
+
54
+ ## Example
55
+
56
+ Check out [Domino Example](http://github.com/ngauthier/domino_example) for an
57
+ example of using Test::Unit and Cucumber with Domino.
58
+
59
+ ## Copyright
60
+
61
+ Copyright (c) 2011 Nick Gauthier, released under the MIT license
data/lib/domino.rb ADDED
@@ -0,0 +1,144 @@
1
+ require 'capybara/dsl'
2
+ require 'nokogiri'
3
+ # To create a basic Domino class, inherit from Domino and
4
+ # define a selector and attributes:
5
+ #
6
+ # module Dom
7
+ # class Post < Domino
8
+ # selector '#posts .post'
9
+ # attribute :title # selector defaults to .title
10
+ # attribute :body, '.post-body' # example of selector override
11
+ # end
12
+ # end
13
+ #
14
+ # Now in your integration test you can use some of Domino's methods:
15
+ #
16
+ # assert_equal 4, Dom::Post.count
17
+ # refute_nil Dom::Post.find_by_title('First Post')
18
+ #
19
+ # What makes it really powerful is defining scoped actions:
20
+ #
21
+ # module Dom
22
+ # class Post < Domino
23
+ # def delete
24
+ # within(id) { click_button 'Delete' }
25
+ # end
26
+ # end
27
+ # end
28
+ #
29
+ # refute_nil Dom::Post.find_by_title('First Post')
30
+ # Dom::Post.find_by_title('First Post').delete
31
+ # assert_nil Dom::Post.find_by_title('First Post')
32
+ class Domino
33
+ include Capybara
34
+ extend Capybara
35
+
36
+ # Namespaced Domino::Error
37
+ class Error < StandardError ; end
38
+
39
+ # Direct access to the nokogiri node, in case you need
40
+ # anything special
41
+ attr_reader :node
42
+
43
+ class << self
44
+ include Enumerable
45
+
46
+ # Iterate over all the Dominos
47
+ def each
48
+ nodes.each do |node|
49
+ yield new(node)
50
+ end
51
+ end
52
+
53
+ # Get an array of all the Dominos
54
+ def all
55
+ map{|node| node}
56
+ end
57
+
58
+ # Define the selector for this Domino
59
+ #
60
+ # module Dom
61
+ # class Post
62
+ # selector '#posts .post'
63
+ # end
64
+ # end
65
+ def selector(s)
66
+ @selector = s
67
+ end
68
+
69
+ # Define an attribute for this Domino
70
+ #
71
+ # module Dom
72
+ # class Post
73
+ # attribute :title # defaults to selector '.title'
74
+ # attribute :body, '.post-body' # use a custom selector
75
+ # end
76
+ # end
77
+ #
78
+ # This will define an attr_reader on the Domino
79
+ # and also a find_by_attribute method:
80
+ #
81
+ # Dom::Post.all.first.title
82
+ # Dom::Post.find_by_title("First Post")
83
+ # Dom::Post.find_by_title(/^First/)
84
+ def attribute(attribute, selector = nil)
85
+ selector ||= %{.#{attribute.to_s}}
86
+
87
+ class_eval %{
88
+ def #{attribute}
89
+ attribute(%{#{selector}})
90
+ end
91
+ def self.find_by_#{attribute}(value)
92
+ find_by_attribute(%{#{selector}}, value)
93
+ end
94
+ }
95
+ end
96
+
97
+ private
98
+
99
+ # Return the html of the current page
100
+ def page_body
101
+ Capybara.current_session.body
102
+ end
103
+
104
+ # Return nokogiri nodes for this object
105
+ def nodes
106
+ if @selector.nil?
107
+ raise Domino::Error.new("You must define a selector")
108
+ end
109
+ Nokogiri::HTML(page_body).css(@selector)
110
+ end
111
+
112
+ # Internal method for finding nodes by a selector
113
+ def find_by_attribute(selector, value)
114
+ case value
115
+ when Regexp
116
+ detect{|node| node.attribute(selector) =~ value }
117
+ else
118
+ detect{|node| node.attribute(selector) == value }
119
+ end
120
+ end
121
+ end
122
+
123
+ # Get the text of the first dom element matching a selector
124
+ #
125
+ # Dom::Post.all.first.attribute('.title')
126
+ def attribute(selector)
127
+ if @node.css(selector).size > 0
128
+ @node.css(selector).first.text.strip
129
+ else
130
+ nil
131
+ end
132
+ end
133
+
134
+ # Dom id for this object.
135
+ def id
136
+ @node['id'].nil? ? nil : %{##{@node['id']}}
137
+ end
138
+
139
+ private
140
+ # Takes a Nokogiri node and sets attributes
141
+ def initialize(node)
142
+ @node = node
143
+ end
144
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: domino
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Nick Gauthier
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-04 00:00:00 -05: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
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 0
32
+ - 4
33
+ - 0
34
+ version: 0.4.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: nokogiri
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 1
48
+ - 4
49
+ - 0
50
+ version: 1.4.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: minitest
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :development
66
+ version_requirements: *id003
67
+ description: "\n Make it easier to deal with UI elements by providing an\n interface that decouples your tests from your views.\n "
68
+ email: ngauthier@gmail.com
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files:
74
+ - README.md
75
+ files:
76
+ - lib/domino.rb
77
+ - MIT-LICENSE
78
+ - README.md
79
+ has_rdoc: true
80
+ homepage: http://www.github.com/ngauthier/domino
81
+ licenses:
82
+ - MIT
83
+ post_install_message:
84
+ rdoc_options: []
85
+
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ requirements: []
107
+
108
+ rubyforge_project:
109
+ rubygems_version: 1.4.2
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: View abstraction for integration testing
113
+ test_files: []
114
+