capybara-widgets 0.0.1 → 0.0.2

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: 243b219f6926b422cc9195f4aa9274dc814258e0
4
- data.tar.gz: dc286684ad060709665a68e87debbd164a7d4421
3
+ metadata.gz: 4486c4e504621dc18e76de0e990848aad563d821
4
+ data.tar.gz: 055b95ae49d0a92266c13dc62e3cec6a444392f8
5
5
  SHA512:
6
- metadata.gz: cc2e60040d367d6cd9188d032f2b85fe2359c8398e5ef2e8c1b0cabafde436ae885ede8b79a535933bd6f74cf80080045619df0e1dac5c8d11fa7aa6f141861c
7
- data.tar.gz: b1a19e794f2a2476c306ecba086928408ef149b40f30150196e69a1e86fe50a50208101d439eb745e2a9a3b80157565d448d9cf676ffad8cb3ca06ff6e1a2a4d
6
+ metadata.gz: 00fd373bc52d8be429a29602db3ba2353e6a69e5962d10e926de7abd15efda38f5ddef8fc87e3c83acf8eb5d13472c20e05fcc3a77137adae6a3488e48fa5b97
7
+ data.tar.gz: 50343fac4b224bf14bb6a086ef1673e1ddcdb0d4ba5578270ebee01f1a78d917bc1d1537812f4d230a8cbb19e9b865cd153f7478ba2170f4bce23af8bc094686
@@ -3,7 +3,6 @@ require 'active_support/core_ext/class/attribute'
3
3
 
4
4
  module Capybara
5
5
  module Widgets
6
-
7
6
  class Page < Widget
8
7
 
9
8
  class_attribute :path
@@ -12,8 +11,20 @@ module Capybara
12
11
  super(page)
13
12
  end
14
13
 
14
+ def loaded?
15
+ true
16
+ end
17
+
15
18
  def reload!
16
19
  visit(current_url)
20
+ loaded?
21
+ self
22
+ end
23
+
24
+ def open!
25
+ visit(self.path)
26
+ loaded?
27
+ self
17
28
  end
18
29
  end
19
30
  end
@@ -32,6 +32,14 @@ module Capybara
32
32
  @root
33
33
  end
34
34
 
35
+ def element(*query)
36
+ root.find(*query)
37
+ end
38
+
39
+ def has_element?(*query)
40
+ root.has_selector?(*query)
41
+ end
42
+
35
43
  class << self
36
44
  def component(name, klass, *query)
37
45
  define_method name do
@@ -39,6 +47,12 @@ module Capybara
39
47
  klass.new(component_root)
40
48
  end
41
49
  end
50
+
51
+ def element(name, *query)
52
+ define_method("#{name}!") { root.find(*query).click }
53
+ define_method("#{name}=") { |arg| root.find(*query).set(arg) }
54
+ define_method("has_#{name}?") { root.has_selector?(*query) }
55
+ end
42
56
  end
43
57
 
44
58
  # delegate missing methods to the @root node
@@ -0,0 +1,22 @@
1
+ require 'cucumber'
2
+ require_relative '../helpers/string_helpers'
3
+
4
+ World Capybara::Widgets::StringHelpers
5
+
6
+ Given(/^I am on a (.* page)/) do |page_name|
7
+ within_widget(to_widget_class(page_name)) do |page|
8
+ page.open!
9
+ end
10
+ end
11
+
12
+ When(/^I navigate to a (.* page)/) do |page_name|
13
+ within_widget(to_widget_class(page_name)) do |page|
14
+ page.open!
15
+ end
16
+ end
17
+
18
+ Then(/^I should be on a (.* page)/) do |page_name|
19
+ within_widget(to_widget_class(page_name)) do |page|
20
+ expect(page).to be_opened
21
+ end
22
+ end
@@ -1,61 +1,88 @@
1
- module Capybara::Widgets::Cucumber
1
+ require 'cucumber'
2
+ require_relative '../helpers/string_helpers'
2
3
 
3
- include Capybara::Widgets::StringHelpers
4
+ World Capybara::Widgets::StringHelpers
4
5
 
5
- def within_widget(klass)
6
- raise "#{klass.name} is not a subclass of Widget" unless klass < Capybara::Widgets::Widget
7
- yield klass.new
8
- end
6
+ def within_widget(klass)
7
+ raise "#{klass.name} is not a subclass of Widget" unless klass < Capybara::Widgets::Widget
8
+ yield klass.new
9
+ end
9
10
 
10
- When(/^I click "([^"]*)" in a "([^"]*)"$/) do |action, widget_name|
11
- within_widget(to_widget_class(widget_name)) do |widget|
12
- widget.send(to_widget_action(action, '!'))
13
- end
14
- end
11
+ def resolve_widget(widget_chain)
12
+ elements = widget_chain.split('->')
13
+ widget_klass = to_widget_class(elements[0])
14
+ action_chain = elements[1..-1].map { |e| to_widget_action(e) }
15
15
 
16
- Then(/^the "([^"]*)" should( not)? be displayed$/) do |widget_name, negated|
17
- widget_class = to_widget_class(widget_name)
18
- within_widget(widget_class) do |widget|
19
- if negated
20
- expect(widget).to be_not_displayed, "#{widget_class} should not be displayed"
21
- else
22
- expect(widget).to be_displayed, "#{widget_class} should be displayed"
23
- end
24
- end
25
- end
16
+ top_widget = widget_klass.new
17
+ apply_action_chain(top_widget, action_chain)
18
+ end
26
19
 
27
- When(/^I set "([^"]*)" to "([^"]*)" in a "([^"]*)"$/) do |field_name, value, widget_name|
28
- within_widget(to_widget_class(widget_name)) do |widget|
29
- widget.send(to_widget_action(field_name, '='), value)
30
- end
31
- end
20
+ When(/^I click "([^"]*)" in a "([^"]*)"$/) do |action, widget_path|
21
+ target_widget = resolve_widget(widget_path)
22
+ target_widget.send(to_widget_action(action, '!'))
23
+ end
32
24
 
33
- And(/^I should see the following "([^"]*)" in a "([^"]*)"$/) do |field_name, widget_name, table|
34
- expected_values = table.raw.flatten
35
- within_widget(to_widget_class(widget_name)) do |widget|
36
- actual_values = widget.send(to_widget_action(field_name))
37
- expect(actual_values).to eq(expected_values)
38
- end
25
+ Then(/^the "([^"]*)" should( not)? be displayed$/) do |widget_path, negated|
26
+ target_widget = resolve_widget(widget_path)
27
+ if negated
28
+ expect(target_widget).to be_not_displayed, "#{target_widget} should not be displayed"
29
+ else
30
+ expect(target_widget).to be_displayed, "#{target_widget} should be displayed"
39
31
  end
32
+ end
33
+
34
+ When(/^I set "([^"]*)" to "([^"]*)" in a "([^"]*)"$/) do |field_name, value, widget_path|
35
+ target_widget = resolve_widget(widget_path)
36
+ target_widget.send(to_widget_action(field_name, '='), value)
37
+ end
38
+
39
+ # Example:
40
+ # I open "My Report" from a "Reports page"
41
+ # is mapped to:
42
+ # ReportsPage.new.open!("My Report")
43
+ When(/^I (.*) "([^"]*)" from a "([^"]*)"$/) do |action_name, action_param, widget_path|
44
+ target_widget = resolve_widget(widget_path)
45
+ target_widget.send(to_widget_action(action_name, '!'), action_param)
46
+ end
47
+
48
+
49
+ And(/^I should see the following "([^"]*)" in a "([^"]*)"$/) do |field_name, widget_path, table|
50
+ expected_values = table.raw.flatten
51
+ target_widget = resolve_widget(widget_path)
52
+ actual_values = target_widget.send(to_widget_action(field_name))
53
+ expect(actual_values).to eq(expected_values)
54
+ end
55
+
56
+ # Expectation. Examples:
57
+ # I should see "Do it" action on a "My page"
58
+ # is mapped to: expect(MyPage.new).to have_action("Do it")
59
+ # I should not see "Do it" action in a "My widget"
60
+ # is mapped to: expect(MyWidget.new).to have_no_action("Do it")
61
+ # I should see "Do it" value in a "My page -> page component"
62
+ # is mapped to: expect(MyPage.new.page_component).to have_value("Do it")
63
+ And(/^I should( not)? see "([^"]*)" (.*) (?:in|on) a "([^"]*)"$/) do |negated, content, widget_action, widget_path|
64
+ negation_prefix = (negated && negated.length > 0) ? 'no_' : ''
65
+ target_widget = resolve_widget(widget_path)
66
+ expect(target_widget).to send("have_#{negation_prefix}#{to_widget_action(widget_action)}", content)
67
+ end
40
68
 
41
- When(/^I fill in the following in a "([^"]*)"$/) do |widget_name, table|
42
- fields = table.rows_hash
43
- within_widget(to_widget_class(widget_name)) do |widget|
44
- fields.each do |k,v|
45
- widget.send(to_widget_action(k, '='), v)
46
- end
69
+ When(/^I fill in the following in a "([^"]*)"$/) do |widget_name, table|
70
+ fields = table.rows_hash
71
+ within_widget(to_widget_class(widget_name)) do |widget|
72
+ fields.each do |k, v|
73
+ widget.send(to_widget_action(k, '='), v)
47
74
  end
48
75
  end
76
+ end
49
77
 
50
- And(/^the "([^"]*)" should be "([^"]*)" in a "([^"]*)"$/) do |field_name, expected, widget_name|
51
- within_widget(to_widget_class(widget_name)) do |widget|
52
- action = to_widget_action(field_name)
53
- if widget.respond_to? "has_#{action}?"
54
- expect(widget.send("has_#{action}?", expected)).to be_true
55
- else
56
- actual = widget.send(action)
57
- expect(actual).to eq(expected)
58
- end
78
+ And(/^the "([^"]*)" should be "([^"]*)" in a "([^"]*)"$/) do |field_name, expected, widget_name|
79
+ within_widget(to_widget_class(widget_name)) do |widget|
80
+ action = to_widget_action(field_name)
81
+ if widget.respond_to? "has_#{action}?"
82
+ expect(widget.send("has_#{action}?", expected)).to be_true
83
+ else
84
+ actual = widget.send(action)
85
+ expect(actual).to eq(expected)
59
86
  end
60
87
  end
61
88
  end
@@ -0,0 +1,2 @@
1
+ require_relative 'cucumber/widget_steps'
2
+ require_relative 'cucumber/page_steps'
@@ -1,9 +1,20 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+
1
3
  module Capybara::Widgets::StringHelpers
2
4
  def to_widget_class(name)
3
- name.to_s.gsub(' ','_').classify.constantize
5
+ name.to_s.strip.gsub(' ','_').classify.constantize
4
6
  end
5
7
 
6
8
  def to_widget_action(name, suffix=nil)
7
- "#{name.to_s.downcase.gsub(' ','_')}#{suffix}"
9
+ "#{name.to_s.strip.downcase.gsub(' ','_')}#{suffix}"
10
+ end
11
+
12
+ def apply_action_chain(widget, chain)
13
+ if chain.length > 0
14
+ next_widget = widget.send(chain[0])
15
+ return apply_action_chain(next_widget, chain[1..-1])
16
+ else
17
+ return widget
18
+ end
8
19
  end
9
20
  end
@@ -1,5 +1,5 @@
1
1
  module Capybara
2
2
  module Widgets
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-widgets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitalii Grygoruk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-23 00:00:00.000000000 Z
11
+ date: 2015-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,6 +110,8 @@ files:
110
110
  - lib/capybara/widgets.rb
111
111
  - lib/capybara/widgets/core/page.rb
112
112
  - lib/capybara/widgets/core/widget.rb
113
+ - lib/capybara/widgets/cucumber.rb
114
+ - lib/capybara/widgets/cucumber/page_steps.rb
113
115
  - lib/capybara/widgets/cucumber/widget_steps.rb
114
116
  - lib/capybara/widgets/helpers/string_helpers.rb
115
117
  - lib/capybara/widgets/version.rb