pouch 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,11 +3,14 @@ module Pouch
3
3
 
4
4
  def element tag, name, identifier, *args, &block
5
5
  define_method name do
6
+ timer { browser.element(tag, identifier).visible? }
6
7
  return browser.element tag, identifier unless block_given?
7
8
  block.call browser.send(:element, tag, identifier), *args
8
9
  end
9
- end
10
10
 
11
+ define_waiting_methods tag, name, identifier, *args, &block
12
+ end
13
+
11
14
  def button name, identifier, *args, &block
12
15
  element :button, name, identifier, args, &block
13
16
  end
@@ -115,5 +118,49 @@ module Pouch
115
118
  element :ul, name, identifier, args, &block
116
119
  end
117
120
 
121
+ private
122
+
123
+ def define_waiting_methods tag, name, identifier, *args, &block
124
+ define_method "when_#{name}_not_visible" do
125
+ result = negative_timer { !browser.element(tag, identifier).visible? }
126
+ unless result
127
+ raise VisibilityError, "#{name} element is still visible"
128
+ end
129
+ self
130
+ end
131
+
132
+ define_method "when_#{name}_present" do
133
+ timer { browser.element(tag, identifier).present? }
134
+ html_element = browser.element tag, identifier
135
+ return located_element unless block_given?
136
+ block.call located_element, *args
137
+ end
138
+
139
+ define_method "when_#{name}_not_present" do
140
+ result = negative_timer { !browser.element(tag, identifier).present? }
141
+ unless result
142
+ raise PresenceError, "#{name} element is still present"
143
+ end
144
+ self
145
+ end
146
+ end
147
+
148
+ def timer &block
149
+ timed_out = Time.now + timeout
150
+ until Time.now > timed_out
151
+ result = yield block rescue false
152
+ break if result
153
+ end
154
+ end
155
+
156
+ def negative_timer &block
157
+ timed_out = Time.now + timeout
158
+ until Time.now > timed_out
159
+ result = yield_block
160
+ return result if result
161
+ end
162
+ false
163
+ end
164
+
118
165
  end
119
166
  end
data/lib/pouch/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pouch
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/pouch.rb CHANGED
@@ -4,7 +4,10 @@ require "pouch/elements"
4
4
  module Pouch
5
5
  include Elements
6
6
 
7
- class ContextualReplacementError < StandardError; end
7
+ class ContextArgumentError < StandardError; end
8
+ class ReplacementError < StandardError; end
9
+ class VisibilityError < StandardError; end
10
+ class PresenceError < StandardError; end
8
11
 
9
12
  def self.included base
10
13
  base.extend self
@@ -18,7 +21,11 @@ module Pouch
18
21
 
19
22
  @browser = browser
20
23
  @context = standardize opts[:context] if opts[:context]
24
+ @timeout = opts[:timeout] || 10
25
+
26
+ set_timer if self.respond_to? :set_timer
21
27
  visit if self.respond_to?(:visit) && start
28
+
22
29
  contextualize_methods
23
30
  end
24
31
 
@@ -30,6 +37,10 @@ module Pouch
30
37
  @context
31
38
  end
32
39
 
40
+ def timeout
41
+ @timeout
42
+ end
43
+
33
44
  def page_url= str
34
45
  define_method :visit do
35
46
  self.browser.goto str
@@ -53,7 +64,7 @@ module Pouch
53
64
  def get_match context
54
65
  methods.select{ |mthd| mthd.to_s.start_with? "#{context}_" }.each_with_object([]) do |mthd, array|
55
66
  unless respond_to? mthd.to_s.gsub("#{context}_", "")
56
- raise ContextualReplacementError, "#{self.class} defined no standard method for replacement '#{mthd}'"
67
+ raise ReplacementError, "#{self.class} defined no standard method for replacement '#{mthd}'"
57
68
  end
58
69
  array << mthd
59
70
  end
@@ -69,7 +80,7 @@ module Pouch
69
80
  if [Array, String, Symbol].include? context.class
70
81
  [context].flatten.map(&:to_s)
71
82
  else
72
- raise "Cannot define Pouch context as #{context.class}"
83
+ raise ContextArgumentError, "cannot define Pouch context with #{context.class}"
73
84
  end
74
85
  end
75
86
 
@@ -12,53 +12,93 @@ describe Pouch::Elements do
12
12
  element(:a, :what_replacement, id: 'no-match')
13
13
  end
14
14
 
15
- let(:browser){ double 'webdriver' }
16
- let(:element){ double 'html_link' }
15
+ let(:browser){ double 'webdriver', :element => element }
16
+ let(:element){ double 'html_link', :visible? => true }
17
17
  let(:page){ Page.new browser }
18
18
  let(:diff){ Page.new browser, context: 'different' }
19
19
  let(:what){ Page.new browser, context: 'what' }
20
20
 
21
21
  describe "#element" do
22
22
  it "returns the link element by default" do
23
- expect(browser).to receive(:element).with(:a, id:'generic').and_return(element)
24
23
  expect(page.generic).to eq element
25
24
  end
26
25
 
27
- it "returns an element that can be clicked" do
28
- expect(browser).to receive(:element).with(:a, id:'generic').and_return(element)
29
- expect(element).to receive(:click)
30
- page.generic.click
31
- end
32
-
33
26
  it "returns the result of the definition block" do
34
- expect(browser).to receive(:element).with(:a, id:'blocked').and_return(element)
35
27
  expect(element).to receive(:href).and_return("www.test.com")
36
28
  expect(page.blocked).to eq "www.test.com"
37
29
  end
38
30
 
31
+ it "calls #timer to wait for visible element" do
32
+ expect(page).to receive(:timer)
33
+ page.generic
34
+ end
35
+
36
+ it "generates #when_generic_not_visible" do
37
+ expect(page).to respond_to :when_generic_not_visible
38
+ end
39
+
40
+ it "generates #when_generic_present method" do
41
+ expect(page).to respond_to :when_generic_present
42
+ end
43
+
44
+ it "generates #when_generic_not_present method" do
45
+ expect(page).to respond_to :when_generic_not_present
46
+ end
47
+
39
48
  context "with context" do
40
49
  it "uses the replacement method" do
41
- expect(browser).to receive(:element).with(:a, id:'generic-diff').and_return(element)
50
+ expect(browser).to receive(:element).twice.with(:a, id:'generic-diff')
42
51
  expect(diff.generic).to eq element
43
52
  end
44
53
 
45
54
  it "uses the replacement method with definition block" do
46
- expect(browser).to receive(:element).with(:a, id:'blocked-diff').and_return(element)
47
55
  expect(element).to receive(:href).and_return("www.diff.com")
48
56
  expect(diff.blocked).to eq "www.diff.com"
49
57
  end
50
58
 
51
59
  it "uses the standard method when context matches no replacement" do
52
- expect(browser).to receive(:element).with(:a, id:'generic').and_return(element)
60
+ expect(browser).to receive(:element).twice.with(:a, id:'generic')
53
61
  expect(Page.new(browser, context: 'test').generic).to eq element
54
62
  end
55
63
 
56
64
  it "throws an error with replacement but no standard method" do
57
- expect{ what.replacement }.to raise_error Pouch::ContextualReplacementError, /Page defined no standard method for replacement/
65
+ expect{ what.replacement }.to raise_error Pouch::ReplacementError, /Page defined no standard method for replacement/
58
66
  end
59
67
  end
60
68
  end
61
69
 
70
+ describe "#when_element_not_visible" do
71
+ Page.send(:link, :invisible, id:'hidden')
72
+
73
+ let(:page){ Page.new 'webdriver' }
74
+
75
+ it "returns the page object instance if valid" do
76
+ expect(page).to receive(:negative_timer).and_return(true)
77
+ expect(page.when_invisible_not_visible).to eq page
78
+ end
79
+
80
+ it "raises an VisibilityError if invalid" do
81
+ expect(page).to receive(:negative_timer).and_return(false)
82
+ expect{ page.when_invisible_not_visible }.to raise_error Pouch::VisibilityError, /invisible element/
83
+ end
84
+ end
85
+
86
+ describe "#when_element_not_present" do
87
+ Page.send(:link, :gone, id:'not-here')
88
+
89
+ let(:page){ Page.new 'webdriver' }
90
+
91
+ it "returns the page object instance if valid" do
92
+ expect(page).to receive(:negative_timer).and_return(true)
93
+ expect(page.when_gone_not_present).to eq page
94
+ end
95
+
96
+ it "raises a PresenceError if invalid" do
97
+ expect(page).to receive(:negative_timer).and_return(false)
98
+ expect{ page.when_gone_not_present }.to raise_error Pouch::PresenceError, /gone element/
99
+ end
100
+ end
101
+
62
102
  describe "#button" do
63
103
  it "calls :element with :button tag" do
64
104
  expect(page).to receive(:element).with(:button, :button_name, {id: 'id'}, [])
@@ -67,7 +107,7 @@ describe Pouch::Elements do
67
107
  end
68
108
 
69
109
  describe "#checkbox" do
70
- it "calls :element with :checkbox tag" do
110
+ it "calls :element with :input tag" do
71
111
  expect(page).to receive(:element).with(:input, :checkbox_name, {id: 'id', type: 'checkbox'}, [])
72
112
  page.send(:checkbox, :checkbox_name, id: 'id')
73
113
  end
@@ -81,7 +121,7 @@ describe Pouch::Elements do
81
121
  end
82
122
 
83
123
  describe "#file_field" do
84
- it "calls :element with :file_field tag" do
124
+ it "calls :element with :input tag" do
85
125
  expect(page).to receive(:element).with(:input, :ff_name, {id: 'id', type: 'file'}, [])
86
126
  page.send(:file_field, :ff_name, id: 'id')
87
127
  end
@@ -172,7 +212,7 @@ describe Pouch::Elements do
172
212
  end
173
213
 
174
214
  describe "#radio_button" do
175
- it "calls :element with :radio tag" do
215
+ it "calls :element with :input tag" do
176
216
  expect(page).to receive(:element).with(:input, :radio_name, {id: 'id', type: 'radio'}, [])
177
217
  page.send(:radio_button, :radio_name, id: 'id')
178
218
  end
@@ -221,7 +261,7 @@ describe Pouch::Elements do
221
261
  end
222
262
 
223
263
  describe "#text_area" do
224
- it "calls :element with :input tag" do
264
+ it "calls :element with :textarea tag" do
225
265
  expect(page).to receive(:element).with(:textarea, :text_area_name, {id: 'id'}, [])
226
266
  page.send(:text_area, :text_area_name, id: 'id')
227
267
  end
data/spec/pouch_spec.rb CHANGED
@@ -18,6 +18,18 @@ describe Pouch do
18
18
  obj = Page2.new 'webdriver'
19
19
  expect(obj.instance_variable_get :@browser).to eq 'webdriver'
20
20
  end
21
+
22
+ it "creates @timeout variable" do
23
+ obj = Page2.new 'webdriver'
24
+ expect(obj.instance_variable_get :@timeout).to eq 10
25
+ end
26
+
27
+ context "with :timeout option" do
28
+ it "creates @timeout variable" do
29
+ obj = Page2.new 'webdriver', timeout: 5
30
+ expect(obj.instance_variable_get :@timeout).to eq 5
31
+ end
32
+ end
21
33
 
22
34
  context "when instance does not respond to #visit" do
23
35
  it "doesn't navigate to page_url if start=true" do
@@ -86,11 +98,11 @@ describe Pouch do
86
98
  end
87
99
 
88
100
  it "raises StandardError with Hash argument" do
89
- expect{ obj.call({}) }.to raise_error(StandardError, /Hash/)
101
+ expect{ obj.call({}) }.to raise_error(Pouch::ContextArgumentError, /Hash/)
90
102
  end
91
103
 
92
104
  it "raises StandardError if one argument is a Number" do
93
- expect{ obj.call(['one', 2]) }.to raise_error(StandardError, /Fixnum/)
105
+ expect{ obj.call(['one', 2]) }.to raise_error(Pouch::ContextArgumentError, /Fixnum/)
94
106
  end
95
107
  end
96
108
  end
@@ -108,5 +120,21 @@ describe Pouch do
108
120
  expect(obj.context).to eq ['one', 'two', 'three']
109
121
  end
110
122
  end
123
+
124
+ describe "#timeout" do
125
+ context "without setting a custom timeout interval" do
126
+ it "returns the page object default timeout" do
127
+ obj = Page.new 'webdriver'
128
+ expect(obj.timeout).to eq 10
129
+ end
130
+ end
131
+
132
+ context "after passing :timeout option to #initialize" do
133
+ it "returns the custom timeout" do
134
+ obj = Page.new 'webdriver', timeout: 5
135
+ expect(obj.timeout).to eq 5
136
+ end
137
+ end
138
+ end
111
139
 
112
140
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pouch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-27 00:00:00.000000000 Z
12
+ date: 2014-11-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: watir-webdriver