Skip to content
This repository has been archived by the owner on Jun 6, 2018. It is now read-only.

Commit

Permalink
Restructure code - move private methods to bottom of class declaratio…
Browse files Browse the repository at this point in the history
…n etc.
  • Loading branch information
jarmo committed Jan 5, 2013
1 parent 896201b commit d2c3b60
Showing 1 changed file with 88 additions and 100 deletions.
188 changes: 88 additions & 100 deletions lib/watir-classic/input_elements.rb
@@ -1,24 +1,21 @@
module Watir

# Super-class for input elements like {SelectList}, {Button} etc.
class InputElement < Element
# @private
def locate
@o = @container.locator_for(InputElementLocator, @specifiers, self.class).locate
end

attr_ole :disabled?
attr_ole :name
attr_ole :value
attr_ole :alt
attr_ole :src
attr_ole :type

# @private
def locate
@o = @container.locator_for(InputElementLocator, @specifiers, self.class).locate
end

end

#
# Input: Select
#

# Returned by {Container#select_list}.
class SelectList < InputElement
attr_ole :multiple?
Expand Down Expand Up @@ -50,7 +47,7 @@ def select(item)
end
matching_options.first.text
end

# Select an item or items in a select box by option's value.
#
# @param [String,Regexp] item option to select by value.
Expand All @@ -63,7 +60,7 @@ def select_value(item)
matching_options.each(&:select)
matching_options.first.value
end

# @example Retrieve selected options as a text:
# browser.select_list.selected_options.map(&:text)
#
Expand Down Expand Up @@ -104,7 +101,7 @@ def matching_items_in_select_list(attribute, value)
end
end
end

# Returned by {Container#option}.
class Option < Element
attr_ole :disabled?
Expand Down Expand Up @@ -167,10 +164,6 @@ def change_selected(value)
end
end

#
# Input: Button
#

# Returned by the {Container#button} method.
class Button < InputElement
alias_method :__value, :value
Expand All @@ -185,10 +178,6 @@ def text
alias_method :value, :text
end

#
# Input: Text
#

# Returned be {Container#text_field}.
class TextField < InputElement
attr_ole :size
Expand All @@ -205,68 +194,12 @@ def maxlength
0
end
end

def text_string_creator
n = []
n << "length:".ljust(TO_S_SIZE) + self.size.to_s
n << "max length:".ljust(TO_S_SIZE) + self.maxlength.to_s
n << "read only:".ljust(TO_S_SIZE) + self.readonly?.to_s
n
end
private :text_string_creator

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

# @private
def assert_not_readonly
if self.readonly?
raise ObjectReadOnlyException,
"Textfield #{@specifiers.inspect} is read only."
end
end

# @deprecated Use "browser.text_field.value.include?(target)" or
# "browser.text_field.value.match(target)"
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

# @deprecated Not part of the WatirSpec API.
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 = ""

# @return [String] text field label's text.
def label
@container.label(:for => name).text
end

# Clear the contents of the text field.
#
# @macro exists
Expand All @@ -285,7 +218,7 @@ def clear
@container.wait
end
end

# Append the specified text value to the contents of the text field.
#
# @param [String] value text to append to current text field's value.
Expand All @@ -300,7 +233,7 @@ def append(value)
type_by_character(value)
end
end

# Sets the contents of the text field to the specified value.
#
# @param [String] value text to set as value.
Expand Down Expand Up @@ -338,21 +271,68 @@ def value=(value)
@o.value = value.to_s
end

# @deprecated Use "browser.text_field.value.include?(target)" or
# "browser.text_field.value.match(target)"
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

# @deprecated Not part of the WatirSpec API.
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

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

# @private
def requires_typing
@type_keys = true
self
@type_keys = true
self
end

# @private
def abhors_typing
@type_keys = false
self
@type_keys = false
self
end

# @return [String] text field label's text.
def label
@container.label(:for => name).text
# @private
def assert_not_readonly
if self.readonly?
raise ObjectReadOnlyException,
"Textfield #{@specifiers.inspect} is read only."
end
end

private
Expand All @@ -370,7 +350,7 @@ def type_by_character(value)
dispatch_event("onKeyUp")
end
end

# Supports double-byte characters
def characters_in(value, &blk)
if RUBY_VERSION =~ /^1\.8/
Expand All @@ -384,7 +364,7 @@ def characters_in(value, &blk)
value.each_char(&blk)
end
end

# Return the value (a string), limited to the maxlength of the element.
def limit_to_maxlength(value)
return value if @o.invoke('type') =~ /textarea/i # text areas don't have maxlength
Expand All @@ -394,6 +374,14 @@ def limit_to_maxlength(value)
value
end

def text_string_creator
n = []
n << "length:".ljust(TO_S_SIZE) + self.size.to_s
n << "max length:".ljust(TO_S_SIZE) + self.maxlength.to_s
n << "read only:".ljust(TO_S_SIZE) + self.readonly?.to_s
n
end

end

# Returned by the {Container#hidden}.
Expand All @@ -402,22 +390,22 @@ class Hidden < TextField
def set(value)
self.value = value
end

# @see TextField#append
def append(value)
self.value = self.value.to_s + value.to_s
end

# @see TextField#clear
def clear
self.value = ""
end

# This method will do nothing since it is impossible to set focus to
# a hidden field.
def focus
end

# @return [Boolean] always false, since hidden element is never visible.
# @macro exists
def visible?
Expand All @@ -440,7 +428,7 @@ def inspect
'#<%s:0x%x located=%s specifiers=%s value=%s>' % [self.class, hash*2, !!ole_object, @specifiers.inspect, @value.inspect]
end
end

# Returned by {Container#radio}.
class Radio < InputElement
include RadioCheckCommon
Expand All @@ -452,7 +440,7 @@ class Radio < InputElement
def clear
perform_action { @o.checked = false }
end

# Check a radio button.
#
# @macro exists
Expand Down Expand Up @@ -485,7 +473,7 @@ def set(value=true)
end
highlight :clear
end

# Clear the checkbox.
# @macro exists
# @macro enabled
Expand Down

0 comments on commit d2c3b60

Please sign in to comment.