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.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 8

def initialize(args)
  return if args.nil?

  hash = {}
  hash = args if args.is_a? Hash

  args.each do |item|
    hash.merge! item if item.is_a? Hash
  end if args.is_a? Array

  @hash = hash
  @by = hash.keys.first
  @value = hash[hash.keys.first]
  @control = hash.fetch :instance if hash.has_key? :instance

  @found_by_type = @by
  @found_by_value = @value
end

Instance Attribute Details

#found_by_typeObject (readonly)

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 (readonly)

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



52
53
54
55
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 52

def Click
  control = self.Find
  control.click
end

#Contain(value) ⇒ Object



80
81
82
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 80

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

#Equals(value, comparator) ⇒ Object



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

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

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 96

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
    return ControlEvaluation.new(evaluation.left, evaluation.right, self)
  end
end

#Find(comparator = nil) ⇒ Object



27
28
29
30
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 27

def Find(comparator = nil)
  @control ||= $driver.FindItemWithWait(@by, @value, comparator)
  @control
end

#FindAllObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 32

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

  list
end

#FindWithoutWait(comparator = nil) ⇒ Object



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

def FindWithoutWait(comparator = nil)
  $driver.FindItemWithoutWait(@by, @value, comparator)
end

#GetValue(item, key) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 120

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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 84

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



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 57

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

  sleep(1)
end

#Substitute(item) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/sapphire/WebAbstractions/Controls/Base/Control.rb', line 139

def Substitute(item)

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

  return item
end

#TextObject



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

def Text
  text = self.Find
  text.text
end

#Visible(shouldWait = true) ⇒ Object



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

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