Selenium-Client 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +7 -0
  2. data/lib/selenium-client.rb +1756 -0
  3. metadata +51 -0
data/README ADDED
@@ -0,0 +1,7 @@
1
+ With Ruby installed on your computer....
2
+
3
+ Change directories to ./ruby.
4
+ Run selenium_example.rb
5
+
6
+ Firefox should open, (you may need to add it to your path)
7
+ and ruby will drive the testing with Selenium.
@@ -0,0 +1,1756 @@
1
+
2
+ # Copyright 2006 ThoughtWorks, Inc
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ # -----------------
18
+ # Original code by Aslak Hellesoy and Darren Hobbs
19
+ # This file has been automatically generated via XSL
20
+ # -----------------
21
+
22
+ require 'net/http'
23
+ require 'uri'
24
+ require 'cgi'
25
+
26
+ # Defines an object that runs Selenium commands.
27
+ #
28
+ # ===Element Locators
29
+ # Element Locators tell Selenium which HTML element a command refers to.
30
+ # The format of a locator is:
31
+ # <em>locatorType</em><b>=</b><em>argument</em>
32
+ # We support the following strategies for locating elements:
33
+ #
34
+ # * <b>identifier</b>=<em>id</em>:
35
+ # Select the element with the specified @id attribute. If no match is
36
+ # found, select the first element whose @name attribute is <em>id</em>.
37
+ # (This is normally the default; see below.)
38
+ # * <b>id</b>=<em>id</em>:
39
+ # Select the element with the specified @id attribute.
40
+ # * <b>name</b>=<em>name</em>:
41
+ # Select the first element with the specified @name attribute.
42
+ # * username
43
+ # * name=username
44
+ #
45
+ # The name may optionally be followed by one or more <em>element-filters</em>, separated from the name by whitespace. If the <em>filterType</em> is not specified, <b>value</b> is assumed.
46
+ # * name=flavour value=chocolate
47
+ #
48
+ #
49
+ # * <b>dom</b>=<em>javascriptExpression</em>:
50
+ #
51
+ # Find an element by evaluating the specified string. This allows you to traverse the HTML Document Object
52
+ # Model using JavaScript. Note that you must not return a value in this string; simply make it the last expression in the block.
53
+ # * dom=document.forms['myForm'].myDropdown
54
+ # * dom=document.images[56]
55
+ # * dom=function foo() { return document.links[1]; }; foo();
56
+ #
57
+ #
58
+ # * <b>xpath</b>=<em>xpathExpression</em>:
59
+ # Locate an element using an XPath expression.
60
+ # * xpath=//img[@alt='The image alt text']
61
+ # * xpath=//table[@id='table1']//tr[4]/td[2]
62
+ # * xpath=//a[contains(@href,'#id1')]
63
+ # * xpath=//a[contains(@href,'#id1')]/@class
64
+ # * xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td
65
+ # * xpath=//input[@name='name2' and @value='yes']
66
+ # * xpath=//*[text()="right"]
67
+ #
68
+ #
69
+ # * <b>link</b>=<em>textPattern</em>:
70
+ # Select the link (anchor) element which contains text matching the
71
+ # specified <em>pattern</em>.
72
+ # * link=The link text
73
+ #
74
+ #
75
+ # * <b>css</b>=<em>cssSelectorSyntax</em>:
76
+ # Select the element using css selectors. Please refer to CSS2 selectors, CSS3 selectors for more information. You can also check the TestCssLocators test in the selenium test suite for an example of usage, which is included in the downloaded selenium core package.
77
+ # * css=a[href="#id3"]
78
+ # * css=span#firstChild + span
79
+ #
80
+ # Currently the css selector locator supports all css1, css2 and css3 selectors except namespace in css3, some pseudo classes(:nth-of-type, :nth-last-of-type, :first-of-type, :last-of-type, :only-of-type, :visited, :hover, :active, :focus, :indeterminate) and pseudo elements(::first-line, ::first-letter, ::selection, ::before, ::after).
81
+ #
82
+ #
83
+ #
84
+ # Without an explicit locator prefix, Selenium uses the following default
85
+ # strategies:
86
+ #
87
+ # * <b>dom</b>, for locators starting with "document."
88
+ # * <b>xpath</b>, for locators starting with "//"
89
+ # * <b>identifier</b>, otherwise
90
+ #
91
+ # ===Element FiltersElement filters can be used with a locator to refine a list of candidate elements. They are currently used only in the 'name' element-locator.
92
+ # Filters look much like locators, ie.
93
+ # <em>filterType</em><b>=</b><em>argument</em>Supported element-filters are:
94
+ # <b>value=</b><em>valuePattern</em>
95
+ #
96
+ # Matches elements based on their values. This is particularly useful for refining a list of similarly-named toggle-buttons.<b>index=</b><em>index</em>
97
+ #
98
+ # Selects a single element based on its position in the list (offset from zero).===String-match Patterns
99
+ # Various Pattern syntaxes are available for matching string values:
100
+ #
101
+ # * <b>glob:</b><em>pattern</em>:
102
+ # Match a string against a "glob" (aka "wildmat") pattern. "Glob" is a
103
+ # kind of limited regular-expression syntax typically used in command-line
104
+ # shells. In a glob pattern, "*" represents any sequence of characters, and "?"
105
+ # represents any single character. Glob patterns match against the entire
106
+ # string.
107
+ # * <b>regexp:</b><em>regexp</em>:
108
+ # Match a string using a regular-expression. The full power of JavaScript
109
+ # regular-expressions is available.
110
+ # * <b>regexpi:</b><em>regexpi</em>:
111
+ # Match a string using a case-insensitive regular-expression.
112
+ # * <b>exact:</b><em>string</em>:
113
+ #
114
+ # Match a string exactly, verbatim, without any of that fancy wildcard
115
+ # stuff.
116
+ #
117
+ #
118
+ # If no pattern prefix is specified, Selenium assumes that it's a "glob"
119
+ # pattern.
120
+ #
121
+ #
122
+ # For commands that return multiple values (such as verifySelectOptions),
123
+ # the string being matched is a comma-separated list of the return values,
124
+ # where both commas and backslashes in the values are backslash-escaped.
125
+ # When providing a pattern, the optional matching syntax (i.e. glob,
126
+ # regexp, etc.) is specified once, as usual, at the beginning of the
127
+ # pattern.
128
+ #
129
+ #
130
+ module Selenium
131
+
132
+ class SeleniumDriver
133
+ include Selenium
134
+
135
+ def initialize(server_host, server_port, browserStartCommand, browserURL, timeout=30000)
136
+ @server_host = server_host
137
+ @server_port = server_port
138
+ @browserStartCommand = browserStartCommand
139
+ @browserURL = browserURL
140
+ @timeout = timeout
141
+ end
142
+
143
+ def to_s
144
+ "SeleniumDriver"
145
+ end
146
+
147
+ def start()
148
+ result = get_string("getNewBrowserSession", [@browserStartCommand, @browserURL])
149
+ @session_id = result
150
+ end
151
+
152
+ def session?
153
+ @session_id != nil
154
+ end
155
+
156
+ def session_reset
157
+ @session_id = nil
158
+ end
159
+
160
+ def stop()
161
+ if session?
162
+ do_command("testComplete", [])
163
+ end
164
+ ensure
165
+ session_reset
166
+ end
167
+
168
+ def do_command(verb, args)
169
+ timeout(@timeout) do
170
+ http = Net::HTTP.new(@server_host, @server_port)
171
+ command_string = '/selenium-server/driver/?cmd=' + CGI::escape(verb)
172
+ args.length.times do |i|
173
+ arg_num = (i+1).to_s
174
+ command_string = command_string + "&" + arg_num + "=" + CGI::escape(args[i].to_s)
175
+ end
176
+ if session?
177
+ command_string = command_string + "&sessionId=" + @session_id.to_s
178
+ end
179
+ #print "Requesting --->" + command_string + "\n"
180
+ response = http.get(command_string)
181
+ #print "RESULT: " + response.body + "\n\n"
182
+ if (response.body[0..1] != "OK")
183
+ raise SeleniumCommandError, response.body
184
+ end
185
+ return response.body
186
+ end
187
+ end
188
+
189
+ def get_string(verb, args)
190
+ result = do_command(verb, args)
191
+ return result[3..result.length]
192
+ end
193
+
194
+ def get_string_array(verb, args)
195
+ csv = get_string(verb, args)
196
+ token = ""
197
+ tokens = []
198
+ escape = false
199
+ csv.split(//).each do |letter|
200
+ if escape
201
+ token = token + letter
202
+ escape = false
203
+ next
204
+ end
205
+ if (letter == '\\')
206
+ escape = true
207
+ elsif (letter == ',')
208
+ tokens.push(token)
209
+ token = ""
210
+ else
211
+ token = token + letter
212
+ end
213
+ end
214
+ tokens.push(token)
215
+ return tokens
216
+ end
217
+
218
+ def get_number(verb, args)
219
+ # Is there something I need to do here?
220
+ return get_string(verb, args)
221
+ end
222
+
223
+ def get_number_array(verb, args)
224
+ # Is there something I need to do here?
225
+ return get_string_array(verb, args)
226
+ end
227
+
228
+ def get_boolean(verb, args)
229
+ boolstr = get_string(verb, args)
230
+ if ("true" == boolstr)
231
+ return true
232
+ end
233
+ if ("false" == boolstr)
234
+ return false
235
+ end
236
+ raise ValueError, "result is neither 'true' nor 'false': " + boolstr
237
+ end
238
+
239
+ def get_boolean_array(verb, args)
240
+ boolarr = get_string_array(verb, args)
241
+ boolarr.length.times do |i|
242
+ if ("true" == boolstr)
243
+ boolarr[i] = true
244
+ next
245
+ end
246
+ if ("false" == boolstr)
247
+ boolarr[i] = false
248
+ next
249
+ end
250
+ raise ValueError, "result is neither 'true' nor 'false': " + boolarr[i]
251
+ end
252
+ return boolarr
253
+ end
254
+
255
+
256
+
257
+ # Clicks on a link, button, checkbox or radio button. If the click action
258
+ # causes a new page to load (like a link usually does), call
259
+ # waitForPageToLoad.
260
+ #
261
+ # 'locator' is an element locator
262
+ def click(locator)
263
+ do_command("click", [locator,])
264
+ end
265
+
266
+
267
+ # Double clicks on a link, button, checkbox or radio button. If the double click action
268
+ # causes a new page to load (like a link usually does), call
269
+ # waitForPageToLoad.
270
+ #
271
+ # 'locator' is an element locator
272
+ def double_click(locator)
273
+ do_command("doubleClick", [locator,])
274
+ end
275
+
276
+
277
+ # Simulates opening the context menu for the specified element (as might happen if the user "right-clicked" on the element).
278
+ #
279
+ # 'locator' is an element locator
280
+ def context_menu(locator)
281
+ do_command("contextMenu", [locator,])
282
+ end
283
+
284
+
285
+ # Clicks on a link, button, checkbox or radio button. If the click action
286
+ # causes a new page to load (like a link usually does), call
287
+ # waitForPageToLoad.
288
+ #
289
+ # 'locator' is an element locator
290
+ # 'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse event relative to the element returned by the locator.
291
+ def click_at(locator,coordString)
292
+ do_command("clickAt", [locator,coordString,])
293
+ end
294
+
295
+
296
+ # Doubleclicks on a link, button, checkbox or radio button. If the action
297
+ # causes a new page to load (like a link usually does), call
298
+ # waitForPageToLoad.
299
+ #
300
+ # 'locator' is an element locator
301
+ # 'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse event relative to the element returned by the locator.
302
+ def double_click_at(locator,coordString)
303
+ do_command("doubleClickAt", [locator,coordString,])
304
+ end
305
+
306
+
307
+ # Simulates opening the context menu for the specified element (as might happen if the user "right-clicked" on the element).
308
+ #
309
+ # 'locator' is an element locator
310
+ # 'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse event relative to the element returned by the locator.
311
+ def context_menu_at(locator,coordString)
312
+ do_command("contextMenuAt", [locator,coordString,])
313
+ end
314
+
315
+
316
+ # Explicitly simulate an event, to trigger the corresponding "on<em>event</em>"
317
+ # handler.
318
+ #
319
+ # 'locator' is an element locator
320
+ # 'eventName' is the event name, e.g. "focus" or "blur"
321
+ def fire_event(locator,eventName)
322
+ do_command("fireEvent", [locator,eventName,])
323
+ end
324
+
325
+
326
+ # Move the focus to the specified element; for example, if the element is an input field, move the cursor to that field.
327
+ #
328
+ # 'locator' is an element locator
329
+ def focus(locator)
330
+ do_command("focus", [locator,])
331
+ end
332
+
333
+
334
+ # Simulates a user pressing and releasing a key.
335
+ #
336
+ # 'locator' is an element locator
337
+ # 'keySequence' is Either be a string("\" followed by the numeric keycode of the key to be pressed, normally the ASCII value of that key), or a single character. For example: "w", "\119".
338
+ def key_press(locator,keySequence)
339
+ do_command("keyPress", [locator,keySequence,])
340
+ end
341
+
342
+
343
+ # Press the shift key and hold it down until doShiftUp() is called or a new page is loaded.
344
+ #
345
+ def shift_key_down()
346
+ do_command("shiftKeyDown", [])
347
+ end
348
+
349
+
350
+ # Release the shift key.
351
+ #
352
+ def shift_key_up()
353
+ do_command("shiftKeyUp", [])
354
+ end
355
+
356
+
357
+ # Press the meta key and hold it down until doMetaUp() is called or a new page is loaded.
358
+ #
359
+ def meta_key_down()
360
+ do_command("metaKeyDown", [])
361
+ end
362
+
363
+
364
+ # Release the meta key.
365
+ #
366
+ def meta_key_up()
367
+ do_command("metaKeyUp", [])
368
+ end
369
+
370
+
371
+ # Press the alt key and hold it down until doAltUp() is called or a new page is loaded.
372
+ #
373
+ def alt_key_down()
374
+ do_command("altKeyDown", [])
375
+ end
376
+
377
+
378
+ # Release the alt key.
379
+ #
380
+ def alt_key_up()
381
+ do_command("altKeyUp", [])
382
+ end
383
+
384
+
385
+ # Press the control key and hold it down until doControlUp() is called or a new page is loaded.
386
+ #
387
+ def control_key_down()
388
+ do_command("controlKeyDown", [])
389
+ end
390
+
391
+
392
+ # Release the control key.
393
+ #
394
+ def control_key_up()
395
+ do_command("controlKeyUp", [])
396
+ end
397
+
398
+
399
+ # Simulates a user pressing a key (without releasing it yet).
400
+ #
401
+ # 'locator' is an element locator
402
+ # 'keySequence' is Either be a string("\" followed by the numeric keycode of the key to be pressed, normally the ASCII value of that key), or a single character. For example: "w", "\119".
403
+ def key_down(locator,keySequence)
404
+ do_command("keyDown", [locator,keySequence,])
405
+ end
406
+
407
+
408
+ # Simulates a user releasing a key.
409
+ #
410
+ # 'locator' is an element locator
411
+ # 'keySequence' is Either be a string("\" followed by the numeric keycode of the key to be pressed, normally the ASCII value of that key), or a single character. For example: "w", "\119".
412
+ def key_up(locator,keySequence)
413
+ do_command("keyUp", [locator,keySequence,])
414
+ end
415
+
416
+
417
+ # Simulates a user hovering a mouse over the specified element.
418
+ #
419
+ # 'locator' is an element locator
420
+ def mouse_over(locator)
421
+ do_command("mouseOver", [locator,])
422
+ end
423
+
424
+
425
+ # Simulates a user moving the mouse pointer away from the specified element.
426
+ #
427
+ # 'locator' is an element locator
428
+ def mouse_out(locator)
429
+ do_command("mouseOut", [locator,])
430
+ end
431
+
432
+
433
+ # Simulates a user pressing the left mouse button (without releasing it yet) on
434
+ # the specified element.
435
+ #
436
+ # 'locator' is an element locator
437
+ def mouse_down(locator)
438
+ do_command("mouseDown", [locator,])
439
+ end
440
+
441
+
442
+ # Simulates a user pressing the right mouse button (without releasing it yet) on
443
+ # the specified element.
444
+ #
445
+ # 'locator' is an element locator
446
+ def mouse_down_right(locator)
447
+ do_command("mouseDownRight", [locator,])
448
+ end
449
+
450
+
451
+ # Simulates a user pressing the left mouse button (without releasing it yet) at
452
+ # the specified location.
453
+ #
454
+ # 'locator' is an element locator
455
+ # 'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse event relative to the element returned by the locator.
456
+ def mouse_down_at(locator,coordString)
457
+ do_command("mouseDownAt", [locator,coordString,])
458
+ end
459
+
460
+
461
+ # Simulates a user pressing the right mouse button (without releasing it yet) at
462
+ # the specified location.
463
+ #
464
+ # 'locator' is an element locator
465
+ # 'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse event relative to the element returned by the locator.
466
+ def mouse_down_right_at(locator,coordString)
467
+ do_command("mouseDownRightAt", [locator,coordString,])
468
+ end
469
+
470
+
471
+ # Simulates the event that occurs when the user releases the mouse button (i.e., stops
472
+ # holding the button down) on the specified element.
473
+ #
474
+ # 'locator' is an element locator
475
+ def mouse_up(locator)
476
+ do_command("mouseUp", [locator,])
477
+ end
478
+
479
+
480
+ # Simulates the event that occurs when the user releases the right mouse button (i.e., stops
481
+ # holding the button down) on the specified element.
482
+ #
483
+ # 'locator' is an element locator
484
+ def mouse_up_right(locator)
485
+ do_command("mouseUpRight", [locator,])
486
+ end
487
+
488
+
489
+ # Simulates the event that occurs when the user releases the mouse button (i.e., stops
490
+ # holding the button down) at the specified location.
491
+ #
492
+ # 'locator' is an element locator
493
+ # 'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse event relative to the element returned by the locator.
494
+ def mouse_up_at(locator,coordString)
495
+ do_command("mouseUpAt", [locator,coordString,])
496
+ end
497
+
498
+
499
+ # Simulates the event that occurs when the user releases the right mouse button (i.e., stops
500
+ # holding the button down) at the specified location.
501
+ #
502
+ # 'locator' is an element locator
503
+ # 'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse event relative to the element returned by the locator.
504
+ def mouse_up_right_at(locator,coordString)
505
+ do_command("mouseUpRightAt", [locator,coordString,])
506
+ end
507
+
508
+
509
+ # Simulates a user pressing the mouse button (without releasing it yet) on
510
+ # the specified element.
511
+ #
512
+ # 'locator' is an element locator
513
+ def mouse_move(locator)
514
+ do_command("mouseMove", [locator,])
515
+ end
516
+
517
+
518
+ # Simulates a user pressing the mouse button (without releasing it yet) on
519
+ # the specified element.
520
+ #
521
+ # 'locator' is an element locator
522
+ # 'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse event relative to the element returned by the locator.
523
+ def mouse_move_at(locator,coordString)
524
+ do_command("mouseMoveAt", [locator,coordString,])
525
+ end
526
+
527
+
528
+ # Sets the value of an input field, as though you typed it in.
529
+ #
530
+ # Can also be used to set the value of combo boxes, check boxes, etc. In these cases,
531
+ # value should be the value of the option selected, not the visible text.
532
+ #
533
+ #
534
+ # 'locator' is an element locator
535
+ # 'value' is the value to type
536
+ def type(locator,value)
537
+ do_command("type", [locator,value,])
538
+ end
539
+
540
+
541
+ # Simulates keystroke events on the specified element, as though you typed the value key-by-key.
542
+ #
543
+ # This is a convenience method for calling keyDown, keyUp, keyPress for every character in the specified string;
544
+ # this is useful for dynamic UI widgets (like auto-completing combo boxes) that require explicit key events.
545
+ # Unlike the simple "type" command, which forces the specified value into the page directly, this command
546
+ # may or may not have any visible effect, even in cases where typing keys would normally have a visible effect.
547
+ # For example, if you use "typeKeys" on a form element, you may or may not see the results of what you typed in
548
+ # the field.
549
+ # In some cases, you may need to use the simple "type" command to set the value of the field and then the "typeKeys" command to
550
+ # send the keystroke events corresponding to what you just typed.
551
+ #
552
+ #
553
+ # 'locator' is an element locator
554
+ # 'value' is the value to type
555
+ def type_keys(locator,value)
556
+ do_command("typeKeys", [locator,value,])
557
+ end
558
+
559
+
560
+ # Set execution speed (i.e., set the millisecond length of a delay which will follow each selenium operation). By default, there is no such delay, i.e.,
561
+ # the delay is 0 milliseconds.
562
+ #
563
+ # 'value' is the number of milliseconds to pause after operation
564
+ def set_speed(value)
565
+ do_command("setSpeed", [value,])
566
+ end
567
+
568
+
569
+ # Get execution speed (i.e., get the millisecond length of the delay following each selenium operation). By default, there is no such delay, i.e.,
570
+ # the delay is 0 milliseconds.
571
+ #
572
+ # See also setSpeed.
573
+ #
574
+ def get_speed()
575
+ return get_string("getSpeed", [])
576
+ end
577
+
578
+
579
+ # Check a toggle-button (checkbox/radio)
580
+ #
581
+ # 'locator' is an element locator
582
+ def check(locator)
583
+ do_command("check", [locator,])
584
+ end
585
+
586
+
587
+ # Uncheck a toggle-button (checkbox/radio)
588
+ #
589
+ # 'locator' is an element locator
590
+ def uncheck(locator)
591
+ do_command("uncheck", [locator,])
592
+ end
593
+
594
+
595
+ # Select an option from a drop-down using an option locator.
596
+ #
597
+ #
598
+ # Option locators provide different ways of specifying options of an HTML
599
+ # Select element (e.g. for selecting a specific option, or for asserting
600
+ # that the selected option satisfies a specification). There are several
601
+ # forms of Select Option Locator.
602
+ #
603
+ # * <b>label</b>=<em>labelPattern</em>:
604
+ # matches options based on their labels, i.e. the visible text. (This
605
+ # is the default.)
606
+ # * label=regexp:^[Oo]ther
607
+ #
608
+ #
609
+ # * <b>value</b>=<em>valuePattern</em>:
610
+ # matches options based on their values.
611
+ # * value=other
612
+ #
613
+ #
614
+ # * <b>id</b>=<em>id</em>:
615
+ #
616
+ # matches options based on their ids.
617
+ # * id=option1
618
+ #
619
+ #
620
+ # * <b>index</b>=<em>index</em>:
621
+ # matches an option based on its index (offset from zero).
622
+ # * index=2
623
+ #
624
+ #
625
+ #
626
+ #
627
+ # If no option locator prefix is provided, the default behaviour is to match on <b>label</b>.
628
+ #
629
+ #
630
+ #
631
+ # 'selectLocator' is an element locator identifying a drop-down menu
632
+ # 'optionLocator' is an option locator (a label by default)
633
+ def select(selectLocator,optionLocator)
634
+ do_command("select", [selectLocator,optionLocator,])
635
+ end
636
+
637
+
638
+ # Add a selection to the set of selected options in a multi-select element using an option locator.
639
+ #
640
+ # @see #doSelect for details of option locators
641
+ #
642
+ # 'locator' is an element locator identifying a multi-select box
643
+ # 'optionLocator' is an option locator (a label by default)
644
+ def add_selection(locator,optionLocator)
645
+ do_command("addSelection", [locator,optionLocator,])
646
+ end
647
+
648
+
649
+ # Remove a selection from the set of selected options in a multi-select element using an option locator.
650
+ #
651
+ # @see #doSelect for details of option locators
652
+ #
653
+ # 'locator' is an element locator identifying a multi-select box
654
+ # 'optionLocator' is an option locator (a label by default)
655
+ def remove_selection(locator,optionLocator)
656
+ do_command("removeSelection", [locator,optionLocator,])
657
+ end
658
+
659
+
660
+ # Unselects all of the selected options in a multi-select element.
661
+ #
662
+ # 'locator' is an element locator identifying a multi-select box
663
+ def remove_all_selections(locator)
664
+ do_command("removeAllSelections", [locator,])
665
+ end
666
+
667
+
668
+ # Submit the specified form. This is particularly useful for forms without
669
+ # submit buttons, e.g. single-input "Search" forms.
670
+ #
671
+ # 'formLocator' is an element locator for the form you want to submit
672
+ def submit(formLocator)
673
+ do_command("submit", [formLocator,])
674
+ end
675
+
676
+
677
+ # Opens an URL in the test frame. This accepts both relative and absolute
678
+ # URLs.
679
+ #
680
+ # The "open" command waits for the page to load before proceeding,
681
+ # ie. the "AndWait" suffix is implicit.
682
+ #
683
+ # <em>Note</em>: The URL must be on the same domain as the runner HTML
684
+ # due to security restrictions in the browser (Same Origin Policy). If you
685
+ # need to open an URL on another domain, use the Selenium Server to start a
686
+ # new browser session on that domain.
687
+ #
688
+ # 'url' is the URL to open; may be relative or absolute
689
+ def open(url)
690
+ do_command("open", [url,])
691
+ end
692
+
693
+
694
+ # Opens a popup window (if a window with that ID isn't already open).
695
+ # After opening the window, you'll need to select it using the selectWindow
696
+ # command.
697
+ #
698
+ # This command can also be a useful workaround for bug SEL-339. In some cases, Selenium will be unable to intercept a call to window.open (if the call occurs during or before the "onLoad" event, for example).
699
+ # In those cases, you can force Selenium to notice the open window's name by using the Selenium openWindow command, using
700
+ # an empty (blank) url, like this: openWindow("", "myFunnyWindow").
701
+ #
702
+ #
703
+ # 'url' is the URL to open, which can be blank
704
+ # 'windowID' is the JavaScript window ID of the window to select
705
+ def open_window(url,windowID)
706
+ do_command("openWindow", [url,windowID,])
707
+ end
708
+
709
+
710
+ # Selects a popup window using a window locator; once a popup window has been selected, all
711
+ # commands go to that window. To select the main window again, use null
712
+ # as the target.
713
+ #
714
+ #
715
+ #
716
+ # Window locators provide different ways of specifying the window object:
717
+ # by title, by internal JavaScript "name," or by JavaScript variable.
718
+ #
719
+ # * <b>title</b>=<em>My Special Window</em>:
720
+ # Finds the window using the text that appears in the title bar. Be careful;
721
+ # two windows can share the same title. If that happens, this locator will
722
+ # just pick one.
723
+ #
724
+ # * <b>name</b>=<em>myWindow</em>:
725
+ # Finds the window using its internal JavaScript "name" property. This is the second
726
+ # parameter "windowName" passed to the JavaScript method window.open(url, windowName, windowFeatures, replaceFlag)
727
+ # (which Selenium intercepts).
728
+ #
729
+ # * <b>var</b>=<em>variableName</em>:
730
+ # Some pop-up windows are unnamed (anonymous), but are associated with a JavaScript variable name in the current
731
+ # application window, e.g. "window.foo = window.open(url);". In those cases, you can open the window using
732
+ # "var=foo".
733
+ #
734
+ #
735
+ #
736
+ # If no window locator prefix is provided, we'll try to guess what you mean like this:
737
+ # 1.) if windowID is null, (or the string "null") then it is assumed the user is referring to the original window instantiated by the browser).
738
+ # 2.) if the value of the "windowID" parameter is a JavaScript variable name in the current application window, then it is assumed
739
+ # that this variable contains the return value from a call to the JavaScript window.open() method.
740
+ # 3.) Otherwise, selenium looks in a hash it maintains that maps string names to window "names".
741
+ # 4.) If <em>that</em> fails, we'll try looping over all of the known windows to try to find the appropriate "title".
742
+ # Since "title" is not necessarily unique, this may have unexpected behavior.
743
+ # If you're having trouble figuring out the name of a window that you want to manipulate, look at the Selenium log messages
744
+ # which identify the names of windows created via window.open (and therefore intercepted by Selenium). You will see messages
745
+ # like the following for each window as it is opened:
746
+ # <tt>debug: window.open call intercepted; window ID (which you can use with selectWindow()) is "myNewWindow"</tt>
747
+ # In some cases, Selenium will be unable to intercept a call to window.open (if the call occurs during or before the "onLoad" event, for example).
748
+ # (This is bug SEL-339.) In those cases, you can force Selenium to notice the open window's name by using the Selenium openWindow command, using
749
+ # an empty (blank) url, like this: openWindow("", "myFunnyWindow").
750
+ #
751
+ #
752
+ # 'windowID' is the JavaScript window ID of the window to select
753
+ def select_window(windowID)
754
+ do_command("selectWindow", [windowID,])
755
+ end
756
+
757
+
758
+ # Selects a frame within the current window. (You may invoke this command
759
+ # multiple times to select nested frames.) To select the parent frame, use
760
+ # "relative=parent" as a locator; to select the top frame, use "relative=top".
761
+ # You can also select a frame by its 0-based index number; select the first frame with
762
+ # "index=0", or the third frame with "index=2".
763
+ #
764
+ # You may also use a DOM expression to identify the frame you want directly,
765
+ # like this: <tt>dom=frames["main"].frames["subframe"]</tt>
766
+ #
767
+ #
768
+ # 'locator' is an element locator identifying a frame or iframe
769
+ def select_frame(locator)
770
+ do_command("selectFrame", [locator,])
771
+ end
772
+
773
+
774
+ # Determine whether current/locator identify the frame containing this running code.
775
+ #
776
+ # This is useful in proxy injection mode, where this code runs in every
777
+ # browser frame and window, and sometimes the selenium server needs to identify
778
+ # the "current" frame. In this case, when the test calls selectFrame, this
779
+ # routine is called for each frame to figure out which one has been selected.
780
+ # The selected frame will return true, while all others will return false.
781
+ #
782
+ #
783
+ # 'currentFrameString' is starting frame
784
+ # 'target' is new frame (which might be relative to the current one)
785
+ def get_whether_this_frame_match_frame_expression(currentFrameString,target)
786
+ return get_boolean("getWhetherThisFrameMatchFrameExpression", [currentFrameString,target,])
787
+ end
788
+
789
+
790
+ # Determine whether currentWindowString plus target identify the window containing this running code.
791
+ #
792
+ # This is useful in proxy injection mode, where this code runs in every
793
+ # browser frame and window, and sometimes the selenium server needs to identify
794
+ # the "current" window. In this case, when the test calls selectWindow, this
795
+ # routine is called for each window to figure out which one has been selected.
796
+ # The selected window will return true, while all others will return false.
797
+ #
798
+ #
799
+ # 'currentWindowString' is starting window
800
+ # 'target' is new window (which might be relative to the current one, e.g., "_parent")
801
+ def get_whether_this_window_match_window_expression(currentWindowString,target)
802
+ return get_boolean("getWhetherThisWindowMatchWindowExpression", [currentWindowString,target,])
803
+ end
804
+
805
+
806
+ # Waits for a popup window to appear and load up.
807
+ #
808
+ # 'windowID' is the JavaScript window "name" of the window that will appear (not the text of the title bar)
809
+ # 'timeout' is a timeout in milliseconds, after which the action will return with an error
810
+ def wait_for_pop_up(windowID,timeout)
811
+ do_command("waitForPopUp", [windowID,timeout,])
812
+ end
813
+
814
+
815
+ #
816
+ # By default, Selenium's overridden window.confirm() function will
817
+ # return true, as if the user had manually clicked OK; after running
818
+ # this command, the next call to confirm() will return false, as if
819
+ # the user had clicked Cancel. Selenium will then resume using the
820
+ # default behavior for future confirmations, automatically returning
821
+ # true (OK) unless/until you explicitly call this command for each
822
+ # confirmation.
823
+ #
824
+ #
825
+ # Take note - every time a confirmation comes up, you must
826
+ # consume it with a corresponding getConfirmation, or else
827
+ # the next selenium operation will fail.
828
+ #
829
+ #
830
+ #
831
+ def choose_cancel_on_next_confirmation()
832
+ do_command("chooseCancelOnNextConfirmation", [])
833
+ end
834
+
835
+
836
+ #
837
+ # Undo the effect of calling chooseCancelOnNextConfirmation. Note
838
+ # that Selenium's overridden window.confirm() function will normally automatically
839
+ # return true, as if the user had manually clicked OK, so you shouldn't
840
+ # need to use this command unless for some reason you need to change
841
+ # your mind prior to the next confirmation. After any confirmation, Selenium will resume using the
842
+ # default behavior for future confirmations, automatically returning
843
+ # true (OK) unless/until you explicitly call chooseCancelOnNextConfirmation for each
844
+ # confirmation.
845
+ #
846
+ #
847
+ # Take note - every time a confirmation comes up, you must
848
+ # consume it with a corresponding getConfirmation, or else
849
+ # the next selenium operation will fail.
850
+ #
851
+ #
852
+ #
853
+ def choose_ok_on_next_confirmation()
854
+ do_command("chooseOkOnNextConfirmation", [])
855
+ end
856
+
857
+
858
+ # Instructs Selenium to return the specified answer string in response to
859
+ # the next JavaScript prompt [window.prompt()].
860
+ #
861
+ # 'answer' is the answer to give in response to the prompt pop-up
862
+ def answer_on_next_prompt(answer)
863
+ do_command("answerOnNextPrompt", [answer,])
864
+ end
865
+
866
+
867
+ # Simulates the user clicking the "back" button on their browser.
868
+ #
869
+ def go_back()
870
+ do_command("goBack", [])
871
+ end
872
+
873
+
874
+ # Simulates the user clicking the "Refresh" button on their browser.
875
+ #
876
+ def refresh()
877
+ do_command("refresh", [])
878
+ end
879
+
880
+
881
+ # Simulates the user clicking the "close" button in the titlebar of a popup
882
+ # window or tab.
883
+ #
884
+ def close()
885
+ do_command("close", [])
886
+ end
887
+
888
+
889
+ # Has an alert occurred?
890
+ #
891
+ #
892
+ # This function never throws an exception
893
+ #
894
+ #
895
+ #
896
+ def is_alert_present()
897
+ return get_boolean("isAlertPresent", [])
898
+ end
899
+
900
+
901
+ # Has a prompt occurred?
902
+ #
903
+ #
904
+ # This function never throws an exception
905
+ #
906
+ #
907
+ #
908
+ def is_prompt_present()
909
+ return get_boolean("isPromptPresent", [])
910
+ end
911
+
912
+
913
+ # Has confirm() been called?
914
+ #
915
+ #
916
+ # This function never throws an exception
917
+ #
918
+ #
919
+ #
920
+ def is_confirmation_present()
921
+ return get_boolean("isConfirmationPresent", [])
922
+ end
923
+
924
+
925
+ # Retrieves the message of a JavaScript alert generated during the previous action, or fail if there were no alerts.
926
+ #
927
+ # Getting an alert has the same effect as manually clicking OK. If an
928
+ # alert is generated but you do not consume it with getAlert, the next Selenium action
929
+ # will fail.
930
+ # Under Selenium, JavaScript alerts will NOT pop up a visible alert
931
+ # dialog.
932
+ # Selenium does NOT support JavaScript alerts that are generated in a
933
+ # page's onload() event handler. In this case a visible dialog WILL be
934
+ # generated and Selenium will hang until someone manually clicks OK.
935
+ #
936
+ #
937
+ def get_alert()
938
+ return get_string("getAlert", [])
939
+ end
940
+
941
+
942
+ # Retrieves the message of a JavaScript confirmation dialog generated during
943
+ # the previous action.
944
+ #
945
+ #
946
+ # By default, the confirm function will return true, having the same effect
947
+ # as manually clicking OK. This can be changed by prior execution of the
948
+ # chooseCancelOnNextConfirmation command.
949
+ #
950
+ #
951
+ # If an confirmation is generated but you do not consume it with getConfirmation,
952
+ # the next Selenium action will fail.
953
+ #
954
+ #
955
+ # NOTE: under Selenium, JavaScript confirmations will NOT pop up a visible
956
+ # dialog.
957
+ #
958
+ #
959
+ # NOTE: Selenium does NOT support JavaScript confirmations that are
960
+ # generated in a page's onload() event handler. In this case a visible
961
+ # dialog WILL be generated and Selenium will hang until you manually click
962
+ # OK.
963
+ #
964
+ #
965
+ #
966
+ def get_confirmation()
967
+ return get_string("getConfirmation", [])
968
+ end
969
+
970
+
971
+ # Retrieves the message of a JavaScript question prompt dialog generated during
972
+ # the previous action.
973
+ #
974
+ # Successful handling of the prompt requires prior execution of the
975
+ # answerOnNextPrompt command. If a prompt is generated but you
976
+ # do not get/verify it, the next Selenium action will fail.
977
+ # NOTE: under Selenium, JavaScript prompts will NOT pop up a visible
978
+ # dialog.
979
+ # NOTE: Selenium does NOT support JavaScript prompts that are generated in a
980
+ # page's onload() event handler. In this case a visible dialog WILL be
981
+ # generated and Selenium will hang until someone manually clicks OK.
982
+ #
983
+ #
984
+ def get_prompt()
985
+ return get_string("getPrompt", [])
986
+ end
987
+
988
+
989
+ # Gets the absolute URL of the current page.
990
+ #
991
+ def get_location()
992
+ return get_string("getLocation", [])
993
+ end
994
+
995
+
996
+ # Gets the title of the current page.
997
+ #
998
+ def get_title()
999
+ return get_string("getTitle", [])
1000
+ end
1001
+
1002
+
1003
+ # Gets the entire text of the page.
1004
+ #
1005
+ def get_body_text()
1006
+ return get_string("getBodyText", [])
1007
+ end
1008
+
1009
+
1010
+ # Gets the (whitespace-trimmed) value of an input field (or anything else with a value parameter).
1011
+ # For checkbox/radio elements, the value will be "on" or "off" depending on
1012
+ # whether the element is checked or not.
1013
+ #
1014
+ # 'locator' is an element locator
1015
+ def get_value(locator)
1016
+ return get_string("getValue", [locator,])
1017
+ end
1018
+
1019
+
1020
+ # Gets the text of an element. This works for any element that contains
1021
+ # text. This command uses either the textContent (Mozilla-like browsers) or
1022
+ # the innerText (IE-like browsers) of the element, which is the rendered
1023
+ # text shown to the user.
1024
+ #
1025
+ # 'locator' is an element locator
1026
+ def get_text(locator)
1027
+ return get_string("getText", [locator,])
1028
+ end
1029
+
1030
+
1031
+ # Briefly changes the backgroundColor of the specified element yellow. Useful for debugging.
1032
+ #
1033
+ # 'locator' is an element locator
1034
+ def highlight(locator)
1035
+ do_command("highlight", [locator,])
1036
+ end
1037
+
1038
+
1039
+ # Gets the result of evaluating the specified JavaScript snippet. The snippet may
1040
+ # have multiple lines, but only the result of the last line will be returned.
1041
+ #
1042
+ # Note that, by default, the snippet will run in the context of the "selenium"
1043
+ # object itself, so <tt>this</tt> will refer to the Selenium object. Use <tt>window</tt> to
1044
+ # refer to the window of your application, e.g. <tt>window.document.getElementById('foo')</tt>
1045
+ # If you need to use
1046
+ # a locator to refer to a single element in your application page, you can
1047
+ # use <tt>this.browserbot.findElement("id=foo")</tt> where "id=foo" is your locator.
1048
+ #
1049
+ #
1050
+ # 'script' is the JavaScript snippet to run
1051
+ def get_eval(script)
1052
+ return get_string("getEval", [script,])
1053
+ end
1054
+
1055
+
1056
+ # Gets whether a toggle-button (checkbox/radio) is checked. Fails if the specified element doesn't exist or isn't a toggle-button.
1057
+ #
1058
+ # 'locator' is an element locator pointing to a checkbox or radio button
1059
+ def is_checked(locator)
1060
+ return get_boolean("isChecked", [locator,])
1061
+ end
1062
+
1063
+
1064
+ # Gets the text from a cell of a table. The cellAddress syntax
1065
+ # tableLocator.row.column, where row and column start at 0.
1066
+ #
1067
+ # 'tableCellAddress' is a cell address, e.g. "foo.1.4"
1068
+ def get_table(tableCellAddress)
1069
+ return get_string("getTable", [tableCellAddress,])
1070
+ end
1071
+
1072
+
1073
+ # Gets all option labels (visible text) for selected options in the specified select or multi-select element.
1074
+ #
1075
+ # 'selectLocator' is an element locator identifying a drop-down menu
1076
+ def get_selected_labels(selectLocator)
1077
+ return get_string_array("getSelectedLabels", [selectLocator,])
1078
+ end
1079
+
1080
+
1081
+ # Gets option label (visible text) for selected option in the specified select element.
1082
+ #
1083
+ # 'selectLocator' is an element locator identifying a drop-down menu
1084
+ def get_selected_label(selectLocator)
1085
+ return get_string("getSelectedLabel", [selectLocator,])
1086
+ end
1087
+
1088
+
1089
+ # Gets all option values (value attributes) for selected options in the specified select or multi-select element.
1090
+ #
1091
+ # 'selectLocator' is an element locator identifying a drop-down menu
1092
+ def get_selected_values(selectLocator)
1093
+ return get_string_array("getSelectedValues", [selectLocator,])
1094
+ end
1095
+
1096
+
1097
+ # Gets option value (value attribute) for selected option in the specified select element.
1098
+ #
1099
+ # 'selectLocator' is an element locator identifying a drop-down menu
1100
+ def get_selected_value(selectLocator)
1101
+ return get_string("getSelectedValue", [selectLocator,])
1102
+ end
1103
+
1104
+
1105
+ # Gets all option indexes (option number, starting at 0) for selected options in the specified select or multi-select element.
1106
+ #
1107
+ # 'selectLocator' is an element locator identifying a drop-down menu
1108
+ def get_selected_indexes(selectLocator)
1109
+ return get_string_array("getSelectedIndexes", [selectLocator,])
1110
+ end
1111
+
1112
+
1113
+ # Gets option index (option number, starting at 0) for selected option in the specified select element.
1114
+ #
1115
+ # 'selectLocator' is an element locator identifying a drop-down menu
1116
+ def get_selected_index(selectLocator)
1117
+ return get_string("getSelectedIndex", [selectLocator,])
1118
+ end
1119
+
1120
+
1121
+ # Gets all option element IDs for selected options in the specified select or multi-select element.
1122
+ #
1123
+ # 'selectLocator' is an element locator identifying a drop-down menu
1124
+ def get_selected_ids(selectLocator)
1125
+ return get_string_array("getSelectedIds", [selectLocator,])
1126
+ end
1127
+
1128
+
1129
+ # Gets option element ID for selected option in the specified select element.
1130
+ #
1131
+ # 'selectLocator' is an element locator identifying a drop-down menu
1132
+ def get_selected_id(selectLocator)
1133
+ return get_string("getSelectedId", [selectLocator,])
1134
+ end
1135
+
1136
+
1137
+ # Determines whether some option in a drop-down menu is selected.
1138
+ #
1139
+ # 'selectLocator' is an element locator identifying a drop-down menu
1140
+ def is_something_selected(selectLocator)
1141
+ return get_boolean("isSomethingSelected", [selectLocator,])
1142
+ end
1143
+
1144
+
1145
+ # Gets all option labels in the specified select drop-down.
1146
+ #
1147
+ # 'selectLocator' is an element locator identifying a drop-down menu
1148
+ def get_select_options(selectLocator)
1149
+ return get_string_array("getSelectOptions", [selectLocator,])
1150
+ end
1151
+
1152
+
1153
+ # Gets the value of an element attribute. The value of the attribute may
1154
+ # differ across browsers (this is the case for the "style" attribute, for
1155
+ # example).
1156
+ #
1157
+ # 'attributeLocator' is an element locator followed by an @ sign and then the name of the attribute, e.g. "foo@bar"
1158
+ def get_attribute(attributeLocator)
1159
+ return get_string("getAttribute", [attributeLocator,])
1160
+ end
1161
+
1162
+
1163
+ # Verifies that the specified text pattern appears somewhere on the rendered page shown to the user.
1164
+ #
1165
+ # 'pattern' is a pattern to match with the text of the page
1166
+ def is_text_present(pattern)
1167
+ return get_boolean("isTextPresent", [pattern,])
1168
+ end
1169
+
1170
+
1171
+ # Verifies that the specified element is somewhere on the page.
1172
+ #
1173
+ # 'locator' is an element locator
1174
+ def is_element_present(locator)
1175
+ return get_boolean("isElementPresent", [locator,])
1176
+ end
1177
+
1178
+
1179
+ # Determines if the specified element is visible. An
1180
+ # element can be rendered invisible by setting the CSS "visibility"
1181
+ # property to "hidden", or the "display" property to "none", either for the
1182
+ # element itself or one if its ancestors. This method will fail if
1183
+ # the element is not present.
1184
+ #
1185
+ # 'locator' is an element locator
1186
+ def is_visible(locator)
1187
+ return get_boolean("isVisible", [locator,])
1188
+ end
1189
+
1190
+
1191
+ # Determines whether the specified input element is editable, ie hasn't been disabled.
1192
+ # This method will fail if the specified element isn't an input element.
1193
+ #
1194
+ # 'locator' is an element locator
1195
+ def is_editable(locator)
1196
+ return get_boolean("isEditable", [locator,])
1197
+ end
1198
+
1199
+
1200
+ # Returns the IDs of all buttons on the page.
1201
+ #
1202
+ # If a given button has no ID, it will appear as "" in this array.
1203
+ #
1204
+ #
1205
+ def get_all_buttons()
1206
+ return get_string_array("getAllButtons", [])
1207
+ end
1208
+
1209
+
1210
+ # Returns the IDs of all links on the page.
1211
+ #
1212
+ # If a given link has no ID, it will appear as "" in this array.
1213
+ #
1214
+ #
1215
+ def get_all_links()
1216
+ return get_string_array("getAllLinks", [])
1217
+ end
1218
+
1219
+
1220
+ # Returns the IDs of all input fields on the page.
1221
+ #
1222
+ # If a given field has no ID, it will appear as "" in this array.
1223
+ #
1224
+ #
1225
+ def get_all_fields()
1226
+ return get_string_array("getAllFields", [])
1227
+ end
1228
+
1229
+
1230
+ # Returns every instance of some attribute from all known windows.
1231
+ #
1232
+ # 'attributeName' is name of an attribute on the windows
1233
+ def get_attribute_from_all_windows(attributeName)
1234
+ return get_string_array("getAttributeFromAllWindows", [attributeName,])
1235
+ end
1236
+
1237
+
1238
+ # deprecated - use dragAndDrop instead
1239
+ #
1240
+ # 'locator' is an element locator
1241
+ # 'movementsString' is offset in pixels from the current location to which the element should be moved, e.g., "+70,-300"
1242
+ def dragdrop(locator,movementsString)
1243
+ do_command("dragdrop", [locator,movementsString,])
1244
+ end
1245
+
1246
+
1247
+ # Configure the number of pixels between "mousemove" events during dragAndDrop commands (default=10).
1248
+ # Setting this value to 0 means that we'll send a "mousemove" event to every single pixel
1249
+ # in between the start location and the end location; that can be very slow, and may
1250
+ # cause some browsers to force the JavaScript to timeout.
1251
+ # If the mouse speed is greater than the distance between the two dragged objects, we'll
1252
+ # just send one "mousemove" at the start location and then one final one at the end location.
1253
+ #
1254
+ #
1255
+ # 'pixels' is the number of pixels between "mousemove" events
1256
+ def set_mouse_speed(pixels)
1257
+ do_command("setMouseSpeed", [pixels,])
1258
+ end
1259
+
1260
+
1261
+ # Returns the number of pixels between "mousemove" events during dragAndDrop commands (default=10).
1262
+ #
1263
+ def get_mouse_speed()
1264
+ return get_number("getMouseSpeed", [])
1265
+ end
1266
+
1267
+
1268
+ # Drags an element a certain distance and then drops it
1269
+ #
1270
+ # 'locator' is an element locator
1271
+ # 'movementsString' is offset in pixels from the current location to which the element should be moved, e.g., "+70,-300"
1272
+ def drag_and_drop(locator,movementsString)
1273
+ do_command("dragAndDrop", [locator,movementsString,])
1274
+ end
1275
+
1276
+
1277
+ # Drags an element and drops it on another element
1278
+ #
1279
+ # 'locatorOfObjectToBeDragged' is an element to be dragged
1280
+ # 'locatorOfDragDestinationObject' is an element whose location (i.e., whose center-most pixel) will be the point where locatorOfObjectToBeDragged is dropped
1281
+ def drag_and_drop_to_object(locatorOfObjectToBeDragged,locatorOfDragDestinationObject)
1282
+ do_command("dragAndDropToObject", [locatorOfObjectToBeDragged,locatorOfDragDestinationObject,])
1283
+ end
1284
+
1285
+
1286
+ # Gives focus to the currently selected window
1287
+ #
1288
+ def window_focus()
1289
+ do_command("windowFocus", [])
1290
+ end
1291
+
1292
+
1293
+ # Resize currently selected window to take up the entire screen
1294
+ #
1295
+ def window_maximize()
1296
+ do_command("windowMaximize", [])
1297
+ end
1298
+
1299
+
1300
+ # Returns the IDs of all windows that the browser knows about.
1301
+ #
1302
+ def get_all_window_ids()
1303
+ return get_string_array("getAllWindowIds", [])
1304
+ end
1305
+
1306
+
1307
+ # Returns the names of all windows that the browser knows about.
1308
+ #
1309
+ def get_all_window_names()
1310
+ return get_string_array("getAllWindowNames", [])
1311
+ end
1312
+
1313
+
1314
+ # Returns the titles of all windows that the browser knows about.
1315
+ #
1316
+ def get_all_window_titles()
1317
+ return get_string_array("getAllWindowTitles", [])
1318
+ end
1319
+
1320
+
1321
+ # Returns the entire HTML source between the opening and
1322
+ # closing "html" tags.
1323
+ #
1324
+ def get_html_source()
1325
+ return get_string("getHtmlSource", [])
1326
+ end
1327
+
1328
+
1329
+ # Moves the text cursor to the specified position in the given input element or textarea.
1330
+ # This method will fail if the specified element isn't an input element or textarea.
1331
+ #
1332
+ # 'locator' is an element locator pointing to an input element or textarea
1333
+ # 'position' is the numerical position of the cursor in the field; position should be 0 to move the position to the beginning of the field. You can also set the cursor to -1 to move it to the end of the field.
1334
+ def set_cursor_position(locator,position)
1335
+ do_command("setCursorPosition", [locator,position,])
1336
+ end
1337
+
1338
+
1339
+ # Get the relative index of an element to its parent (starting from 0). The comment node and empty text node
1340
+ # will be ignored.
1341
+ #
1342
+ # 'locator' is an element locator pointing to an element
1343
+ def get_element_index(locator)
1344
+ return get_number("getElementIndex", [locator,])
1345
+ end
1346
+
1347
+
1348
+ # Check if these two elements have same parent and are ordered siblings in the DOM. Two same elements will
1349
+ # not be considered ordered.
1350
+ #
1351
+ # 'locator1' is an element locator pointing to the first element
1352
+ # 'locator2' is an element locator pointing to the second element
1353
+ def is_ordered(locator1,locator2)
1354
+ return get_boolean("isOrdered", [locator1,locator2,])
1355
+ end
1356
+
1357
+
1358
+ # Retrieves the horizontal position of an element
1359
+ #
1360
+ # 'locator' is an element locator pointing to an element OR an element itself
1361
+ def get_element_position_left(locator)
1362
+ return get_number("getElementPositionLeft", [locator,])
1363
+ end
1364
+
1365
+
1366
+ # Retrieves the vertical position of an element
1367
+ #
1368
+ # 'locator' is an element locator pointing to an element OR an element itself
1369
+ def get_element_position_top(locator)
1370
+ return get_number("getElementPositionTop", [locator,])
1371
+ end
1372
+
1373
+
1374
+ # Retrieves the width of an element
1375
+ #
1376
+ # 'locator' is an element locator pointing to an element
1377
+ def get_element_width(locator)
1378
+ return get_number("getElementWidth", [locator,])
1379
+ end
1380
+
1381
+
1382
+ # Retrieves the height of an element
1383
+ #
1384
+ # 'locator' is an element locator pointing to an element
1385
+ def get_element_height(locator)
1386
+ return get_number("getElementHeight", [locator,])
1387
+ end
1388
+
1389
+
1390
+ # Retrieves the text cursor position in the given input element or textarea; beware, this may not work perfectly on all browsers.
1391
+ #
1392
+ # Specifically, if the cursor/selection has been cleared by JavaScript, this command will tend to
1393
+ # return the position of the last location of the cursor, even though the cursor is now gone from the page. This is filed as SEL-243.
1394
+ #
1395
+ # This method will fail if the specified element isn't an input element or textarea, or there is no cursor in the element.
1396
+ #
1397
+ # 'locator' is an element locator pointing to an input element or textarea
1398
+ def get_cursor_position(locator)
1399
+ return get_number("getCursorPosition", [locator,])
1400
+ end
1401
+
1402
+
1403
+ # Returns the specified expression.
1404
+ #
1405
+ # This is useful because of JavaScript preprocessing.
1406
+ # It is used to generate commands like assertExpression and waitForExpression.
1407
+ #
1408
+ #
1409
+ # 'expression' is the value to return
1410
+ def get_expression(expression)
1411
+ return get_string("getExpression", [expression,])
1412
+ end
1413
+
1414
+
1415
+ # Returns the number of nodes that match the specified xpath, eg. "//table" would give
1416
+ # the number of tables.
1417
+ #
1418
+ # 'xpath' is the xpath expression to evaluate. do NOT wrap this expression in a 'count()' function; we will do that for you.
1419
+ def get_xpath_count(xpath)
1420
+ return get_number("getXpathCount", [xpath,])
1421
+ end
1422
+
1423
+
1424
+ # Temporarily sets the "id" attribute of the specified element, so you can locate it in the future
1425
+ # using its ID rather than a slow/complicated XPath. This ID will disappear once the page is
1426
+ # reloaded.
1427
+ #
1428
+ # 'locator' is an element locator pointing to an element
1429
+ # 'identifier' is a string to be used as the ID of the specified element
1430
+ def assign_id(locator,identifier)
1431
+ do_command("assignId", [locator,identifier,])
1432
+ end
1433
+
1434
+
1435
+ # Specifies whether Selenium should use the native in-browser implementation
1436
+ # of XPath (if any native version is available); if you pass "false" to
1437
+ # this function, we will always use our pure-JavaScript xpath library.
1438
+ # Using the pure-JS xpath library can improve the consistency of xpath
1439
+ # element locators between different browser vendors, but the pure-JS
1440
+ # version is much slower than the native implementations.
1441
+ #
1442
+ # 'allow' is boolean, true means we'll prefer to use native XPath; false means we'll only use JS XPath
1443
+ def allow_native_xpath(allow)
1444
+ do_command("allowNativeXpath", [allow,])
1445
+ end
1446
+
1447
+
1448
+ # Specifies whether Selenium will ignore xpath attributes that have no
1449
+ # value, i.e. are the empty string, when using the non-native xpath
1450
+ # evaluation engine. You'd want to do this for performance reasons in IE.
1451
+ # However, this could break certain xpaths, for example an xpath that looks
1452
+ # for an attribute whose value is NOT the empty string.
1453
+ #
1454
+ # The hope is that such xpaths are relatively rare, but the user should
1455
+ # have the option of using them. Note that this only influences xpath
1456
+ # evaluation when using the ajaxslt engine (i.e. not "javascript-xpath").
1457
+ #
1458
+ # 'ignore' is boolean, true means we'll ignore attributes without value at the expense of xpath "correctness"; false means we'll sacrifice speed for correctness.
1459
+ def ignore_attributes_without_value(ignore)
1460
+ do_command("ignoreAttributesWithoutValue", [ignore,])
1461
+ end
1462
+
1463
+
1464
+ # Runs the specified JavaScript snippet repeatedly until it evaluates to "true".
1465
+ # The snippet may have multiple lines, but only the result of the last line
1466
+ # will be considered.
1467
+ #
1468
+ # Note that, by default, the snippet will be run in the runner's test window, not in the window
1469
+ # of your application. To get the window of your application, you can use
1470
+ # the JavaScript snippet <tt>selenium.browserbot.getCurrentWindow()</tt>, and then
1471
+ # run your JavaScript in there
1472
+ #
1473
+ #
1474
+ # 'script' is the JavaScript snippet to run
1475
+ # 'timeout' is a timeout in milliseconds, after which this command will return with an error
1476
+ def wait_for_condition(script,timeout)
1477
+ do_command("waitForCondition", [script,timeout,])
1478
+ end
1479
+
1480
+
1481
+ # Specifies the amount of time that Selenium will wait for actions to complete.
1482
+ #
1483
+ # Actions that require waiting include "open" and the "waitFor*" actions.
1484
+ #
1485
+ # The default timeout is 30 seconds.
1486
+ #
1487
+ # 'timeout' is a timeout in milliseconds, after which the action will return with an error
1488
+ def set_timeout(timeout)
1489
+ do_command("setTimeout", [timeout,])
1490
+ end
1491
+
1492
+
1493
+ # Waits for a new page to load.
1494
+ #
1495
+ # You can use this command instead of the "AndWait" suffixes, "clickAndWait", "selectAndWait", "typeAndWait" etc.
1496
+ # (which are only available in the JS API).
1497
+ # Selenium constantly keeps track of new pages loading, and sets a "newPageLoaded"
1498
+ # flag when it first notices a page load. Running any other Selenium command after
1499
+ # turns the flag to false. Hence, if you want to wait for a page to load, you must
1500
+ # wait immediately after a Selenium command that caused a page-load.
1501
+ #
1502
+ #
1503
+ # 'timeout' is a timeout in milliseconds, after which this command will return with an error
1504
+ def wait_for_page_to_load(timeout)
1505
+ do_command("waitForPageToLoad", [timeout,])
1506
+ end
1507
+
1508
+
1509
+ # Waits for a new frame to load.
1510
+ #
1511
+ # Selenium constantly keeps track of new pages and frames loading,
1512
+ # and sets a "newPageLoaded" flag when it first notices a page load.
1513
+ #
1514
+ #
1515
+ # See waitForPageToLoad for more information.
1516
+ #
1517
+ # 'frameAddress' is FrameAddress from the server side
1518
+ # 'timeout' is a timeout in milliseconds, after which this command will return with an error
1519
+ def wait_for_frame_to_load(frameAddress,timeout)
1520
+ do_command("waitForFrameToLoad", [frameAddress,timeout,])
1521
+ end
1522
+
1523
+
1524
+ # Return all cookies of the current page under test.
1525
+ #
1526
+ def get_cookie()
1527
+ return get_string("getCookie", [])
1528
+ end
1529
+
1530
+
1531
+ # Returns the value of the cookie with the specified name, or throws an error if the cookie is not present.
1532
+ #
1533
+ # 'name' is the name of the cookie
1534
+ def get_cookie_by_name(name)
1535
+ return get_string("getCookieByName", [name,])
1536
+ end
1537
+
1538
+
1539
+ # Returns true if a cookie with the specified name is present, or false otherwise.
1540
+ #
1541
+ # 'name' is the name of the cookie
1542
+ def is_cookie_present(name)
1543
+ return get_boolean("isCookiePresent", [name,])
1544
+ end
1545
+
1546
+
1547
+ # Create a new cookie whose path and domain are same with those of current page
1548
+ # under test, unless you specified a path for this cookie explicitly.
1549
+ #
1550
+ # 'nameValuePair' is name and value of the cookie in a format "name=value"
1551
+ # 'optionsString' is options for the cookie. Currently supported options include 'path', 'max_age' and 'domain'. the optionsString's format is "path=/path/, max_age=60, domain=.foo.com". The order of options are irrelevant, the unit of the value of 'max_age' is second. Note that specifying a domain that isn't a subset of the current domain will usually fail.
1552
+ def create_cookie(nameValuePair,optionsString)
1553
+ do_command("createCookie", [nameValuePair,optionsString,])
1554
+ end
1555
+
1556
+
1557
+ # Delete a named cookie with specified path and domain. Be careful; to delete a cookie, you
1558
+ # need to delete it using the exact same path and domain that were used to create the cookie.
1559
+ # If the path is wrong, or the domain is wrong, the cookie simply won't be deleted. Also
1560
+ # note that specifying a domain that isn't a subset of the current domain will usually fail.
1561
+ #
1562
+ # Since there's no way to discover at runtime the original path and domain of a given cookie,
1563
+ # we've added an option called 'recurse' to try all sub-domains of the current domain with
1564
+ # all paths that are a subset of the current path. Beware; this option can be slow. In
1565
+ # big-O notation, it operates in O(n*m) time, where n is the number of dots in the domain
1566
+ # name and m is the number of slashes in the path.
1567
+ #
1568
+ # 'name' is the name of the cookie to be deleted
1569
+ # 'optionsString' is options for the cookie. Currently supported options include 'path', 'domain' and 'recurse.' The optionsString's format is "path=/path/, domain=.foo.com, recurse=true". The order of options are irrelevant. Note that specifying a domain that isn't a subset of the current domain will usually fail.
1570
+ def delete_cookie(name,optionsString)
1571
+ do_command("deleteCookie", [name,optionsString,])
1572
+ end
1573
+
1574
+
1575
+ # Calls deleteCookie with recurse=true on all cookies visible to the current page.
1576
+ # As noted on the documentation for deleteCookie, recurse=true can be much slower
1577
+ # than simply deleting the cookies using a known domain/path.
1578
+ #
1579
+ def delete_all_visible_cookies()
1580
+ do_command("deleteAllVisibleCookies", [])
1581
+ end
1582
+
1583
+
1584
+ # Sets the threshold for browser-side logging messages; log messages beneath this threshold will be discarded.
1585
+ # Valid logLevel strings are: "debug", "info", "warn", "error" or "off".
1586
+ # To see the browser logs, you need to
1587
+ # either show the log window in GUI mode, or enable browser-side logging in Selenium RC.
1588
+ #
1589
+ # 'logLevel' is one of the following: "debug", "info", "warn", "error" or "off"
1590
+ def set_browser_log_level(logLevel)
1591
+ do_command("setBrowserLogLevel", [logLevel,])
1592
+ end
1593
+
1594
+
1595
+ # Creates a new "script" tag in the body of the current test window, and
1596
+ # adds the specified text into the body of the command. Scripts run in
1597
+ # this way can often be debugged more easily than scripts executed using
1598
+ # Selenium's "getEval" command. Beware that JS exceptions thrown in these script
1599
+ # tags aren't managed by Selenium, so you should probably wrap your script
1600
+ # in try/catch blocks if there is any chance that the script will throw
1601
+ # an exception.
1602
+ #
1603
+ # 'script' is the JavaScript snippet to run
1604
+ def run_script(script)
1605
+ do_command("runScript", [script,])
1606
+ end
1607
+
1608
+
1609
+ # Defines a new function for Selenium to locate elements on the page.
1610
+ # For example,
1611
+ # if you define the strategy "foo", and someone runs click("foo=blah"), we'll
1612
+ # run your function, passing you the string "blah", and click on the element
1613
+ # that your function
1614
+ # returns, or throw an "Element not found" error if your function returns null.
1615
+ #
1616
+ # We'll pass three arguments to your function:
1617
+ # * locator: the string the user passed in
1618
+ # * inWindow: the currently selected window
1619
+ # * inDocument: the currently selected document
1620
+ #
1621
+ #
1622
+ # The function must return null if the element can't be found.
1623
+ #
1624
+ # 'strategyName' is the name of the strategy to define; this should use only letters [a-zA-Z] with no spaces or other punctuation.
1625
+ # 'functionDefinition' is a string defining the body of a function in JavaScript. For example: <tt>return inDocument.getElementById(locator);</tt>
1626
+ def add_location_strategy(strategyName,functionDefinition)
1627
+ do_command("addLocationStrategy", [strategyName,functionDefinition,])
1628
+ end
1629
+
1630
+
1631
+ # Saves the entire contents of the current window canvas to a PNG file.
1632
+ # Currently this only works in Mozilla and when running in chrome mode.
1633
+ # Contrast this with the captureScreenshot command, which captures the
1634
+ # contents of the OS viewport (i.e. whatever is currently being displayed
1635
+ # on the monitor), and is implemented in the RC only. Implementation
1636
+ # mostly borrowed from the Screengrab! Firefox extension. Please see
1637
+ # http://www.screengrab.org for details.
1638
+ #
1639
+ # 'filename' is the path to the file to persist the screenshot as. No filename extension will be appended by default. Directories will not be created if they do not exist, and an exception will be thrown, possibly by native code.
1640
+ # 'kwargs' is a kwargs string that modifies the way the screenshot is captured. Example: "background=#CCFFDD" . Currently valid options: * background:: the background CSS for the HTML document. This may be useful to set for capturing screenshots of less-than-ideal layouts, for example where absolute positioning causes the calculation of the canvas dimension to fail and a black background is exposed (possibly obscuring black text).
1641
+ def capture_entire_page_screenshot(filename,kwargs)
1642
+ do_command("captureEntirePageScreenshot", [filename,kwargs,])
1643
+ end
1644
+
1645
+
1646
+ # Writes a message to the status bar and adds a note to the browser-side
1647
+ # log.
1648
+ #
1649
+ # 'context' is the message to be sent to the browser
1650
+ def set_context(context)
1651
+ do_command("setContext", [context,])
1652
+ end
1653
+
1654
+
1655
+ # Sets a file input (upload) field to the file listed in fileLocator
1656
+ #
1657
+ # 'fieldLocator' is an element locator
1658
+ # 'fileLocator' is a URL pointing to the specified file. Before the file can be set in the input field (fieldLocator), Selenium RC may need to transfer the file to the local machine before attaching the file in a web page form. This is common in selenium grid configurations where the RC server driving the browser is not the same machine that started the test. Supported Browsers: Firefox ("*chrome") only.
1659
+ def attach_file(fieldLocator,fileLocator)
1660
+ do_command("attachFile", [fieldLocator,fileLocator,])
1661
+ end
1662
+
1663
+
1664
+ # Captures a PNG screenshot to the specified file.
1665
+ #
1666
+ # 'filename' is the absolute path to the file to be written, e.g. "c:\blah\screenshot.png"
1667
+ def capture_screenshot(filename)
1668
+ do_command("captureScreenshot", [filename,])
1669
+ end
1670
+
1671
+
1672
+ # Kills the running Selenium Server and all browser sessions. After you run this command, you will no longer be able to send
1673
+ # commands to the server; you can't remotely start the server once it has been stopped. Normally
1674
+ # you should prefer to run the "stop" command, which terminates the current browser session, rather than
1675
+ # shutting down the entire server.
1676
+ #
1677
+ def shut_down_selenium_server()
1678
+ do_command("shutDownSeleniumServer", [])
1679
+ end
1680
+
1681
+
1682
+ # Simulates a user pressing a key (without releasing it yet) by sending a native operating system keystroke.
1683
+ # This function uses the java.awt.Robot class to send a keystroke; this more accurately simulates typing
1684
+ # a key on the keyboard. It does not honor settings from the shiftKeyDown, controlKeyDown, altKeyDown and
1685
+ # metaKeyDown commands, and does not target any particular HTML element. To send a keystroke to a particular
1686
+ # element, focus on the element first before running this command.
1687
+ #
1688
+ # 'keycode' is an integer keycode number corresponding to a java.awt.event.KeyEvent; note that Java keycodes are NOT the same thing as JavaScript keycodes!
1689
+ def key_down_native(keycode)
1690
+ do_command("keyDownNative", [keycode,])
1691
+ end
1692
+
1693
+
1694
+ # Simulates a user releasing a key by sending a native operating system keystroke.
1695
+ # This function uses the java.awt.Robot class to send a keystroke; this more accurately simulates typing
1696
+ # a key on the keyboard. It does not honor settings from the shiftKeyDown, controlKeyDown, altKeyDown and
1697
+ # metaKeyDown commands, and does not target any particular HTML element. To send a keystroke to a particular
1698
+ # element, focus on the element first before running this command.
1699
+ #
1700
+ # 'keycode' is an integer keycode number corresponding to a java.awt.event.KeyEvent; note that Java keycodes are NOT the same thing as JavaScript keycodes!
1701
+ def key_up_native(keycode)
1702
+ do_command("keyUpNative", [keycode,])
1703
+ end
1704
+
1705
+
1706
+ # Simulates a user pressing and releasing a key by sending a native operating system keystroke.
1707
+ # This function uses the java.awt.Robot class to send a keystroke; this more accurately simulates typing
1708
+ # a key on the keyboard. It does not honor settings from the shiftKeyDown, controlKeyDown, altKeyDown and
1709
+ # metaKeyDown commands, and does not target any particular HTML element. To send a keystroke to a particular
1710
+ # element, focus on the element first before running this command.
1711
+ #
1712
+ # 'keycode' is an integer keycode number corresponding to a java.awt.event.KeyEvent; note that Java keycodes are NOT the same thing as JavaScript keycodes!
1713
+ def key_press_native(keycode)
1714
+ do_command("keyPressNative", [keycode,])
1715
+ end
1716
+
1717
+
1718
+ end
1719
+
1720
+ SeleneseInterpreter = SeleniumDriver # for backward compatibility
1721
+
1722
+ end
1723
+
1724
+ class SeleniumCommandError < RuntimeError
1725
+ end
1726
+
1727
+ # Defines a mixin module that you can use to write Selenium tests
1728
+ # without typing "@selenium." in front of every command. Every
1729
+ # call to a missing method will be automatically sent to the @selenium
1730
+ # object.
1731
+ module SeleniumHelper
1732
+
1733
+ # Overrides standard "open" method with @selenium.open
1734
+ def open(addr)
1735
+ @selenium.open(addr)
1736
+ end
1737
+
1738
+ # Overrides standard "type" method with @selenium.type
1739
+ def type(inputLocator, value)
1740
+ @selenium.type(inputLocator, value)
1741
+ end
1742
+
1743
+ # Overrides standard "select" method with @selenium.select
1744
+ def select(inputLocator, optionLocator)
1745
+ @selenium.select(inputLocator, optionLocator)
1746
+ end
1747
+
1748
+ # Passes all calls to missing methods to @selenium
1749
+ def method_missing(method_name, *args)
1750
+ if args.empty?
1751
+ @selenium.send(method_name)
1752
+ else
1753
+ @selenium.send(method_name, *args)
1754
+ end
1755
+ end
1756
+ end