Class: Watir::TextField

Inherits:
InputElement show all
Defined in:
lib/watir-classic/input_elements.rb

Overview

Returned be Container#text_field.

Direct Known Subclasses

Hidden

Instance Attribute Summary

Attributes inherited from Element

#container

Attributes included from Container

#page_container

Instance Method Summary collapse

Methods inherited from InputElement

#alt, #disabled?, #label, #name, #required?, #src, #type, #value

Methods inherited from Element

#<=>, #attribute_value, #class_name, #click, #disabled?, #double_click, #enabled?, #exists?, #fire_event, #flash, #focus, #focused?, #id, #initialize, #inner_html, #inspect, #method_missing, #ole_object, #outer_html, #parent, #right_click, #send_keys, #style, #tag_name, #title, #to_subtype, #unique_number, #visible?

Methods included from DragAndDropHelper

#drag_and_drop_by, #drag_and_drop_on

Methods included from Container

#a, #abbr, #address, #alert, #area, #article, #aside, #audio, #b, #base, #bdi, #bdo, #blockquote, #body, #br, #button, #canvas, #caption, #checkbox, #cite, #code, #col, #colgroup, #command, #data, #datalist, #dd, #del, #details, #dfn, #div, #dl, #dt, #element, #em, #embed, #fieldset, #figcaption, #figure, #file_field, #font, #footer, #form, #frame, #frameset, #h1, #h2, #h3, #h4, #h5, #h6, #head, #header, #hgroup, #hidden, #hr, #i, #img, #input, #ins, #kbd, #keygen, #label, #legend, #li, #map, #mark, #menu, #meta, #meter, #modal_dialog, #nav, #noscript, #object, #ol, #optgroup, #option, #output, #p, #param, #pre, #progress, #q, #radio, #rp, #rt, #ruby, #s, #samp, #script, #section, #select, #small, #source, #span, #strong, #style, #sub, #summary, #sup, #table, #tbody, #td, #text_field, #textarea, #tfoot, #th, #thead, #time, #title, #tr, #track, #u, #ul, #var, #video, #wbr

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

#append(value) ⇒ Object

Append the specified text value to the contents of the text field.

Parameters:

  • value (String)

    text to append to current text field’s value.

Raises:



236
237
238
239
240
241
242
243
# File 'lib/watir-classic/input_elements.rb', line 236

def append(value)
  perform_action do
    assert_not_readonly
    @o.scrollIntoView
    @o.focus(0)
    type_by_character(value)
  end
end

#clearObject

Clear the contents of the text field.

Raises:



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/watir-classic/input_elements.rb', line 216

def clear
  perform_action do
    assert_not_readonly
    @o.scrollIntoView
    @o.focus(0)
    @o.select(0)
    dispatch_event("onSelect")
    @o.value = ""
    dispatch_event("onKeyPress")
    dispatch_event("onChange")
    @container.wait
  end
end

#drag_contents_to(destination_how, destination_what) ⇒ Object

Deprecated.

Not part of the WatirSpec API.



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/watir-classic/input_elements.rb', line 296

def drag_contents_to(destination_how, destination_what)
  Kernel.warn "Deprecated(TextField#drag_contents_to) - is not parf ot the WatirSpec API and might be deleted in the future."
  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

#maxlengthFixnum

Returns value of maxlength attribute.

Returns:

  • (Fixnum)

    value of maxlength attribute.



202
203
204
205
206
207
208
209
# File 'lib/watir-classic/input_elements.rb', line 202

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

#readonly?String, ...

Retrieve element’s readonly? from the OLE method.

Returns:

  • (String, Boolean, Fixnum)

    element’s “readonly?” attribute value. Return type depends of the attribute type.

  • (String)

    an empty String if the “readonly?” attribute does not exist.

See Also:



197
# File 'lib/watir-classic/input_elements.rb', line 197

attr_ole :readonly?

#set(value) ⇒ Object

Sets the contents of the text field to the specified value.

Parameters:

  • value (String)

    text to set as value.

Raises:



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

def set(value)
  perform_action do
    assert_not_readonly
    @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
  end
end

#sizeString, ...

Retrieve element’s size from the OLE method.

Returns:

  • (String, Boolean, Fixnum)

    element’s “size” attribute value. Return type depends of the attribute type.

  • (String)

    an empty String if the “size” attribute does not exist.

See Also:



196
# File 'lib/watir-classic/input_elements.rb', line 196

attr_ole :size

#to_sObject



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

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

#value=(value) ⇒ Object

Note:

it does not cause any JavaScript events to be fired or exceptions to be raised. Using #set is recommended.

Sets the value of the text field directly.

Parameters:

  • value (String)

    value to be set.



277
278
279
280
# File 'lib/watir-classic/input_elements.rb', line 277

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

#verify_contains(target) ⇒ Object

Deprecated.

Use “browser.text_field.value.include?(target)” or “browser.text_field.value.match(target) instead.”



284
285
286
287
288
289
290
291
292
293
# File 'lib/watir-classic/input_elements.rb', line 284

def verify_contains(target)
  Kernel.warn "Deprecated(TextField#verify_contains) - use \"browser.text_field.value.include?(target)\" or \"browser.text_field.value.match(target)\" instead."
  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