rwebspec 4.0 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,6 +1,12 @@
1
1
  CHANGELOG
2
2
  =========
3
3
 
4
+ 4.1
5
+ [Fixed] - click_button, click_button_with_id support :index
6
+ [Refactored] - unit tests
7
+ [Feature] - Save screenshot for Selenium
8
+ [CHANGE] - open_browser(base_url, options) => open_browser(:base_url => "http://")
9
+
4
10
  4.0
5
11
  - Merge rwebspec-webdriver with rwebspec!
6
12
 
data/README CHANGED
@@ -9,7 +9,7 @@ specification "User Profile" do
9
9
  include TestHelper
10
10
 
11
11
  before(:all) do
12
- open_browser("http://demo.adminwise.com")
12
+ open_browser(:base_url => "http://demo.adminwise.com")
13
13
  reset_database
14
14
  end
15
15
 
data/Rakefile CHANGED
@@ -1,7 +1,8 @@
1
1
  require 'rubygems'
2
2
  require 'rspec/core/rake_task'
3
3
  # require 'rake/rdoctask'
4
- require 'rake/gempackagetask'
4
+ require 'rubygems/package_task'
5
+
5
6
  require 'rdoc' # require rdoc 2
6
7
  # gem 'darkfish-rdoc'
7
8
  # require 'darkfish-rdoc'
@@ -24,7 +25,7 @@ end
24
25
 
25
26
  desc 'Run all specs'
26
27
  RSpec::Core::RakeTask.new('spec') do |t|
27
- t.rspec_opts = ['--format', 'specdoc', '--colour']
28
+ t.rspec_opts = ['']
28
29
  # t.libs = ["lib", "server/lib" ]
29
30
  t.pattern = Dir['spec/**/*_spec.rb'].sort
30
31
  end
@@ -77,7 +78,7 @@ end
77
78
  spec = Gem::Specification.new do |s|
78
79
  s.platform= Gem::Platform::RUBY
79
80
  s.name = "rwebspec"
80
- s.version = "4.0"
81
+ s.version = "4.1.0"
81
82
  s.summary = "Web application functional specification in Ruby"
82
83
  s.description = "Executable functional specification for web applications in RSpec syntax with Watir or Selenium"
83
84
 
@@ -97,13 +98,13 @@ spec = Gem::Specification.new do |s|
97
98
  s.files = s.files + Dir.glob( "sample/**/*")
98
99
  s.files = s.files + Dir.glob( "docs/**/*" )
99
100
  s.add_dependency(%q<rspec>, [">= 2.10"])
100
- s.add_dependency(%q<rspec-core>, [">= 2.10.1"])
101
+ s.add_dependency(%q<rspec-core>, ["= 2.10.1"])
101
102
  s.add_dependency("commonwatir", ">= 3.0")
102
- if RUBY_PLATFORM =~ /mingw/
103
+ unless RUBY_PLATFORM =~ /mingw/
103
104
  s.add_dependency("selenium-webdriver")
104
105
  end
105
106
  end
106
107
 
107
- Rake::GemPackageTask.new(spec) do |pkg|
108
+ Gem::PackageTask.new(spec) do |pkg|
108
109
  pkg.need_zip = true
109
110
  end
@@ -57,7 +57,11 @@ module RWebSpec
57
57
  end
58
58
 
59
59
  def notify_screenshot_location(image_file_path)
60
- connect_to_testwise(" SHOT", image_file_path)
60
+ begin
61
+ connect_to_testwise(" SHOT", image_file_path)
62
+ rescue => e
63
+ puts "[DEBUG] failed to notify TestWise a screenshot"
64
+ end
61
65
  end
62
66
 
63
67
  # find out the line (and file) the execution is on, and notify iTest via Socket
@@ -80,11 +80,13 @@ module RWebSpec
80
80
  # assert_link_present_with_text("Click ") # =>
81
81
  #
82
82
  def assert_link_present_with_text(link_text)
83
+
83
84
  @web_browser.links.each { |link|
84
85
  return if link.text.include?(link_text)
85
86
  }
86
87
  fail( "can't find the link containing text: #{link_text}")
87
- end
88
+
89
+ end
88
90
 
89
91
  def assert_link_not_present_with_text(link_text)
90
92
  @web_browser.links.each { |link|
@@ -92,23 +94,43 @@ module RWebSpec
92
94
  }
93
95
  end
94
96
 
97
+ def is_selenium_element?(elem)
98
+ elem.class.name =~ /Selenium/
99
+ end
100
+
101
+ def element_name(elem)
102
+ elem.class.name =~ /Selenium/ ? elem['name'] : elem.name
103
+ end
104
+
105
+ def element_value(elem)
106
+ elem.class.name =~ /Selenium/ ? elem['value'] : elem.value
107
+ end
95
108
 
96
109
  ##
97
110
  # Checkbox
98
111
  def assert_checkbox_not_selected(checkbox_name)
99
112
  @web_browser.checkboxes.each { |checkbox|
100
- if (checkbox.name == checkbox_name) then
101
- perform_assertion { assert(!checkbox.set?, "Checkbox #{checkbox_name} is checked unexpectly") }
113
+ the_element_name = element_name(checkbox)
114
+ if (the_element_name == checkbox_name) then
115
+ if is_selenium_element?(checkbox)
116
+ perform_assertion { assert(!checkbox.selected?, "Checkbox #{checkbox_name} is checked unexpectly") }
117
+ else
118
+ perform_assertion { assert(!checkbox.set?, "Checkbox #{checkbox_name} is checked unexpectly") }
119
+ end
102
120
  end
103
121
  }
104
122
  end
105
-
106
123
  alias assert_checkbox_not_checked assert_checkbox_not_selected
107
124
 
108
125
  def assert_checkbox_selected(checkbox_name)
109
126
  @web_browser.checkboxes.each { |checkbox|
110
- if (checkbox.name == checkbox_name) then
111
- perform_assertion { assert(checkbox.set?, "Checkbox #{checkbox_name} not checked") }
127
+ the_element_name = element_name(checkbox)
128
+ if (the_element_name == checkbox_name) then
129
+ if is_selenium_element?(checkbox)
130
+ perform_assertion { assert(checkbox.selected?, "Checkbox #{checkbox_name} not checked") }
131
+ else
132
+ perform_assertion { assert(checkbox.set?, "Checkbox #{checkbox_name} not checked") }
133
+ end
112
134
  end
113
135
  }
114
136
  end
@@ -130,7 +152,8 @@ module RWebSpec
130
152
 
131
153
  def assert_option_not_present(select_name, option_label)
132
154
  @web_browser.select_lists.each { |select|
133
- next unless select.name == select_name
155
+ the_element_name = element_name(select)
156
+ next unless the_element_name == select_name
134
157
  select.options.each do |option| # items in the list
135
158
  perform_assertion { assert(!(option.text == option_label), "unexpected select option: #{option_label} for #{select_name} found") }
136
159
  end
@@ -141,10 +164,19 @@ module RWebSpec
141
164
 
142
165
  def assert_option_value_present(select_name, option_value)
143
166
  @web_browser.select_lists.each { |select|
144
- next unless select.name == select_name
145
- select.options.each do |option| # items in the list
146
- return if option.value == option_value
147
- end
167
+ the_element_name = element_name(select)
168
+ next unless the_element_name == select_name
169
+
170
+ if RWebSpec.framework == "Watir"
171
+ select.options.each do |option| # items in the list
172
+ return if option.value == option_value
173
+ end
174
+ else
175
+ select.find_elements(:tag_name => "option" ).each do |option|
176
+ return if element_value(option) == option_value
177
+ end
178
+ end
179
+
148
180
  }
149
181
  fail("can't find the combo box with value: #{option_value}")
150
182
  end
@@ -154,12 +186,21 @@ module RWebSpec
154
186
 
155
187
  def assert_option_present(select_name, option_label)
156
188
  @web_browser.select_lists.each { |select|
157
- next unless select.name == select_name
158
- select.options.each do |option| # items in the list
159
- return if option.text == option_label
160
- end
189
+ the_element_name = element_name(select)
190
+ next unless the_element_name == select_name
191
+
192
+ if RWebSpec.framework == "Watir"
193
+ select.options.each do |option| # items in the list
194
+ return if option.text == option_label
195
+ end
196
+ else
197
+ select.find_elements(:tag_name => "option" ).each do |option|
198
+ return if option.text == option_label
199
+ end
200
+ end
201
+
161
202
  }
162
- fail("can't find the combob box: #{select_name} with value: #{option_label}")
203
+ fail("can't find the combo box: #{select_name} with value: #{option_label}")
163
204
  end
164
205
 
165
206
  alias assert_select_label_present assert_option_present
@@ -167,12 +208,25 @@ module RWebSpec
167
208
 
168
209
  def assert_option_equals(select_name, option_label)
169
210
  @web_browser.select_lists.each { |select|
170
- next unless select.name == select_name
171
- select.options.each do |option| # items in the list
172
- if (option.text == option_label) then
173
- perform_assertion { assert_equal(select.value, option.value, "Select #{select_name}'s value is not equal to expected option label: '#{option_label}'") }
174
- end
175
- end
211
+ the_element_name = element_name(select)
212
+ next unless the_element_name == select_name
213
+
214
+ if RWebSpec.framework == "Watir"
215
+
216
+ select.options.each do |option| # items in the list
217
+ if (option.text == option_label) then
218
+ perform_assertion { assert_equal(select.value, option.value, "Select #{select_name}'s value is not equal to expected option label: '#{option_label}'") }
219
+ end
220
+ end
221
+
222
+ else
223
+ select.find_elements(:tag_name => "option" ).each do |option|
224
+ if (option.text == option_label) then
225
+ assert option.selected?
226
+ end
227
+ end
228
+
229
+ end
176
230
  }
177
231
  end
178
232
 
@@ -181,8 +235,14 @@ module RWebSpec
181
235
 
182
236
  def assert_option_value_equals(select_name, option_value)
183
237
  @web_browser.select_lists.each { |select|
184
- next unless select.name == select_name
185
- perform_assertion { assert_equal(select.value, option_value, "Select #{select_name}'s value is not equal to expected: '#{option_value}'") }
238
+ the_element_name = element_name(select)
239
+ next unless the_element_name == select_name
240
+
241
+ if RWebSpec.framework == "Watir"
242
+ perform_assertion { assert_equal(select.value, option_value, "Select #{select_name}'s value is not equal to expected: '#{option_value}'") }
243
+ else
244
+ perform_assertion { assert_equal(element_value(select), option_value, "Select #{select_name}'s value is not equal to expected: '#{option_value}'") }
245
+ end
186
246
  }
187
247
  end
188
248
 
@@ -195,24 +255,31 @@ module RWebSpec
195
255
  # radio_group is the name field, radio options 'value' field
196
256
  def assert_radio_option_not_present(radio_group, radio_option)
197
257
  @web_browser.radios.each { |radio|
198
- if (radio.name == radio_group) then
199
- perform_assertion { assert(!(radio_option == radio.value), "unexpected radio option: " + radio_option + " found") }
258
+ the_element_name = element_name(radio)
259
+ if (the_element_name == radio_group) then
260
+ perform_assertion { assert(!(radio_option == element_value(radio) ), "unexpected radio option: " + radio_option + " found") }
200
261
  end
201
262
  }
202
263
  end
203
264
 
204
265
  def assert_radio_option_present(radio_group, radio_option)
205
266
  @web_browser.radios.each { |radio|
206
- return if (radio.name == radio_group) and (radio_option == radio.value)
267
+ the_element_name = element_name(radio)
268
+ return if (the_element_name == radio_group) and (radio_option == element_value(radio) )
207
269
  }
208
270
  fail("can't find the radio option : '#{radio_option}'")
209
271
  end
210
272
 
211
273
  def assert_radio_option_selected(radio_group, radio_option)
212
274
  @web_browser.radios.each { |radio|
213
- if (radio.name == radio_group and radio_option == radio.value) then
214
- perform_assertion { assert(radio.set?, "Radio button #{radio_group}-[#{radio_option}] not checked") }
215
- end
275
+ the_element_name = element_name(radio)
276
+ if (the_element_name == radio_group and radio_option == element_value(radio) ) then
277
+ if is_selenium_element?(radio)
278
+ perform_assertion { assert(radio.selected?, "Radio button #{radio_group}-[#{radio_option}] not checked") }
279
+ else
280
+ perform_assertion { assert(radio.set?, "Radio button #{radio_group}-[#{radio_option}] not checked") }
281
+ end
282
+ end
216
283
  }
217
284
  end
218
285
 
@@ -221,8 +288,13 @@ module RWebSpec
221
288
 
222
289
  def assert_radio_option_not_selected(radio_group, radio_option)
223
290
  @web_browser.radios.each { |radio|
224
- if (radio.name == radio_group and radio_option == radio.value) then
225
- perform_assertion { assert(!radio.set?, "Radio button #{radio_group}-[#{radio_option}] checked unexpected") }
291
+ the_element_name = element_name(radio)
292
+ if (the_element_name == radio_group and radio_option == element_value(radio) ) then
293
+ if is_selenium_element?(radio)
294
+ perform_assertion { assert(!radio.selected?, "Radio button #{radio_group}-[#{radio_option}] checked unexpected") }
295
+ else
296
+ perform_assertion { assert(!radio.set?, "Radio button #{radio_group}-[#{radio_option}] checked unexpected") }
297
+ end
226
298
  end
227
299
  }
228
300
  end
@@ -234,26 +306,28 @@ module RWebSpec
234
306
  # Button
235
307
  def assert_button_not_present(button_id)
236
308
  @web_browser.buttons.each { |button|
237
- perform_assertion { assert(button.id != button_id, "unexpected button id: #{button_id} found") }
309
+ the_button_id = RWebSpec.framework == "Watir" ? button.id : button["id"]
310
+ perform_assertion { assert(the_button_id != button_id, "unexpected button id: #{button_id} found") }
238
311
  }
239
312
  end
240
313
 
241
314
  def assert_button_not_present_with_text(text)
242
315
  @web_browser.buttons.each { |button|
243
- perform_assertion { assert(button.value != text, "unexpected button id: #{text} found") }
316
+ perform_assertion { assert(element_value(button) != text, "unexpected button id: #{text} found") }
244
317
  }
245
318
  end
246
319
 
247
320
  def assert_button_present(button_id)
248
321
  @web_browser.buttons.each { |button|
249
- return if button_id == button.id
322
+ the_button_id = RWebSpec.framework == "Watir" ? button.id : button["id"]
323
+ return if button_id == the_button_id
250
324
  }
251
325
  fail("can't find the button with id: #{button_id}")
252
326
  end
253
327
 
254
328
  def assert_button_present_with_text(button_text)
255
- @web_browser.buttons.each { |button|
256
- return if button_text == button.value
329
+ @web_browser.buttons.each { |button|
330
+ return if button_text == element_value(button)
257
331
  }
258
332
  fail("can't find the button with text: #{button_text}")
259
333
  end
@@ -269,7 +343,12 @@ module RWebSpec
269
343
  # assert_exists("label", "receipt_date")
270
344
  # assert_exists(:span, :receipt_date)
271
345
  def assert_exists(tag, element_id)
272
- perform_assertion { assert(eval("#{tag}(:id, '#{element_id.to_s}').exists?"), "Element '#{tag}' with id: '#{element_id}' not found") }
346
+ if RWebSpec.framework == "Watir"
347
+ perform_assertion { assert(eval("#{tag}(:id, '#{element_id.to_s}').exists?"), "Element '#{tag}' with id: '#{element_id}' not found") }
348
+ else
349
+ perform_assertion { assert( @web_browser.driver.find_element(:tag_name => tag, :id => element_id))}
350
+ end
351
+
273
352
  end
274
353
 
275
354
  alias assert_exists? assert_exists
@@ -350,7 +429,12 @@ module RWebSpec
350
429
  # assert_text_field_value("text1", "text already there") => true
351
430
  #
352
431
  def assert_text_field_value(textfield_name, text)
353
- perform_assertion { assert_equal(text, text_field(:name, textfield_name).value) }
432
+ if RWebSpec.framework == "Watir"
433
+ perform_assertion { assert_equal(text, text_field(:name, textfield_name).value) }
434
+ else
435
+ the_element = @web_browser.driver.find_element(:name, textfield_name)
436
+ perform_assertion { assert_equal(text, element_value(the_element)) }
437
+ end
354
438
  end
355
439
 
356
440
 
@@ -373,9 +457,9 @@ module RWebSpec
373
457
 
374
458
  private
375
459
  def table_source(table_id, options)
376
- elem_table = table(:id, table_id.to_s)
460
+ elem_table = RWebSpec.framework == "Watir" ? table(:id, table_id.to_s) : @web_browser.driver.find_element(:id => table_id)
377
461
  elem_table_text = elem_table.text
378
- elem_table_html = is_firefox? ? elem_table.innerHTML : elem_table.html
462
+ elem_table_html = RWebSpec.framework == "Watir" ? elem_table.html : elem_table["innerHTML"];
379
463
  table_source = options[:just_plain_text] ? elem_table_text : elem_table_html
380
464
  end
381
465
 
@@ -40,8 +40,8 @@ module RWebSpec
40
40
  alias try_until try_for
41
41
 
42
42
  def try(timeout = $testwise_polling_timeout, polling_interval = $testwise_polling_interval || 1, &block)
43
- puts "Warning: method 'try' is deprecated (won't support in RWebSpec 3), use try_until instead."
44
- try_until(timeout, polling_interval) {
43
+ puts "Warning: method 'try' is deprecated (won't support in RWebSpec 3), use try_for instead."
44
+ try_for(timeout, polling_interval) {
45
45
  yield
46
46
  }
47
47
  end
@@ -92,42 +92,6 @@ module RWebSpec
92
92
  end
93
93
 
94
94
 
95
-
96
- # use win32screenshot library to save curernt active window, which shall be IE
97
- #
98
- # opts[:to_dir] => the direcotry to save image under
99
- def take_screenshot(opts = {})
100
- # puts "calling new take screenshot: #{$screenshot_supported}"
101
- unless $screenshot_supported
102
- puts " [WARN] Screenhost not supported, check whether win32screenshot gem is installed"
103
- return
104
- end
105
-
106
- begin
107
- screenshot_image_filename = "screenshot_" + Time.now.strftime("%m%d%H%M%S") + ".jpg"
108
- the_dump_dir = opts[:to_dir] || default_dump_dir
109
- FileUtils.mkdir_p(the_dump_dir) unless File.exists?(the_dump_dir)
110
- screenshot_image_filepath = File.join(the_dump_dir, screenshot_image_filename)
111
- screenshot_image_filepath.gsub!("/", "\\") if is_windows?
112
-
113
- FileUtils.rm_f(screenshot_image_filepath) if File.exist?(screenshot_image_filepath)
114
-
115
- if is_firefox? then
116
- Win32::Screenshot::Take.of(:window, :title => /mozilla\sfirefox/i).write(screenshot_image_filepath)
117
- elsif ie
118
- Win32::Screenshot::Take.of(:window, :title => /internet\sexplorer/i).write(screenshot_image_filepath)
119
- else
120
- Win32::Screenshot::Take.of(:foreground).write(screenshot_image_filepath)
121
- end
122
- notify_screenshot_location(screenshot_image_filepath)
123
- rescue ::DL::DLTypeError => de
124
- puts "No screenshot libray found: #{de}"
125
- rescue => e
126
- puts "error on taking screenshot: #{e}"
127
- end
128
-
129
- end
130
-
131
95
  #= Convenient functions
132
96
  #
133
97
 
@@ -29,7 +29,7 @@ module RWebSpec
29
29
  # 1. pass as first argument
30
30
  # 2. If running using TestWise, used as confiured
31
31
  # 3. Use default value set
32
- def open_browser(base_url = nil, options = {})
32
+ def open_browser(options = {})
33
33
 
34
34
  begin
35
35
  support_unicode
@@ -38,7 +38,12 @@ module RWebSpec
38
38
  end
39
39
 
40
40
  base_url ||= $TESTWISE_PROJECT_BASE_URL
41
- base_url ||= $TESTWISE_PROJECT_BASE_URL
41
+ if options && options.class == String
42
+ base_url ||= options
43
+ elsif options && options.class == Hash && options[:base_url]
44
+ base_url ||= options[:base_url]
45
+ end
46
+
42
47
  base_url ||= $BASE_URL
43
48
  raise "base_url must be set" if base_url.nil?
44
49
 
@@ -50,7 +55,7 @@ module RWebSpec
50
55
  :go => true}
51
56
 
52
57
  options = default_options.merge options
53
- ($TESTWISE_HIDE_BROWSER || $TESTWISE_HIDE_BROWSER) ? $HIDE_IE = true : $HIDE_IE = false
58
+ ($TESTWISE_HIDE_BROWSER) ? $HIDE_IE = true : $HIDE_IE = false
54
59
 
55
60
  if base_url =~ /^file:/
56
61
  uri_base = base_url
@@ -152,7 +157,7 @@ module RWebSpec
152
157
 
153
158
  # Go to another page on the testing site.
154
159
  #
155
- # open_browser("http://www.itest2.com")
160
+ # open_browser(:base_url => "http://www.itest2.com")
156
161
  # goto_page("/demo") # visit page http://www.itest2.com/demo
157
162
  #
158
163
  def goto_page(page)
@@ -165,7 +170,7 @@ module RWebSpec
165
170
 
166
171
  # Go to another web site, normally different site being tested on
167
172
  #
168
- # open_browser("http://www.itest2.com")
173
+ # open_browser(:base_url => "http://www.itest2.com")
169
174
  # goto_url("http://myorganized.info")
170
175
  def goto_url(url)
171
176
  @web_browser.goto_url url
@@ -751,6 +756,58 @@ module RWebSpec
751
756
  def basic_authentication(username, password, options = {})
752
757
  basic_authentication_ie(options[:title], username, password, options)
753
758
  end
759
+
760
+ # TODO: Common driver module => this is shared by both Watir and Selenium
761
+ #
762
+
763
+
764
+ # TODO: Common driver module => this is shared by both Watir and Selenium
765
+ #
766
+
767
+ # use win32screenshot library or Selenium to save curernt active window
768
+ #
769
+ # opts[:to_dir] => the direcotry to save image under
770
+ def take_screenshot(to_file = nil, opts = {})
771
+ # puts "calling new take screenshot: #{$screenshot_supported}"
772
+ # unless $screenshot_supported
773
+ # puts " [WARN] Screenhost not supported, check whether win32screenshot gem is installed"
774
+ # return
775
+ # end
776
+
777
+ if to_file
778
+ screenshot_image_filepath = to_file
779
+ else
780
+ screenshot_image_filename = "screenshot_" + Time.now.strftime("%m%d%H%M%S") + ".jpg"
781
+ the_dump_dir = opts[:to_dir] || default_dump_dir
782
+ FileUtils.mkdir_p(the_dump_dir) unless File.exists?(the_dump_dir)
783
+ screenshot_image_filepath = File.join(the_dump_dir, screenshot_image_filename)
784
+ screenshot_image_filepath.gsub!("/", "\\") if is_windows?
785
+
786
+ FileUtils.rm_f(screenshot_image_filepath) if File.exist?(screenshot_image_filepath)
787
+ end
788
+
789
+ if RWebSpec.framework == "Watir"
790
+ begin
791
+ if is_firefox? then
792
+ Win32::Screenshot::Take.of(:window, :title => /mozilla\sfirefox/i).write(screenshot_image_filepath)
793
+ elsif ie
794
+ Win32::Screenshot::Take.of(:window, :title => /internet\sexplorer/i).write(screenshot_image_filepath)
795
+ else
796
+ Win32::Screenshot::Take.of(:foreground).write(screenshot_image_filepath)
797
+ end
798
+ notify_screenshot_location(screenshot_image_filepath)
799
+ rescue ::DL::DLTypeError => de
800
+ puts "No screenshot libray found: #{de}"
801
+ rescue => e
802
+ puts "error on taking screenshot: #{e}"
803
+ end
804
+ else
805
+ # save screenshot with selenium
806
+ @web_browser.driver.save_screenshot(screenshot_image_filepath)
807
+ notify_screenshot_location(screenshot_image_filepath)
808
+ end
809
+
810
+ end
754
811
 
755
812
  # end of methods
756
813
 
@@ -295,7 +295,7 @@ module RWebSpec
295
295
 
296
296
  # Go to a page
297
297
  # Usage:
298
- # open_browser("http://www.itest2.com"
298
+ # open_browser(:base_url => "http://www.itest2.com")
299
299
  # ....
300
300
  # goto_page("/purchase") # full url => http://www.itest.com/purchase
301
301
  def goto_page(page)
@@ -29,8 +29,9 @@ module RWebSpec
29
29
  #
30
30
  # New Options:
31
31
  # :browser => :ie | :firefox | :chrome
32
- def open_browser(base_url = nil, options = {})
32
+ def open_browser(options = {})
33
33
  # puts "[DEBUG] [SeleniumDriver] Callling open_browser #{base_url}"
34
+
34
35
  begin
35
36
  support_unicode
36
37
  rescue => e
@@ -38,6 +39,11 @@ module RWebSpec
38
39
  end
39
40
 
40
41
  base_url ||= $TESTWISE_PROJECT_BASE_URL
42
+ if options && options.class == String
43
+ base_url ||= options
44
+ elsif options && options.class == Hash && options[:base_url]
45
+ base_url ||= options[:base_url]
46
+ end
41
47
  base_url ||= $BASE_URL
42
48
  raise "base_url must be set" if base_url.nil?
43
49
 
@@ -80,7 +86,8 @@ module RWebSpec
80
86
  end
81
87
 
82
88
  # remembering browser handle for debugging need
83
- $browser = @web_browser
89
+ $browser = @web_browser
90
+
84
91
  return @web_browser
85
92
  end
86
93
 
@@ -179,7 +186,7 @@ module RWebSpec
179
186
 
180
187
  # Go to another page on the testing site.
181
188
  #
182
- # open_browser("http://www.itest2.com")
189
+ # open_browser(:base_url => "http://www.itest2.com")
183
190
  # goto_page("/demo") # visit page http://www.itest2.com/demo
184
191
  #
185
192
  def goto_page(page)
@@ -191,7 +198,7 @@ module RWebSpec
191
198
 
192
199
  # Go to another web site, normally different site being tested on
193
200
  #
194
- # open_browser("http://www.itest2.com")
201
+ # open_browser(:base_url => "http://www.itest2.com")
195
202
  # goto_url("http://myorganized.info")
196
203
  def goto_url(url)
197
204
  puts "Calling web_browser goto: #{@web_browser.inspect}"
@@ -394,6 +401,8 @@ module RWebSpec
394
401
  else
395
402
  to_dir = ENV['TEMP_DIR'] || (is_windows? ? "C:\\temp" : "/tmp")
396
403
  end
404
+ puts "Save screenshot to default dir: #{to_dir}"
405
+ return to_dir
397
406
  end
398
407
 
399
408
  # For current page souce to a file in specified folder for inspection
@@ -741,5 +750,56 @@ module RWebSpec
741
750
  basic_authentication_ie(options[:title], username, password, options)
742
751
  end
743
752
 
753
+
754
+
755
+ # TODO: Common driver module => this is shared by both Watir and Selenium
756
+ #
757
+
758
+ # use win32screenshot library or Selenium to save curernt active window
759
+ #
760
+ # opts[:to_dir] => the direcotry to save image under
761
+ def take_screenshot(to_file = nil, opts = {})
762
+ # puts "calling new take screenshot: #{$screenshot_supported}"
763
+ # unless $screenshot_supported
764
+ # puts " [WARN] Screenhost not supported, check whether win32screenshot gem is installed"
765
+ # return
766
+ # end
767
+
768
+ if to_file
769
+ screenshot_image_filepath = to_file
770
+ else
771
+ screenshot_image_filename = "screenshot_" + Time.now.strftime("%m%d%H%M%S") + ".jpg"
772
+ the_dump_dir = opts[:to_dir] || default_dump_dir
773
+ FileUtils.mkdir_p(the_dump_dir) unless File.exists?(the_dump_dir)
774
+ screenshot_image_filepath = File.join(the_dump_dir, screenshot_image_filename)
775
+ screenshot_image_filepath.gsub!("/", "\\") if is_windows?
776
+
777
+ FileUtils.rm_f(screenshot_image_filepath) if File.exist?(screenshot_image_filepath)
778
+ end
779
+
780
+ if RWebSpec.framework == "Watir"
781
+ begin
782
+ if is_firefox? then
783
+ Win32::Screenshot::Take.of(:window, :title => /mozilla\sfirefox/i).write(screenshot_image_filepath)
784
+ elsif ie
785
+ Win32::Screenshot::Take.of(:window, :title => /internet\sexplorer/i).write(screenshot_image_filepath)
786
+ else
787
+ Win32::Screenshot::Take.of(:foreground).write(screenshot_image_filepath)
788
+ end
789
+ notify_screenshot_location(screenshot_image_filepath)
790
+ rescue ::DL::DLTypeError => de
791
+ puts "No screenshot libray found: #{de}"
792
+ rescue => e
793
+ puts "error on taking screenshot: #{e}"
794
+ end
795
+ else
796
+ # save screenshot with selenium
797
+ @web_browser.driver.save_screenshot(screenshot_image_filepath)
798
+ notify_screenshot_location(screenshot_image_filepath)
799
+ end
800
+
801
+ end
802
+
803
+
744
804
  end
745
805
  end
@@ -37,8 +37,16 @@ module RWebSpec
37
37
  when "ie"
38
38
  initialize_ie_browser(existing_browser, options)
39
39
  when "htmlunit"
40
- initialize_htmlunit_browser(base_url, options)
40
+ initialize_htmlunit_browser(base_url, options)
41
41
  end
42
+
43
+ begin
44
+ if options[:resize_to] && options[:resize_to].class == Array
45
+ @browser.manage.window.resize_to(options[:resize_to][0], options[:resize_to][1])
46
+ end
47
+ rescue => e
48
+ puts "[ERROR] failed to resize => #{options[:resize_to]}"
49
+ end
42
50
  end
43
51
 
44
52
  def initialize_firefox_browser(existing_browser, base_url, options)
@@ -83,7 +91,7 @@ module RWebSpec
83
91
  else
84
92
  @browser.speed = :zippy
85
93
  end
86
- return
94
+ return @browser
87
95
  end
88
96
 
89
97
  @browser = Selenium::WebDriver.for :ie
@@ -101,6 +109,7 @@ module RWebSpec
101
109
  # else
102
110
  # puts "close other browser instances not working yet in Ruby 1.9.1 version of Watir"
103
111
  # end
112
+
104
113
  end
105
114
 
106
115
  # TODO resuse not working yet
@@ -258,12 +267,43 @@ module RWebSpec
258
267
  end
259
268
  end
260
269
 
261
- [:images, :links, :buttons, :select_lists, :checkboxes, :radios, :text_fields, :divs, :dls, :dds, :dts, :ems, :lis, :maps, :spans, :strongs, :ps, :pres, :labels].each do |method|
270
+ # :links => removed
271
+ # :checkboxes => removed
272
+ # :radios => removed
273
+ # :select_lists => removed
274
+ # :buttons => removed
275
+ # :divs => removed
276
+ [:images, :text_fields, :dls, :dds, :dts, :ems, :lis, :maps, :spans, :strongs, :ps, :pres, :labels].each do |method|
262
277
  define_method method do
263
278
  @browser.send(method)
264
279
  end
265
280
  end
266
281
 
282
+ def links
283
+ @browser.find_elements(:tag_name, "a")
284
+ end
285
+
286
+ def checkboxes
287
+ @browser.find_elements(:xpath, "//input[@type='checkbox']")
288
+ end
289
+
290
+ def radios
291
+ @browser.find_elements(:xpath, "//input[@type='radio']")
292
+ end
293
+
294
+ def select_lists
295
+ @browser.find_elements(:tag_name, "select")
296
+ end
297
+
298
+ def buttons
299
+ button_array = @browser.find_elements(:tag_name, "button") + @browser.find_elements(:xpath, "//input[@type='submit']") + @browser.find_elements(:xpath, "//input[@type='button']")
300
+ return button_array
301
+ end
302
+
303
+ def divs
304
+ @browser.find_elements(:tag_name, "divs")
305
+ end
306
+
267
307
  # current url
268
308
  def current_url
269
309
  @browser.current_url
@@ -373,7 +413,7 @@ module RWebSpec
373
413
 
374
414
  # Go to a page
375
415
  # Usage:
376
- # open_browser("http://www.itest2.com"
416
+ # open_browser(:base_url => "http://www.itest2.com")
377
417
  # ....
378
418
  # goto_page("/purchase") # full url => http://www.itest.com/purchase
379
419
  def goto_page(page)
@@ -435,8 +475,17 @@ module RWebSpec
435
475
  # Click a button with give HTML id
436
476
  # Usage:
437
477
  # click_button_with_id("btn_sumbit")
478
+ # click_button_with_id("btn_sumbit", :index => 2) # the secone link with same id, not good gractice in HTML
438
479
  def click_button_with_id(id, opts = {})
439
- find_element(:id, id).click
480
+ if opts && opts[:index] && opts[:index].to_i() > 0
481
+ elements = find_elements(:id, id)
482
+ the_index = opts[:index].to_i() - 1
483
+ first_match = elements[the_index]
484
+ first_match.click
485
+ else
486
+ find_element(:id, id).click
487
+ end
488
+
440
489
  end
441
490
 
442
491
  # Click a button with give name
@@ -459,14 +508,15 @@ module RWebSpec
459
508
  if matching_buttons.size > 0
460
509
 
461
510
  if opts && opts[:index]
462
- puts "Call matching buttons: #{matching_buttons.inspect}"
463
- first_match = matching_buttons[opts[:index].to_i() - 1]
511
+ the_index = opts[:index].to_i() - 1
512
+ puts "Call matching buttons: #{matching_buttons.inspect} => #{the_index}"
513
+ first_match = matching_buttons[the_index]
464
514
  first_match.click
515
+ else
516
+ the_button = matching_buttons[0]
517
+ the_button.click
465
518
  end
466
519
 
467
- the_button = matching_buttons[0]
468
- the_button.click
469
-
470
520
  else
471
521
  raise "No button with value: #{caption} found"
472
522
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rwebspec
3
3
  version: !ruby/object:Gem::Version
4
- version: '4.0'
4
+ version: 4.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire: rwebspec
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-23 00:00:00.000000000 Z
12
+ date: 2013-01-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - '='
36
36
  - !ruby/object:Gem::Version
37
37
  version: 2.10.1
38
38
  type: :runtime
@@ -40,7 +40,7 @@ dependencies:
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - '='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 2.10.1
46
46
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '3.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: selenium-webdriver
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
62
78
  description: Executable functional specification for web applications in RSpec syntax
63
79
  with Watir or Selenium
64
80
  email: zhimin@agileway.com.au