Class: Watir::TextField

Inherits:
InputElement show all
Defined in:
lib/watir/input_elements.rb,
lib/watir/camel_case.rb

Overview

This class is the main class for Text Fields Normally a user would not need to create this object as it is returned by the Watir::Container#text_field method

Direct Known Subclasses

Hidden

Constant Summary collapse

INPUT_TYPES =

:stopdoc:

["text", "password", "textarea"]

Constants inherited from Element

Element::TO_S_SIZE

Instance Attribute Summary

Attributes inherited from Element

#container

Attributes included from Container

#activeObjectHighLightColor, #page_container, #type_keys, #typingspeed

Instance Method Summary collapse

Methods inherited from InputElement

#initialize, #locate

Methods inherited from Element

#<=>, #activeObjectHighLightColor, #after_text, #assert_enabled, #assert_exists, #attribute_value, #before_text, #click, #click!, #click_no_wait, #document, #enabled?, #exists?, #fire_event, #flash, #focus, #initialize, #inspect, #ole_object, #ole_object=, #parent, #text, #type_keys, #typingspeed, #visible?

Methods included from Container

#area, #areas, #button, #buttons, #cell, #cells, #checkbox, #checkboxes, #dds, #divs, #dls, #dts, #element, #elements, #ems, #file_field, #file_fields, #form, #forms, #frame, #hidden, #hiddens, #image, #images, #labels, #link, #links, #lis, #locate_all_elements, #locate_input_element, #locate_tagged_element, #log, #map, #maps, #modal_dialog, #popup, #pres, #ps, #radio, #radios, #row, #rows, #select_list, #select_lists, #set_container, #show_all_objects, #spans, #strongs, #table, #tables, #text_field, #text_fields, #wait

Constructor Details

This class inherits a constructor from Watir::InputElement

Instance Method Details

#abhors_typingObject

:nodoc:



400
401
402
403
# File 'lib/watir/input_elements.rb', line 400

def abhors_typing #:nodoc:
	@type_keys = false
	self
end

#append(value) ⇒ Object

Appends the specified string value to the contents of the text box.

Raises UnknownObjectException if the object cant be found
Raises ObjectDisabledException if the object is disabled
Raises ObjectReadOnlyException if the object is read only


349
350
351
352
353
354
355
356
357
358
359
# File 'lib/watir/input_elements.rb', line 349

def append(value)
  assert_exists
  assert_enabled
  assert_not_readonly
  
  highlight(:set)
  @o.scrollIntoView
  @o.focus
  type_by_character(value)
  highlight(:clear)
end

#assert_not_readonlyObject

:nodoc:



273
274
275
276
277
278
# File 'lib/watir/input_elements.rb', line 273

def assert_not_readonly #:nodoc:
  if self.readonly?
    raise ObjectReadOnlyException, 
      "Textfield #{@how} and #{@what} is read only."
  end
end

#clearObject

Clears the contents of the text box.

Raises UnknownObjectException if the object can't be found
Raises ObjectDisabledException if the object is disabled
Raises ObjectReadOnlyException if the object is read only


327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/watir/input_elements.rb', line 327

def clear
  assert_exists
  assert_enabled
  assert_not_readonly
  
  highlight(:set)
  
  @o.scrollIntoView
  @o.focus
  @o.select
  @o.fireEvent("onSelect")
  @o.value = ""
  @o.fireEvent("onKeyPress")
  @o.fireEvent("onChange")
  @container.wait
  highlight(:clear)
end

#drag_contents_to(destination_how, destination_what) ⇒ Object

Drag the entire contents of the text field to another text field

19 Jan 2005 - It is added as prototype functionality, and may change
 * destination_how   - symbol, :id, :name how we identify the drop target
 * destination_what  - string or regular expression, the name, id, etc of the text field that will be the drop target


300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/watir/input_elements.rb', line 300

def drag_contents_to(destination_how, destination_what)
  assert_exists
  destination = @container.text_field(destination_how, destination_what)
  unless destination.exists?
    raise UnknownObjectException, "Unable to locate destination using #{destination_how } and #{destination_what } "
  end
  
  @o.focus
  @o.select
  value = self.value
  
  @o.fireEvent("onSelect")
  @o.fireEvent("ondragstart")
  @o.fireEvent("ondrag")
  destination.fireEvent("onDragEnter")
  destination.fireEvent("onDragOver")
  destination.fireEvent("ondrop")
  
  @o.fireEvent("ondragend")
  destination.value = destination.value + value.to_s
  self.value = ""
end

#dragContentsToObject



58
# File 'lib/watir/camel_case.rb', line 58

alias dragContentsTo drag_contents_to

#maxLengthObject



57
# File 'lib/watir/camel_case.rb', line 57

alias maxLength maxlength

#maxlengthObject

return number of maxlength attribute



247
248
249
250
251
252
253
254
# File 'lib/watir/input_elements.rb', line 247

def maxlength
  assert_exists
  begin
    ole_object.invoke('maxlength').to_i
  rescue WIN32OLERuntimeError
    0
  end
end

#requires_typingObject

:nodoc:



396
397
398
399
# File 'lib/watir/input_elements.rb', line 396

def requires_typing #:nodoc:
	@type_keys = true
	self
end

#set(value) ⇒ Object

Sets the contents of the text box to the specified text value

Raises UnknownObjectException if the object cant be found
Raises ObjectDisabledException if the object is disabled
Raises ObjectReadOnlyException if the object is read only


365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/watir/input_elements.rb', line 365

def set(value)
  assert_exists
  assert_enabled
  assert_not_readonly
  
  highlight(:set)
  @o.scrollIntoView
  if type_keys
   @o.focus
   @o.select
   @o.fireEvent("onSelect")
   @o.fireEvent("onKeyPress")
   @o.value = ""
   type_by_character(value)
   @o.fireEvent("onChange")
   @o.fireEvent("onBlur")
 else
@o.value = limit_to_maxlength(value)
 end
  highlight(:clear)
end

#to_sObject



266
267
268
269
270
271
# File 'lib/watir/input_elements.rb', line 266

def to_s
  assert_exists
  r = string_creator
  r += text_string_creator
  r.join("\n")
end

#value=(v) ⇒ Object

Sets the value of the text field directly. It causes no events to be fired or exceptions to be raised, so generally shouldn’t be used. It is preffered to use the set method.



391
392
393
394
# File 'lib/watir/input_elements.rb', line 391

def value=(v)
  assert_exists
  @o.value = v.to_s
end

#verify_contains(target) ⇒ Object

Returns true if the text field contents is matches the specified target, which can be either a string or a regular expression.

Raises UnknownObjectException if the object can't be found

– I vote for deprecating this we should use text_field().text.include?(some) or text.match(/some/) instead of this method



286
287
288
289
290
291
292
293
294
# File 'lib/watir/input_elements.rb', line 286

def verify_contains(target) #:nodoc:
  assert_exists
  if target.kind_of? String
    return true if self.value == target
  elsif target.kind_of? Regexp
    return true if self.value.match(target) != nil
  end
  return false
end