test-page 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -57,12 +57,14 @@ module Test
57
57
  #
58
58
  # @return [Object] if element is specified for {#initialize}.
59
59
  # @return [Object] otherwise {.element} block is evaluated once per {Page} instance and its value will be returned.
60
+ # @raise [NoBrowserSetException] if {.element} has been set via block and browser has not been set.
60
61
  def element
61
62
  @setup_done ||= begin
62
63
  setup if respond_to?(:setup)
63
64
  true
64
65
  end
65
66
  @element ||= begin
67
+ raise_no_browser_set_exception unless browser
66
68
  element_proc = self.class.element_block
67
69
  element_proc && instance_eval(&element_proc)
68
70
  end
@@ -133,9 +135,14 @@ module Test
133
135
  # Proxies every method call not found on {Page} to element instance.
134
136
  # Subsequent executions of the same method will be invoked on the {Page} object directly.
135
137
  def method_missing(name, *args)
136
- if element.respond_to?(name)
138
+ begin
139
+ el = element
140
+ rescue SystemStackError
141
+ raise_invalid_element_definition
142
+ end
143
+ if el.respond_to?(name)
137
144
  self.class.send :define_method, name do |*args|
138
- element.send(name, *args) {yield}
145
+ el.send(name, *args) {yield}
139
146
  end
140
147
  self.send(name, *args) {yield}
141
148
  else
@@ -151,6 +158,31 @@ module Test
151
158
  end
152
159
  page_with_browser ? page_with_browser.browser : nil
153
160
  end
154
-
161
+
162
+ def raise_no_browser_set_exception
163
+ raise NoBrowserSetException.new %q[No browser has been set to the page!
164
+
165
+ Set it to the class directly:
166
+ Test::Page.browser = browser_instance
167
+
168
+ Or set it to the instance of page:
169
+ page = MyPage.new
170
+ page.browser = browser_instance]
171
+ end
172
+
173
+ def raise_invalid_element_definition
174
+ raise InvalidElementDefinition.new %q[Element defined via block cannot be evaluated, because it is causing SystemStackError.
175
+
176
+ This is usually caused by the fact that the browser instance is not used to search that element.
177
+
178
+ For example, this is not a correct way to define an element:
179
+ element { div(:id => "something") }
180
+
181
+ Correct way would be like this:
182
+ element { browser.div(:id => "something") }]
183
+ end
184
+
185
+ NoBrowserSetException = Class.new(RuntimeError)
186
+ InvalidElementDefinition = Class.new(RuntimeError)
155
187
  end
156
188
  end
@@ -1,5 +1,5 @@
1
1
  module Test
2
2
  class Page
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -3,10 +3,9 @@ require File.expand_path("../../lib/test/page", File.dirname(__FILE__))
3
3
 
4
4
  describe Test::Page do
5
5
  let(:page_class) { Class.new(Test::Page) }
6
+ before { Test::Page.browser = nil }
6
7
 
7
8
  context ".browser" do
8
- before { Test::Page.browser = nil }
9
-
10
9
  it "sets the browser object for page" do
11
10
  Test::Page.browser = "my browser"
12
11
  Test::Page.browser.should == "my browser"
@@ -32,6 +31,8 @@ describe Test::Page do
32
31
  end
33
32
 
34
33
  context ".element" do
34
+ before { page_class.browser = "foo" }
35
+
35
36
  it "sets the element via block" do
36
37
  page_class.element { "my element" }
37
38
  page = page_class.new
@@ -48,6 +49,7 @@ describe Test::Page do
48
49
 
49
50
  context "#element" do
50
51
  it "evaluates element provided by the block only once per instance" do
52
+ page_class.browser = "foo"
51
53
  block_called = false
52
54
  page_class.element do
53
55
  raise "block should have been called only once!" if block_called
@@ -58,6 +60,23 @@ describe Test::Page do
58
60
  2.times { page.element.should == "my element in block" }
59
61
  block_called.should be_true
60
62
  end
63
+
64
+ it "raises an exception if browser is not set" do
65
+ page_class.element { "whatever" }
66
+
67
+ expect {
68
+ page_class.new.element
69
+ }.to raise_error(Test::Page::NoBrowserSetException)
70
+ end
71
+
72
+ it "raises an exception if element is set via block without using browser" do
73
+ page_class.element { foo_bar }
74
+ page_class.browser = "foo"
75
+
76
+ expect {
77
+ page_class.new.element
78
+ }.to raise_error(Test::Page::InvalidElementDefinition)
79
+ end
61
80
  end
62
81
 
63
82
  context "#setup" do
@@ -121,6 +140,7 @@ describe Test::Page do
121
140
  it "returns the new page instance" do
122
141
  second_page = Class.new(Test::Page)
123
142
  page_class.send(:define_method, :redirect_me) { redirect_to second_page }
143
+ page_class.browser = "foo"
124
144
  page = page_class.new
125
145
  page.redirect_me.should be_an_instance_of(second_page)
126
146
  end
@@ -128,6 +148,7 @@ describe Test::Page do
128
148
  it "reuses the existing page element" do
129
149
  second_page = Class.new(Test::Page)
130
150
  page_class.send(:define_method, :redirect_me) { redirect_to second_page }
151
+ page_class.browser = "foo"
131
152
  page = page_class.new "provided element"
132
153
  redirected_page = page.redirect_me
133
154
  redirected_page.element.should == "provided element"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: test-page
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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: 2012-10-31 00:00:00.000000000 Z
12
+ date: 2012-11-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec