Class: Watir::TextField

Inherits:
InputElement show all
Defined in:
lib/watir-classic/input_elements.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

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

#locate

Methods inherited from Element

#<=>, #__ole_inner_elements, #activeObjectHighLightColor, #assert_enabled, #assert_exists, #attribute_value, #click, #click!, #create_event, #disabled?, #dispatch_event, #document, #double_click, #enabled?, #exists?, #fire_event, #flash, #focus, #focused?, #initialize, #inspect, #locate, #method_missing, #ole_object, #ole_object=, #parent, #right_click, #send_keys, #style, #tag_name, #to_subtype, #type_keys, #typingspeed, #visible?

Methods included from DragAndDropHelper

#drag_and_drop_by, #drag_and_drop_on

Methods included from Container

#__ole_inner_elements, #alert, #locator_for, #modal_dialog, #set_container, support_element, #wait

Methods included from Exception

message_for_unable_to_locate

Methods included from ElementExtensions

#present?, #wait_until_present, #wait_while_present, #when_present

Constructor Details

This class inherits a constructor from Watir::Element

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Watir::Element

Instance Method Details

#abhors_typingObject

:nodoc:



322
323
324
325
# File 'lib/watir-classic/input_elements.rb', line 322

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


271
272
273
274
275
276
277
278
279
280
281
# File 'lib/watir-classic/input_elements.rb', line 271

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

#assert_not_readonlyObject

:nodoc:



194
195
196
197
198
199
# File 'lib/watir-classic/input_elements.rb', line 194

def assert_not_readonly #:nodoc:
  if self.readonly?
    raise ObjectReadOnlyException, 
      "Textfield #{@specifiers.inspect} 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


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/watir-classic/input_elements.rb', line 249

def clear
  assert_exists
  assert_enabled
  assert_not_readonly
  
  highlight(:set)
  
  @o.scrollIntoView
  @o.focus(0)
  @o.select(0)
  dispatch_event("onSelect")
  @o.value = ""
  dispatch_event("onKeyPress")
  dispatch_event("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


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/watir-classic/input_elements.rb', line 221

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(0)
  @o.select(0)
  value = self.value
  
  dispatch_event("onSelect")
  dispatch_event("ondragstart")
  dispatch_event("ondrag")
  destination.assert_exists
  destination.dispatch_event("onDragEnter")
  destination.dispatch_event("onDragOver")
  destination.dispatch_event("ondrop")
  
  dispatch_event("ondragend")
  destination.value = destination.value + value.to_s
  self.value = ""
end

#labelObject



327
328
329
# File 'lib/watir-classic/input_elements.rb', line 327

def label
  @container.label(:for => name).text
end

#maxlengthObject

return number of maxlength attribute



169
170
171
172
173
174
175
176
# File 'lib/watir-classic/input_elements.rb', line 169

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

#requires_typingObject

:nodoc:



318
319
320
321
# File 'lib/watir-classic/input_elements.rb', line 318

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


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/watir-classic/input_elements.rb', line 287

def set(value)
  assert_exists
  assert_enabled
  assert_not_readonly

  highlight(:set)
  @o.scrollIntoView
  if type_keys
   @o.focus(0)
   @o.select(0)
   dispatch_event("onSelect")
   dispatch_event("onKeyPress")
   @o.value = ""
   type_by_character(value)
   dispatch_event("onChange")
   dispatch_event("onBlur")
 else
@o.value = limit_to_maxlength(value)
 end
  highlight(:clear)
end

#to_sObject



187
188
189
190
191
192
# File 'lib/watir-classic/input_elements.rb', line 187

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.



313
314
315
316
# File 'lib/watir-classic/input_elements.rb', line 313

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



207
208
209
210
211
212
213
214
215
# File 'lib/watir-classic/input_elements.rb', line 207

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