Class: Domino
- Inherits:
-
Object
- Object
- Domino
- Extended by:
- Capybara, Enumerable
- Includes:
- Capybara
- Defined in:
- lib/domino.rb
Overview
To create a basic Domino class, inherit from Domino and define a selector and attributes:
module Dom
class Post < Domino
selector '#posts .post'
attribute :title # selector defaults to .title
attribute :body, '.post-body' # example of selector override
end
end
Now in your integration test you can use some of Domino's methods:
assert_equal 4, Dom::Post.count
refute_nil Dom::Post.find_by_title('First Post')
What makes it really powerful is defining scoped actions:
module Dom
class Post < Domino
def delete
within(id) { 'Delete' }
end
end
end
refute_nil Dom::Post.find_by_title('First Post')
Dom::Post.find_by_title('First Post').delete
assert_nil Dom::Post.find_by_title('First Post')
Defined Under Namespace
Classes: Error
Instance Attribute Summary (collapse)
-
- (Object) node
readonly
Direct access to the capybara node, in case you need anything special.
Class Method Summary (collapse)
-
+ (Object) all
Get an array of all the Dominos.
-
+ (Object) attribute(attribute, selector = nil)
Define an attribute for this Domino.
-
+ (Object) each
Iterate over all the Dominos.
-
+ (Object) selector(s)
Define the selector for this Domino.
Instance Method Summary (collapse)
-
- (Object) attribute(selector)
Get the text of the first dom element matching a selector.
-
- (Object) id
Dom id for this object.
Instance Attribute Details
- (Object) node (readonly)
Direct access to the capybara node, in case you need anything special
40 41 42 |
# File 'lib/domino.rb', line 40 def node @node end |
Class Method Details
+ (Object) all
Get an array of all the Dominos
53 54 55 |
# File 'lib/domino.rb', line 53 def all map{|node| node} end |
+ (Object) attribute(attribute, selector = nil)
Define an attribute for this Domino
module Dom
class Post
attribute :title # defaults to selector '.title'
attribute :body, '.post-body' # use a custom selector
end
end
This will define an attr_reader on the Domino and also a find_by_attribute method:
Dom::Post.all.first.title
Dom::Post.find_by_title("First Post")
Dom::Post.find_by_title(/^First/)
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/domino.rb', line 83 def attribute(attribute, selector = nil) selector ||= %{.#{attribute.to_s}} class_eval %{ def #{attribute} attribute(%{#{selector}}) end def self.find_by_#{attribute}(value) find_by_attribute(%{#{selector}}, value) end } end |
+ (Object) each
Iterate over all the Dominos
46 47 48 49 50 |
# File 'lib/domino.rb', line 46 def each nodes.each do |node| yield new(node) end end |
+ (Object) selector(s)
Define the selector for this Domino
module Dom
class Post
selector '#posts .post'
end
end
64 65 66 |
# File 'lib/domino.rb', line 64 def selector(s) @selector = s end |
Instance Method Details
- (Object) attribute(selector)
Get the text of the first dom element matching a selector
Dom::Post.all.first.attribute('.title')
120 121 122 123 124 |
# File 'lib/domino.rb', line 120 def attribute(selector) @node.find(selector).text rescue Capybara::ElementNotFound nil end |
- (Object) id
Dom id for this object.
127 128 129 |
# File 'lib/domino.rb', line 127 def id @node['id'].nil? ? nil : %{##{@node['id']}} end |