Class: Sapphire::WebAbstractions::Control

Inherits:
Object
  • Object
show all
Defined in:
lib/sapphire/WebAbstractions/Controls/Base/Control.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Control

Returns a new instance of Control.



9
10
11
12
13
14
15
16
17
18
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 9

def initialize(args)
  return if args.nil?
  @array = args

  if args.is_a? Hash
    @control = args.fetch :instance if args.has_key? :instance
    @found_by_type = args.keys.first
    @found_by_value = args.fetch(args.keys.first)
  end
end

Instance Attribute Details

#controlObject

Returns the value of attribute control.



7
8
9
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 7

def control
  @control
end

#found_by_typeObject

Returns the value of attribute found_by_type.



5
6
7
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 5

def found_by_type
  @found_by_type
end

#found_by_valueObject

Returns the value of attribute found_by_value.



6
7
8
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 6

def found_by_value
  @found_by_value
end

Instance Method Details

#ClickObject



46
47
48
49
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 46

def Click
  self.Find if @control.nil?
  @control.click
end

#Contain(expected_value) ⇒ Object



75
76
77
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 75

def Contain(expected_value)
  return ContainsComparison.new(ControlEvaluation.new(self.Text, expected_value, self))
end

#Equals(value, comparator) ⇒ Object



70
71
72
73
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 70

def Equals(value, comparator)
  evaluation = ControlEvaluation.new(self.Text, value, self)
  EqualsComparison.new(evaluation)
end

#Evaluate(key, arg, comparator, block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 91

def Evaluate(key, arg, comparator, block)

  value = GetValue(arg, key)
  timeout = GetValue(arg, :wait)
  timeout ||= 20

  begin
    value = value.Text if value.is_a? Control
    evaluation = block.call(self, value)

    wait = Selenium::WebDriver::Wait.new(:timeout => timeout)
      result = wait.until {
        evaluation = block.call(self, value)
        y = evaluation.Evaluate()
        comparator = EqualsComparison.new(evaluation) if evaluation == nil
        evaluation if comparator.Compare(y == true, true)
      }

    return result
  rescue
    puts $!.backtrace if !$verbose.nil?
    return ControlEvaluation.new(evaluation.left, evaluation.right, self)
  end
end

#Find(comparator = nil) ⇒ Object



20
21
22
23
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 20

def Find(comparator = nil)
  @control, @found_by_type, @found_by_value = $driver.FindItemWithWait(@array, comparator)
  @control
end

#FindAllObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 25

def FindAll
  items, @found_by_type, @found_by_value = $driver.FindAllItems(@array)
  list = []
  items.each do |item|
    hash = {@found_by_type => @found_by_value, :instance => item}
    list << Control.new(hash)
  end

  list
end

#FindWithoutWait(comparator = nil) ⇒ Object



36
37
38
39
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 36

def FindWithoutWait(comparator = nil)
  @control, @found_by_type, @found_by_value = $driver.FindItemWithoutWait(@array, comparator)
  @control
end

#GetValue(item, key) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 116

def GetValue(item, key)

  if item.is_a? Array
    item.each do |sub_item|
      value = GetValue(sub_item, key)
      return value if !value.nil?
    end
  end

  if item.is_a? Hash
    return Substitute(key) if Parameters.instance.Contains(key)
    return Substitute(item[key]) if item.has_key? key
  end

  return Parameter(key) if Parameters.instance.Contains(key)

  nil
end

#In(values, comparator) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 79

def In(values, comparator)
  text = self.Text
  values.each do |value|
    if comparator.Compare(text, value)
      return ControlEvaluation.new(text, value, self)
    end
  end

  #error land
  return ControlEvaluation.new(text, values, self)
end

#MouseOverObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 51

def MouseOver
  self.Find
  if(@found_by_type == :id)
    $driver.ExecuteScript("document.getElementById('"+ @found_by_value +"').style.visibility = 'visible'; ")
  elsif (@found_by_type == :name)
    $driver.ExecuteScript("document.getElementByName('"+ @found_by_value +"').style.visibility = 'visible'; ")
  elsif (@found_by_type == :xpath)
    $driver.ExecuteScript("document.evaluate( '" + @found_by_value + "', document, null, XPathResult.ANY_TYPE, null ).iterateNext().style.visibility = 'visible'; ")
  end

  sleep(1)
end

#Substitute(item) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 135

def Substitute(item)

  return $page.Get(item) if $page.Contains(item)
  return Parameter(item) if Parameters.instance.Contains(item)

  return item
end

#TextObject



41
42
43
44
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 41

def Text
  text = self.Find
  text.text
end

#Visible(shouldWait = true) ⇒ Object



64
65
66
67
68
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 64

def Visible(shouldWait = true)
  control = self.Find if shouldWait
  control = self.FindWithoutWait if !shouldWait
  ControlEvaluation.new(control.displayed?, true, self)
end