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?, #name, #src, #type, #value

Methods inherited from Element

#<=>, #attribute_value, #class_name, #click, #disabled?, #double_click, #enabled?, #exists?, #fire_event, #flash, #focus, #focused?, #html, #id, #initialize, #inspect, #method_missing, #ole_object, #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, #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:



233
234
235
236
237
238
239
240
# File 'lib/watir-classic/input_elements.rb', line 233

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:



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/watir-classic/input_elements.rb', line 213

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.



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

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

#labelString

Returns text field label’s text.

Returns:

  • (String)

    text field label’s text.



204
205
206
# File 'lib/watir-classic/input_elements.rb', line 204

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

#maxlengthFixnum

Returns value of maxlength attribute.

Returns:

  • (Fixnum)

    value of maxlength attribute.



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

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:



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

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:



248
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 248

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:



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

attr_ole :size

#to_sObject



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

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.



274
275
276
277
# File 'lib/watir-classic/input_elements.rb', line 274

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.”



281
282
283
284
285
286
287
288
289
290
# File 'lib/watir-classic/input_elements.rb', line 281

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