cucumber-salad 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  require 'chronic'
2
2
 
3
3
  require 'cucumber/salad/widget_container'
4
+ require 'cucumber/salad/widget_macros'
4
5
  require 'cucumber/salad/conversions'
5
6
  require 'cucumber/salad/instance_conversions'
6
7
  require 'cucumber/salad/node_text'
@@ -1,5 +1,5 @@
1
1
  module Cucumber
2
2
  module Salad
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
@@ -6,7 +6,7 @@ module Cucumber
6
6
  end
7
7
 
8
8
  def widget(name, options = {})
9
- widget_class(name).in_node(root, options)
9
+ widget_class(name).find_in(root, options)
10
10
  end
11
11
 
12
12
  private
@@ -0,0 +1,84 @@
1
+ module Cucumber
2
+ module Salad
3
+ module WidgetMacros
4
+ # Defines a new action.
5
+ #
6
+ # This is a shortcut to help defining a widget and a method that clicks
7
+ # on that widget. You can then send a widget instance the message given
8
+ # by +name+.
9
+ #
10
+ # @example
11
+ # # Consider the widget will encapsulate the following HTML
12
+ # #
13
+ # # <div id="profile">
14
+ # # <a href="/profiles/1/edit" rel="edit">Edit</a>
15
+ # # </div>
16
+ # class PirateProfile < Salad::Widget
17
+ # root "#profile"
18
+ #
19
+ # # Declare the action
20
+ # action :edit, '[rel = edit]'
21
+ # end
22
+ #
23
+ # # Click the link
24
+ # widget(:pirate_profile).edit
25
+ #
26
+ # @param name the name of the action
27
+ # @param selector the selector for the widget that will be clicked
28
+ def action(name, selector)
29
+ widget name, selector
30
+
31
+ define_method name do
32
+ widget(name).click
33
+
34
+ self
35
+ end
36
+ end
37
+
38
+ # Declares a new sub-widget.
39
+ #
40
+ # Sub-widgets are accessible inside the container widget using the
41
+ # +widget+ message.
42
+ #
43
+ # @param name the name of the sub-widget
44
+ # @param selector the sub-widget selector
45
+ # @param parent [Class] the parent class of the new sub-widget
46
+ #
47
+ # @yield A block allowing you to further customize the widget behavior.
48
+ #
49
+ # @see #widget
50
+ def widget(name, selector, parent = Widgets::Widget, &block)
51
+ type = Class.new(parent) {
52
+ root selector
53
+
54
+ instance_eval(&block) if block
55
+ }
56
+
57
+ const_set(Salad::WidgetName.new(name).to_sym, type)
58
+ end
59
+
60
+ # Creates a delegator for one sub-widget message.
61
+ #
62
+ # Since widgets are accessed through {WidgetContainer#widget}, we can't
63
+ # use {Forwardable} to delegate messages to widgets.
64
+ #
65
+ # @param name the name of the receiver sub-widget
66
+ # @param widget_message the name of the message to be sent to the sub-widget
67
+ # @param method_name the name of the delegator. If +nil+ the method will
68
+ # have the same name as the message it will send.
69
+ def widget_delegator(name, widget_message, method_name = nil)
70
+ method_name = method_name || widget_message
71
+
72
+ class_eval <<-RUBY
73
+ def #{method_name}(*args)
74
+ if args.size == 1
75
+ widget(:#{name}).#{widget_message} args.first
76
+ else
77
+ widget(:#{name}).#{widget_message} *args
78
+ end
79
+ end
80
+ RUBY
81
+ end
82
+ end
83
+ end
84
+ end
@@ -32,10 +32,6 @@ module Cucumber
32
32
  end
33
33
  end
34
34
 
35
- def self.submit(attrs)
36
- new.fill_all(attrs).submit
37
- end
38
-
39
35
  def self.text_field(name, label = nil)
40
36
  define_method "#{name}=" do |val|
41
37
  l = label || name_to_locator(name)
@@ -44,42 +40,28 @@ module Cucumber
44
40
  end
45
41
  end
46
42
 
47
- def initialize(settings = {})
48
- s = settings.dup
49
- data = s.delete(:data) || {}
50
-
51
- super s
52
-
53
- fill_all data
54
-
55
- if block_given?
56
- yield self
57
-
58
- submit
59
- end
60
- end
43
+ action :submit, '[type = submit]'
61
44
 
62
- def fill_all(attrs)
63
- attrs.each do |k, v|
45
+ # Sets the given form attributes.
46
+ #
47
+ # @param attributes [Hash] the attributes and values we want to set.
48
+ #
49
+ # @return the current widget.
50
+ def set(attributes)
51
+ attributes.each do |k, v|
64
52
  send "#{k}=", v
65
53
  end
66
54
 
67
55
  self
68
56
  end
69
57
 
70
- def submit
71
- root.find('[type = "submit"]').click
72
-
73
- self
74
- end
75
-
76
- # Submit form with +attrs+.
58
+ # Submit form with +attributes+.
77
59
  #
78
60
  # @param attrs [Hash] the form fields and their values
79
61
  #
80
- # @return self
81
- def submit_with(attrs)
82
- fill_all attrs
62
+ # @return the current widget
63
+ def submit_with(attributes)
64
+ set attributes
83
65
  submit
84
66
  end
85
67
 
@@ -3,43 +3,39 @@ module Cucumber
3
3
  module Widgets
4
4
  class Widget
5
5
  extend Forwardable
6
+ extend WidgetMacros
6
7
 
7
- include Salad::Conversions
8
8
  include WidgetContainer
9
9
 
10
- def self.action(name, selector, parent = Widget)
11
- widget name, selector, parent
12
-
13
- define_method name do
14
- widget(name).click
15
- end
16
- end
17
-
18
10
  def self.has_instance?(parent_node)
19
11
  parent_node.has_selector?(selector)
20
12
  end
21
13
 
22
- def self.in_node(node, options = {})
14
+ # Finds a single instance of the current widget in +node+.
15
+ #
16
+ # @param node the node we want to search in
17
+ #
18
+ # @return a new instance of the current widget class.
19
+ #
20
+ # @raise [Capybara::ElementNotFoundError] if the widget can't be found
21
+ def self.find_in(node, options = {})
23
22
  new(options.merge(root: node.find(selector)))
24
23
  end
25
24
 
25
+ # Sets this widget's default selector.
26
+ #
27
+ # @param selector [String] a CSS or XPath query
26
28
  def self.root(selector)
27
29
  @selector = selector
28
30
  end
29
31
 
32
+ # @return The selector specified with +root+.
30
33
  def self.selector
31
34
  @selector
32
35
  end
33
36
 
34
- def self.widget(name, selector, parent = Widget, &block)
35
- type = Class.new(parent) {
36
- root selector
37
-
38
- instance_eval(&block) if block
39
- }
40
-
41
- const_set(Salad::WidgetName.new(name).to_sym, type)
42
- end
37
+ # @return The root node of the current widget
38
+ attr_reader :root
43
39
 
44
40
  def_delegators :root, :click
45
41
 
@@ -68,7 +64,7 @@ module Cucumber
68
64
 
69
65
  private
70
66
 
71
- attr_accessor :root
67
+ attr_writer :root
72
68
 
73
69
  def page
74
70
  Capybara.current_session
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: cucumber-salad
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.3.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - David Leal
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-20 00:00:00.000000000 Z
12
+ date: 2013-07-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  version_requirements: !ruby/object:Gem::Requirement
@@ -111,9 +111,9 @@ files:
111
111
  - lib/cucumber/salad/table/void_mapping.rb
112
112
  - lib/cucumber/salad/version.rb
113
113
  - lib/cucumber/salad/widget_container.rb
114
+ - lib/cucumber/salad/widget_macros.rb
114
115
  - lib/cucumber/salad/widget_name.rb
115
116
  - lib/cucumber/salad/widgets.rb
116
- - lib/cucumber/salad/widgets/atom.rb
117
117
  - lib/cucumber/salad/widgets/auto_table.rb
118
118
  - lib/cucumber/salad/widgets/base_table.rb
119
119
  - lib/cucumber/salad/widgets/document.rb
@@ -146,3 +146,4 @@ signing_key:
146
146
  specification_version: 3
147
147
  summary: A set of helpers to ease writing cucumber features
148
148
  test_files: []
149
+ has_rdoc:
@@ -1,19 +0,0 @@
1
- module Cucumber
2
- module Salad
3
- module Widgets
4
- class Atom < Widget
5
- def to_a
6
- [to_s]
7
- end
8
-
9
- def ==(other)
10
- to_s.downcase == other.to_s.downcase
11
- end
12
-
13
- def to_s
14
- node_text(root)
15
- end
16
- end
17
- end
18
- end
19
- end