safariwatir 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/safariwatir.rb CHANGED
@@ -94,6 +94,7 @@ module Watir
94
94
  def operate_by_value(&block)
95
95
  @scripter.operate_by_input_value(self, &block)
96
96
  end
97
+ alias_method :operate_by_caption, :operate_by_value
97
98
  end
98
99
 
99
100
  class Form < HtmlElement
@@ -234,6 +235,8 @@ module Watir
234
235
  def column_count
235
236
  # TODO
236
237
  end
238
+
239
+ def tag; "TABLE"; end
237
240
  end
238
241
 
239
242
  class TableRow
@@ -257,6 +260,8 @@ module Watir
257
260
  def column_count
258
261
  # TODO
259
262
  end
263
+
264
+ def tag; "TR"; end
260
265
  end
261
266
 
262
267
  class TableCell < ContentElement
@@ -272,16 +277,20 @@ module Watir
272
277
  def operate(&block)
273
278
  @scripter.operate_by_table_cell(self, &block)
274
279
  end
280
+
281
+ def tag; "TD"; end
275
282
  end
276
283
 
277
284
  class TextField < InputElement
278
285
  def set(value)
286
+ @scripter.focus(self)
279
287
  @scripter.highlight(self) do
280
288
  clear_text_input
281
289
  value.length.times do |i|
282
290
  append_text_input(value[i, 1])
283
291
  end
284
292
  end
293
+ @scripter.blur(self)
285
294
  end
286
295
 
287
296
  def getContents
@@ -385,6 +394,8 @@ module Watir
385
394
  include Container
386
395
  include PageContainer
387
396
 
397
+ DEFAULT_TYPING_LAG = 0.08
398
+
388
399
  def self.start(url = nil)
389
400
  safari = new
390
401
  safari.goto(url) if url
@@ -394,8 +405,26 @@ module Watir
394
405
  def initialize
395
406
  @scripter = AppleScripter.new
396
407
  @scripter.ensure_window_ready
408
+ set_slow_speed
409
+ end
410
+
411
+ def set_fast_speed
412
+ @scripter.typing_lag = 0
397
413
  end
398
414
 
415
+ def set_slow_speed
416
+ @scripter.typing_lag = DEFAULT_TYPING_LAG
417
+ end
418
+
419
+ def speed=(how_fast)
420
+ case how_fast
421
+ when :fast : set_fast_speed
422
+ when :slow : set_slow_speed
423
+ else
424
+ raise ArgumentError, "Invalid speed: #{how_fast}"
425
+ end
426
+ end
427
+
399
428
  def close
400
429
  scripter.close
401
430
  end
@@ -8,9 +8,18 @@ module Watir
8
8
  NO_RESPONSE = "__safari_watir_no_response__"
9
9
  TABLE_CELL_NOT_FOUND = "__safari_watir_cell_unfound__"
10
10
 
11
+ JS_LIBRARY = %|
12
+ function dispatchOnChange(element) {
13
+ var event = document.createEvent('HTMLEvents');
14
+ event.initEvent('change', true, true);
15
+ element.dispatchEvent(event);
16
+ }
17
+ |
18
+
11
19
  class JavaScripter
12
20
  def operate(locator, operation)
13
- %|#{locator}
21
+ %|#{JS_LIBRARY}
22
+ #{locator}
14
23
  if (element) {
15
24
  #{operation}
16
25
  } else {
@@ -76,10 +85,11 @@ if (element) {
76
85
  include Watir::Exception
77
86
 
78
87
  attr_reader :js
88
+ attr_accessor :typing_lag
79
89
  private :js
80
90
 
81
91
  TIMEOUT = 10
82
-
92
+
83
93
  def initialize(scripter = JavaScripter.new)
84
94
  @js = scripter
85
95
  @app = AS.app("Safari")
@@ -125,11 +135,22 @@ if (element == undefined) {
125
135
  def document_text
126
136
  execute(%|document.getElementsByTagName('BODY').item(0).innerText;|)
127
137
  end
138
+
139
+ def document_html
140
+ execute(%|document.getElementsByTagName('BODY').item(0).outerHTML;|)
141
+ end
142
+
143
+ def focus(element)
144
+ execute(element.operate { %|element.focus();| }, element)
145
+ end
146
+
147
+ def blur(element)
148
+ execute(element.operate { %|element.blur();| }, element)
149
+ end
128
150
 
129
151
  def highlight(element, &block)
130
152
  execute(element.operate do
131
- %|element.focus();
132
- element.originalColor = element.style.backgroundColor;
153
+ %|element.originalColor = element.style.backgroundColor;
133
154
  element.style.backgroundColor = 'yellow';|
134
155
  end, element)
135
156
 
@@ -150,7 +171,23 @@ element.style.backgroundColor = 'yellow';|
150
171
 
151
172
  def select_option(element = @element)
152
173
  execute(element.operate do
153
- handle_option(element, %|element.options[i].selected = true;|)
174
+ %|var selected = -1;
175
+ var previous_selection = 0;
176
+ for (var i = 0; i < element.options.length; i++) {
177
+ if (element.options[i].selected) {
178
+ previous_selection = i;
179
+ }
180
+ if (element.options[i].#{element.how} == '#{element.what}') {
181
+ element.options[i].selected = true;
182
+ selected = i;
183
+ }
184
+ }
185
+ if (selected == -1) {
186
+ return '#{ELEMENT_NOT_FOUND}';
187
+ } else if (previous_selection != selected) {
188
+ dispatchOnChange(element.options[selected]);
189
+ }
190
+ |
154
191
  end, element)
155
192
  end
156
193
 
@@ -177,8 +214,10 @@ if (!option_found) {
177
214
  end
178
215
 
179
216
  def append_text_input(value, element = @element)
217
+ sleep typing_lag
180
218
  execute(element.operate do
181
219
  %|element.value += '#{value}';
220
+ dispatchOnChange(element);
182
221
  element.setSelectionRange(element.value.length, element.value.length);|
183
222
  end, element)
184
223
  end
@@ -419,22 +458,22 @@ SCRIPT`
419
458
  nil
420
459
  end
421
460
 
422
- def page_load
461
+ def page_load
423
462
  last_location = current_location
424
463
  yield
425
464
  sleep 1
426
- return if last_location == current_location
427
465
 
428
466
  tries = 0
429
467
  TIMEOUT.times do |tries|
430
468
  if "complete" == eval_js("DOCUMENT.readyState")
469
+ sleep 0.4
431
470
  handle_client_redirect
432
471
  break
433
472
  else
434
473
  sleep 1
435
474
  end
436
475
  end
437
- raise "Unable to load page withing #{TIMEOUT} seconds" if tries == TIMEOUT-1
476
+ raise "Unable to load page within #{TIMEOUT} seconds" if tries == TIMEOUT-1
438
477
  end
439
478
 
440
479
  def handle_client_redirect
@@ -2,11 +2,13 @@ require 'rubygems'
2
2
  require 'safariwatir'
3
3
 
4
4
  # TODO
5
- # Be more attached to the Safari window. Currently, if a different window is selected, the AppleScript executes against it.
5
+ # Be more attached to the Safari window.
6
+ # Currently, if a different window is selected, the AppleScript executes against it.
6
7
  # Verify onclick is working for buttons and links
7
8
  # TextFields should not respond to button method, etc.
8
9
 
9
- # Unsupported Elements: Test that P/Div/Span/TD handle link, button, etc., Javascript confirm [OK/CANCEL], Javascript prompt, Javascript popup windows
10
+ # Unsupported Elements: Test that P/Div/Span/TD handle link, button, etc.,
11
+ # Javascript confirm [OK/CANCEL], Javascript prompt, Javascript popup windows
10
12
 
11
13
  # Need to find a better way to distinguish between a submit button and a checkbox, re: page_load
12
14
 
@@ -24,9 +26,7 @@ def safari.google_to_prag
24
26
  link(:text, "Programming Ruby, 2nd Ed.").click
25
27
  link(:url, "http://www.pragmaticprogrammer.com/titles/ruby/code/index.html").click
26
28
  link(:text, "Catalog").click
27
- # site was down
28
29
  link(:text, "All Books").click
29
- # goto("http://pragmaticprogrammer.com/bookshelf/") # workaround
30
30
  link(:text, /Agile Retrospectives/).click
31
31
  puts "FAILURE prag" unless contains_text("Dave Hoover")
32
32
  end
@@ -57,7 +57,7 @@ def safari.google_advanced
57
57
  radio(:id, "ss").set
58
58
  text_field(:name, "as_q").set("obtiva")
59
59
  button(:name, "btnG").click
60
- puts "FAILURE google" unless contains_text("RailsConf Facebook")
60
+ puts "FAILURE google" unless contains_text("Training, Coaching, and Software Development")
61
61
  end
62
62
 
63
63
  def safari.reddit
@@ -100,8 +100,9 @@ def safari.weinberg
100
100
  end
101
101
 
102
102
  def safari.tables
103
- goto("http://basecamphq.com/")
104
- puts "FAILURE basecamp content" unless table(:index, 1)[1][2].text =~ /What is Basecamp\?/
103
+ # Site Redesign, need to update test
104
+ # goto("http://basecamphq.com/")
105
+ # puts "FAILURE basecamp content" unless table(:index, 1)[1][2].text =~ /What is Basecamp\?/
105
106
 
106
107
  goto("http://www.jimthatcher.com/webcourse9.htm")
107
108
  puts "FAILURE thatcher" unless cell(:id, "c5").text == "subtotals"
@@ -119,6 +120,13 @@ def safari.tables
119
120
  puts "FAILURE dreamweaver" unless table(:id, "titletable")[1][1].text =~ /CSS/
120
121
  end
121
122
 
123
+ def safari.onchange
124
+ goto("http://www.gr8cardeal.co.uk/?s=1")
125
+ select_list(:name, "manufacturer").select_value("BMW")
126
+ sleep 0.3 # There's an Ajax call here
127
+ select_list(:name, "model").select_value("Z4 ROADSTER")
128
+ end
129
+
122
130
  begin
123
131
  safari.google_to_prag
124
132
  safari.ala
@@ -129,6 +137,7 @@ begin
129
137
  safari.redsquirrel
130
138
  safari.weinberg
131
139
  safari.tables
140
+ safari.onchange
132
141
  ensure
133
142
  safari.close
134
- end
143
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: safariwatir
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2006-11-22 00:00:00 -06:00
6
+ version: 0.2.1
7
+ date: 2006-12-24 00:00:00 -06:00
8
8
  summary: Automated testing tool for web applications.
9
9
  require_paths:
10
10
  - .