terminus_spec 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/.gitignore +26 -0
  2. data/.rspec +1 -0
  3. data/.rvmrc +2 -0
  4. data/.travis.yml +6 -0
  5. data/Gemfile +7 -0
  6. data/HISTORY.md +54 -0
  7. data/LICENSE +20 -0
  8. data/README.md +13 -0
  9. data/Rakefile +43 -0
  10. data/cucumber.yml +6 -0
  11. data/lib/terminus_spec.rb +200 -0
  12. data/lib/terminus_spec/factory.rb +27 -0
  13. data/lib/terminus_spec/generators.rb +80 -0
  14. data/lib/terminus_spec/locators.rb +23 -0
  15. data/lib/terminus_spec/logger.rb +7 -0
  16. data/lib/terminus_spec/matchers.rb +41 -0
  17. data/lib/terminus_spec/platform_selenium.rb +18 -0
  18. data/lib/terminus_spec/platform_selenium/platform_object.rb +214 -0
  19. data/lib/terminus_spec/platform_selenium/web_objects/all.rb +98 -0
  20. data/lib/terminus_spec/platform_selenium/web_objects/button.rb +13 -0
  21. data/lib/terminus_spec/platform_selenium/web_objects/link.rb +13 -0
  22. data/lib/terminus_spec/platform_selenium/web_objects/text_field.rb +14 -0
  23. data/lib/terminus_spec/platform_watir.rb +18 -0
  24. data/lib/terminus_spec/platform_watir/platform_object.rb +190 -0
  25. data/lib/terminus_spec/platform_watir/web_objects/all.rb +88 -0
  26. data/lib/terminus_spec/platform_watir/web_objects/text_field.rb +13 -0
  27. data/lib/terminus_spec/platforms.rb +25 -0
  28. data/lib/terminus_spec/version.rb +3 -0
  29. data/lib/terminus_spec/web_objects/all.rb +172 -0
  30. data/lib/terminus_spec/web_objects/button.rb +32 -0
  31. data/lib/terminus_spec/web_objects/link.rb +40 -0
  32. data/lib/terminus_spec/web_objects/text_field.rb +45 -0
  33. data/spec/spec_helper.rb +37 -0
  34. data/spec/terminus_spec/factory_spec.rb +40 -0
  35. data/spec/terminus_spec/generators_spec.rb +179 -0
  36. data/spec/terminus_spec/locators_spec.rb +30 -0
  37. data/spec/terminus_spec/platform_selenium_spec.rb +28 -0
  38. data/spec/terminus_spec/platform_watir_spec.rb +32 -0
  39. data/spec/terminus_spec/platforms_spec.rb +43 -0
  40. data/spec/terminus_spec/terminus_spec.rb +271 -0
  41. data/spec/terminus_spec/web_objects/all_spec.rb +87 -0
  42. data/spec/terminus_spec/web_objects/button_spec.rb +35 -0
  43. data/spec/terminus_spec/web_objects/link_spec.rb +46 -0
  44. data/spec/terminus_spec/web_objects/text_field_spec.rb +48 -0
  45. data/spec/terminus_spec/webobject_selenium_spec.rb +154 -0
  46. data/spec/terminus_spec/webobject_watir_spec.rb +120 -0
  47. data/specs/app/favicon.ico +0 -0
  48. data/specs/app/images/mass_extinction.jpg +0 -0
  49. data/specs/app/index.html +20 -0
  50. data/specs/app/modal_1.html +41 -0
  51. data/specs/app/modal_2.html +29 -0
  52. data/specs/app/server.rb +25 -0
  53. data/specs/app/style.css +18 -0
  54. data/specs/app/success_1.html +12 -0
  55. data/specs/app/success_2.html +12 -0
  56. data/specs/app/test_event.html +39 -0
  57. data/specs/app/test_form.html +114 -0
  58. data/specs/app/test_frame.html +15 -0
  59. data/specs/app/test_iframe.html +15 -0
  60. data/specs/app/test_static.html +92 -0
  61. data/specs/engine/borg.rb +27 -0
  62. data/specs/engine/hooks.rb +23 -0
  63. data/terminus_spec.gemspec +31 -0
  64. metadata +198 -0
@@ -0,0 +1,23 @@
1
+ module TerminusSpec
2
+ module Locators
3
+
4
+ # Returns a link object.
5
+ # @param [Hash] locator how the link can be found
6
+ def link_web_object(locator)
7
+ @platform.get_link_for(locator.clone)
8
+ end
9
+
10
+ # Returns a text field object.
11
+ # @param [Hash] locator how the link can be found
12
+ def text_field_web_object(locator)
13
+ @platform.get_text_field_for(locator.clone)
14
+ end
15
+
16
+ # Returns a button object.
17
+ # @param [Hash] locator how the button can be found
18
+ def button_web_object(locator)
19
+ @platform.get_button_for(locator.clone)
20
+ end
21
+
22
+ end # module Locators
23
+ end # module TerminusSpec
@@ -0,0 +1,7 @@
1
+ module TerminusSpec
2
+
3
+ def self.trace(message, level=2)
4
+ puts("*" * level + " #{message}") if ENV['TRACE'] == 'on'
5
+ end
6
+
7
+ end # module TerminusSpec
@@ -0,0 +1,41 @@
1
+ module TerminusSpec
2
+ module Matchers
3
+
4
+ RSpec::Matchers.define :have_title do |text|
5
+ match do |the_page|
6
+ the_page =~ Regexp.new(text)
7
+ end
8
+
9
+ failure_message_for_should do |actual|
10
+ "Expected that '#{text}' would be the title of the page; found '#{actual}' instead."
11
+ end
12
+
13
+ failure_message_for_should_not do |actual|
14
+ "Expected that '#{text}' would not be the title of the page; it was, however."
15
+ end
16
+
17
+ description do
18
+ "checking for a title of #{text}"
19
+ end
20
+ end
21
+
22
+ RSpec::Matchers.define :contain do |text|
23
+ match do |the_page|
24
+ the_page =~ Regexp.new(text)
25
+ end
26
+
27
+ failure_message_for_should do |actual|
28
+ "Expected that '#{text}' would be in the page content; it was not found."
29
+ end
30
+
31
+ failure_message_for_should_not do |actual|
32
+ "Expected that '#{text}' would not be in the page content; it was, however."
33
+ end
34
+
35
+ description do
36
+ "checking for #{text}"
37
+ end
38
+ end
39
+
40
+ end # module Matchers
41
+ end # module TerminusSpec
@@ -0,0 +1,18 @@
1
+ module TerminusSpec
2
+ module Platforms
3
+ module SeleniumWebDriver
4
+
5
+ def self.create_platform_object_for(browser)
6
+ require 'terminus_spec/platform_selenium/platform_object'
7
+ return SeleniumWebDriver::PlatformObject.new(browser)
8
+ end
9
+
10
+ def self.works_for?(browser)
11
+ browser.is_a?(Selenium::WebDriver::Driver)
12
+ end
13
+
14
+ end # module SeleniumWebDriver
15
+ end # module Platforms
16
+ end # module TerminusSpec
17
+
18
+ TerminusSpec::Platforms.associate(:selenium_webdriver, TerminusSpec::Platforms::SeleniumWebDriver)
@@ -0,0 +1,214 @@
1
+ module TerminusSpec
2
+ module Platforms
3
+ module SeleniumWebDriver
4
+
5
+ class PlatformObject
6
+
7
+ attr_reader :browser
8
+
9
+ def initialize(browser)
10
+ @browser = browser
11
+ end
12
+
13
+ # Platform method to navigate to a specified URL.
14
+ # See TerminusSpec#navigate_to
15
+ def navigate_to(url)
16
+ @browser.navigate.to url
17
+ end
18
+
19
+ # Platform method to retrieve the title for the current page.
20
+ # See TerminusSpec#title
21
+ def title
22
+ @browser.title
23
+ end
24
+
25
+ # Platform method to retrieve text from the current page.
26
+ # See TerminusSpec#text
27
+ def text
28
+ @browser.find_element(:tag_name, 'body').text
29
+ end
30
+
31
+ # Platform method to retrieve the HTML markup for the current page.
32
+ # See TerminusSpec#html
33
+ def html
34
+ @browser.page_source
35
+ end
36
+
37
+ # Platform method to get the current URL.
38
+ # See TerminusSpec#current_url
39
+ def current_url
40
+ @browser.current_url
41
+ end
42
+
43
+ # Platform method to refresh the page.
44
+ # See TerminusSpec#refresh
45
+ def refresh
46
+ @browser.navigate.refresh
47
+ end
48
+
49
+ # Platform method to go to the preceding page in history.
50
+ # See TerminusSpec#back
51
+ def back
52
+ @browser.navigate.back
53
+ end
54
+
55
+ # Platform method to go to the succeeding page in history.
56
+ # See TerminusSpec#forward
57
+ def forward
58
+ @browser.navigate.forward
59
+ end
60
+
61
+ # Platform method to clear the cookies from the browser.
62
+ # See TerminusSpec#clear_cookies
63
+ def clear_cookies
64
+ @browser.manage.delete_all_cookies
65
+ end
66
+
67
+ # Platform method to save the current screenshot to a file.
68
+ # See TerminusSpec#save_screenshot
69
+ def save_screenshot(file_name)
70
+ @browser.save_screenshot(file_name)
71
+ end
72
+
73
+ # Platform method to wait for a condition on a page to be true.
74
+ # See TerminusSpec#wait_until
75
+ def wait_until(timeout, message, &block)
76
+ wait = Selenium::WebDriver::Wait.new({:timeout=>timeout, :message=>message})
77
+ wait.until(&block)
78
+ end
79
+
80
+ # Platform method to handle an alert popup message box.
81
+ # See TerminusSpec#alert
82
+ def will_alert(&block)
83
+ @browser.execute_script "window.alert = function(msg) { window.__lastWatirAlert = msg; }"
84
+ yield
85
+ value = @browser.execute_script "return window.__lastWatirAlert"
86
+ value
87
+ end
88
+
89
+ # Platform method to handle an confirmation popup message box.
90
+ # See TerminusSpec#will_confirm
91
+ def will_confirm(response, &block)
92
+ @browser.execute_script "window.confirm = function(msg) { window.__lastWatirConfirm=msg; return #{!!response} }"
93
+ yield
94
+ value = @browser.execute_script "return window.__lastWatirConfirm"
95
+ value
96
+ end
97
+
98
+ # Platform method to handle a prompt popup message box.
99
+ # See TerminusSpec#will_prompt
100
+ def will_prompt(response, &block)
101
+ @browser.execute_script "window.prompt = function(text, value) { window.__lastWatirPrompt = { message: text, default_value: value }; return #{response.to_json}; }"
102
+ yield
103
+ result = @browser.execute_script "return window.__lastWatirPrompt"
104
+ result && result.dup.each_key { |k| result[k.to_sym] = result.delete(k) }
105
+ result
106
+ end
107
+
108
+ # Platform method to attach to a displayed window.
109
+ # See TerminusSpec#attach_to_window
110
+ def attach_to_window(identifier, &block)
111
+ value = identifier.values.first
112
+ key = identifier.keys.first
113
+ handles = @browser.window_handles
114
+ handles.each do |handle|
115
+ @browser.switch_to.window handle
116
+ if (key == :title and value == @browser.title) or (key == :url and value == @browser.current_url)
117
+ return @browser.switch_to.window handle, &block
118
+ end
119
+ end
120
+ end
121
+
122
+ # Platform method to return a link object.
123
+ # Link objects are of type: TerminusSpec::WebObjects::Link
124
+ # See TerminusSpec::Generators#link
125
+ def get_link_for(locator)
126
+ key, value = get_platform_locator_for(locator, WebObjects::Link, 'a')
127
+ web_object = @browser.find_element(key, value)
128
+ WebObjects::Link.new(web_object, :platform => :selenium_webdriver)
129
+ end
130
+
131
+ # Platform method to click a link object.
132
+ # See TerminusSpec::Generators#link
133
+ def click_link_for(locator)
134
+ key, value = get_platform_locator_for(locator, WebObjects::Link, 'a')
135
+ @browser.find_element(key, value).click
136
+ end
137
+
138
+ # Platform method to return a text field object.
139
+ # Text field objects are of type: TerminusSpec::WebObjects::TextField
140
+ # See TerminusSpec::Generators#text_field
141
+ def get_text_field_for(locator)
142
+ key, value = get_platform_locator_for(locator, WebObjects::TextField, 'input', :type => 'text')
143
+ web_object = @browser.find_element(key, value)
144
+ WebObjects::TextField.new(web_object, :platform => :selenium_webdriver)
145
+ end
146
+
147
+ # Platform method to get the value in a text field object.
148
+ # See TerminusSpec::Generators#text_field
149
+ def get_text_field_value_for(locator)
150
+ key, value = get_platform_locator_for(locator, WebObjects::TextField, 'input', :type => 'text')
151
+ value = @browser.find_element(key, value).attribute('value')
152
+ value
153
+ end
154
+
155
+ # Platform method to set a value in a text field object.
156
+ # See TerminusSpec::Generators#text_field
157
+ def set_text_field_value_for(locator, text)
158
+ key, value = get_platform_locator_for(locator, WebObjects::TextField, 'input', :type => 'text')
159
+ @browser.find_element(key, value).clear
160
+ @browser.find_element(key, value).send_keys(text)
161
+ end
162
+
163
+ # Platform method to return a button object.
164
+ # Button objects are of type: TerminusSpec::WebObjects::Button
165
+ # See TerminusSpec::Generators#button
166
+ def get_button_for(locator)
167
+ key, value = get_platform_locator_for(locator, WebObjects::Button, 'input', :type => 'submit')
168
+ web_object = @browser.find_element(key, value)
169
+ WebObjects::Button.new(web_object, :platform => :selenium_webdriver)
170
+ end
171
+
172
+ # Platform method to click a button object.
173
+ # See TerminusSpec::Generators#button
174
+ def click_button_for(locator)
175
+ key, value = get_platform_locator_for(locator, WebObjects::Button, 'input', :type => 'submit')
176
+ @browser.find_element(key, value).click
177
+ end
178
+
179
+ protected
180
+
181
+ def get_platform_locator_for(locator, web_object, tag=nil, qualifier=nil)
182
+ locator = qualify_with_tagname_for locator, tag, qualifier if tag
183
+ key, value = web_object.have_selenium_find_object_with locator
184
+ return key, value
185
+ end
186
+
187
+ def qualify_with_tagname_for(locator, tag, qualifier=nil)
188
+ return locator if locator.length < 2 and support_is_provided_for locator, tag, qualifier
189
+ locator[:tag_name] = tag
190
+ if qualifier
191
+ qualifier.each do |key, value|
192
+ locator[key] = value
193
+ end
194
+ end
195
+ locator
196
+ end
197
+
198
+ def support_is_provided_for(locator, tag, qualifier)
199
+ return false if locator[:index]
200
+ return false if locator[:text] and tag == 'input' and qualifier[:type] == 'hidden'
201
+ return false if locator[:href] and tag == 'a'
202
+ return false if locator[:text] and ['div', 'span', 'td'].include? tag
203
+ return false if locator[:value] and tag == 'input' and qualifier[:type] == 'submit'
204
+ return false if locator[:value] and tag == 'input' and qualifier[:type] == 'radio'
205
+ true
206
+ end
207
+
208
+ end # class PlatformObject
209
+
210
+ end # module SeleniumWebDriver
211
+ end # module Platforms
212
+ end # module TerminusSpec
213
+
214
+ Dir["#{File.dirname(__FILE__)}/../web_objects/**/*.rb"].each { |file| require file }
@@ -0,0 +1,98 @@
1
+ module TerminusSpec
2
+ module Platforms
3
+ module SeleniumWebDriver
4
+ module WebObject
5
+
6
+ def ==(other)
7
+ @web_object == other # other.element??
8
+ end
9
+
10
+ def visible?
11
+ @web_object.displayed?
12
+ end
13
+
14
+ def exists?
15
+ nil != @web_object
16
+ end
17
+
18
+ def text
19
+ @web_object.text
20
+ end
21
+
22
+ def value
23
+ @web_object.attribute('value')
24
+ end
25
+
26
+ def tag_name
27
+ @web_object.tag_name
28
+ end
29
+
30
+ def clear
31
+ @web_object.clear
32
+ end
33
+
34
+ def right_click
35
+ @web_object.context_click
36
+ end
37
+
38
+ # Send keystrokes to the object.
39
+ # @param [String, Symbol, Array]
40
+ # @examples
41
+ # web_object.send_keys "test" #=> value: 'foo'
42
+ # web_object.send_keys "tet", :arrow_left, "s" #=> value: 'test'
43
+ # web_object.send_keys [:control, 'a'], :space #=> value: ' '
44
+ # @see Selenium::WebDriver::Keys::KEYS
45
+ def send_keys(*args)
46
+ @web_object.send_keys(*args)
47
+ end
48
+
49
+ # Waits until an object is present on the screen.
50
+ # @param [Integer] (defaults to: 5) seconds to wait before timing out
51
+ def when_present(timeout=5)
52
+ wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => "Object not present in #{timeout} seconds"})
53
+ wait.until do
54
+ self.exists?
55
+ end
56
+ self
57
+ end
58
+
59
+ # Waits until an object is visible on the screen.
60
+ # @param [Integer] (defaults to: 5) seconds to wait before timing out
61
+ def when_visible(timeout=5)
62
+ wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => "Object not present in #{timeout} seconds"})
63
+ wait.until do
64
+ self.visible?
65
+ end
66
+ self
67
+ end
68
+
69
+ # Waits until an object is not visible on the screen.
70
+ # @param [Integer] (defaults to: 5) seconds to wait before timing out
71
+ def when_not_visible(timeout=5)
72
+ wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => "Object not present in #{timeout} seconds"})
73
+ wait.until do
74
+ not self.visible?
75
+ end
76
+ self
77
+ end
78
+
79
+ # Waits until a given returns true (and thus was executed).
80
+ # @param [Integer] (defaults to: 5) seconds to wait before timing out
81
+ # @param [String] the message to display if the event times out
82
+ # @param the block to execute when the event occurs
83
+ def wait_until(timeout=5, message=nil, &block)
84
+ wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => message})
85
+ wait.until(&block)
86
+ end
87
+
88
+ # Get the value of a the given attribute of the object.
89
+ # @param [String] attribute name
90
+ # @return [String,nil] attribute value
91
+ def attribute(attribute_name)
92
+ @web_object.attribute attribute_name
93
+ end
94
+
95
+ end # module WebObject
96
+ end # module SeleniumWebDriver
97
+ end # module Platforms
98
+ end # module TerminusSpec
@@ -0,0 +1,13 @@
1
+ module TerminusSpec
2
+ module Platforms
3
+ module SeleniumWebDriver
4
+ module Button
5
+
6
+ def text
7
+ raise "Returning text from a button element in Selenium is not possible."
8
+ end
9
+
10
+ end # module Button
11
+ end # module SeleniumWebDriver
12
+ end # module Platforms
13
+ end # module TerminusSpec
@@ -0,0 +1,13 @@
1
+ module TerminusSpec
2
+ module Platforms
3
+ module SeleniumWebDriver
4
+ module Link
5
+
6
+ def value
7
+ raise "Returning value from a link element in Selenium is not possible."
8
+ end
9
+
10
+ end # module Link
11
+ end # module SeleniumWebDriver
12
+ end # module Platforms
13
+ end # module TerminusSpec
@@ -0,0 +1,14 @@
1
+ module TerminusSpec
2
+ module Platforms
3
+ module SeleniumWebDriver
4
+ module TextField
5
+
6
+ def value=(this)
7
+ @web_object.clear
8
+ @web_object.send_keys(this)
9
+ end
10
+
11
+ end # module TextField
12
+ end # module SeleniumWebDriver
13
+ end # module Platforms
14
+ end # module TerminusSpec