Class: Celerity::TextField

Class representing text field elements

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

Constants

DEFAULT_HOW
:name
NON_TEXT_TYPES
%w[file radio checkbox submit reset image button hidden]
TAGS
[ Identifier.new('textarea'), Identifier.new('input', :type => ["text", "password", /^(?!(#{ Regexp.union(*NON_TEXT_TYPES) })$)/]) ]

Constructor Summary

This class inherits a constructor from Celerity::Element.

Dynamic Method Handling

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

public String method_missing(meth, *args, &blk)

Dynamically get element attributes.

Meta Tags

Parameters:

Returns:

[String]

The resulting attribute.

Raises:

[NoMethodError]

if the element doesn’t support this attribute.

See Also:

ATTRIBUTES constant for a list of valid methods for a given element.
[View source]


235
236
237
238
239
240
241
242
243
244
245
# File 'lib/celerity/element.rb', line 235

def method_missing(meth, *args, &blk)
  assert_exists

  meth = selector_to_attribute(meth)

  if self.class::ATTRIBUTES.include?(meth) || (self.class == Element && @object.hasAttribute(meth.to_s))
    return @object.getAttribute(meth.to_s)
  end
  Log.warn "Element\#method_missing calling super with #{meth.inspect}"
  super
end

Public Visibility

Public Instance Method Summary

#append(value)

Append the given value to the text in the text field.

#clear

Clear the text field.

#contains_text(expected_text)

Check if the given text fields contains the given String or Regexp.

#drag_contents_to(how, what) #dragContentsTo

This bascially just moves the text to the other text field using TextField#append TODO: check if HtmlUnit supports some kind of dragging.

#requires_typing
#set(value)

Set the text field to the given value.

#type
#value #getContents #get_contents

Returns the text in the text field.

#verify_contains(expected)

A boolean version of TextField#contains_text.

Returns: boolean

#visible?

Public Instance Method Details

append

public append(value)

Append the given value to the text in the text field.

[View source]


76
77
78
79
80
# File 'lib/celerity/elements/text_field.rb', line 76

def append(value)
  assert_enabled
  assert_not_readonly
  type_string(value)
end

clear

public clear

Clear the text field.

[View source]


25
26
27
28
# File 'lib/celerity/elements/text_field.rb', line 25

def clear
  assert_exists
  insert_string ''
end

contains_text

public contains_text(expected_text)

Check if the given text fields contains the given String or Regexp.

[View source]


110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/celerity/elements/text_field.rb', line 110

def contains_text(expected_text)
  assert_exists

  case expected_text
  when Regexp
    value() =~ expected_text
  when String
    value().index(expected_text)
  else
    raise TypeError, "expected String or Regexp, got #{expected_text.inspect}:#{expected_text.class}"
  end
end

drag_contents_to

public drag_contents_to(how, what)

Also known as: dragContentsTo

This bascially just moves the text to the other text field using TextField#append TODO: check if HtmlUnit supports some kind of dragging.

[View source]


99
100
101
102
103
104
# File 'lib/celerity/elements/text_field.rb', line 99

def drag_contents_to(how, what)
  assert_exists # assert_enabled?
  val = self.value
  self.value = ''
  @container.text_field(how, what).append(val)
end

requires_typing

public requires_typing
[View source]


82
# File 'lib/celerity/watir_compatibility.rb', line 82

def requires_typing; end

set

public set(value)

Set the text field to the given value. This ensures execution of JavaScript events (onkeypress etc.), but is slower than value=

[View source]


35
36
37
38
39
40
# File 'lib/celerity/elements/text_field.rb', line 35

def set(value)
  assert_enabled
  assert_not_readonly
  clear
  type_string(value.to_s)
end

type

public type
[View source]


83
84
85
86
87
88
89
90
91
92
# File 'lib/celerity/elements/text_field.rb', line 83

def type
  assert_exists
  type = @object.getAttribute 'type'

  if NON_TEXT_TYPES.include?(type)
    type
  else
    'text'
  end
end

value

public value

Also known as: getContents get_contents

Returns the text in the text field.

[View source]


47
48
49
50
51
52
53
54
55
# File 'lib/celerity/elements/text_field.rb', line 47

def value
  assert_exists
  case @object.getTagName
  when 'textarea'
    @object.getText
  when 'input'
    @object.getValueAttribute
  end
end

verify_contains

public boolean verify_contains(expected)

A boolean version of TextField#contains_text

Meta Tags

Parameters:

Returns:

[boolean]
[View source]


129
130
131
132
# File 'lib/celerity/elements/text_field.rb', line 129

def verify_contains(expected)
  # assert_exists called by contains_text
  !!contains_text(expected)
end

visible?

public visible?
[View source]


16
17
18
19
# File 'lib/celerity/elements/text_field.rb', line 16

def visible?
  assert_exists
  type == 'hidden' ? false : super
end