Class: Screen

Inherits:
Object
  • Object
show all
Defined in:
lib/icuke/screen.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Screen

Returns a new instance of Screen.



6
7
8
9
10
11
# File 'lib/icuke/screen.rb', line 6

def initialize(xml)
  @xml = Nokogiri::XML::Document.parse(xml).root
  frame = @xml.at_xpath('/screen/frame')
  @x, @y = frame['x'].to_f, frame['y'].to_f
  @width, @height = frame['width'].to_f, frame['height'].to_f
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



4
5
6
# File 'lib/icuke/screen.rb', line 4

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



4
5
6
# File 'lib/icuke/screen.rb', line 4

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



4
5
6
# File 'lib/icuke/screen.rb', line 4

def x
  @x
end

#xmlObject (readonly)

Returns the value of attribute xml.



4
5
6
# File 'lib/icuke/screen.rb', line 4

def xml
  @xml
end

#yObject (readonly)

Returns the value of attribute y.



4
5
6
# File 'lib/icuke/screen.rb', line 4

def y
  @y
end

Instance Method Details

#center_coordinatesObject



68
69
70
# File 'lib/icuke/screen.rb', line 68

def center_coordinates
  @center ||= element_center(@xml.at_xpath('/screen'))
end

#element_center(element) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/icuke/screen.rb', line 25

def element_center(element)
  frame = element.at_xpath('./frame')
  
  x = frame['x'].to_f
  y = frame['y'].to_f
  
  x += (frame['width'].to_f / 2)
  y += (frame['height'].to_f / 2)
  
  return x, y
end

#exists?(text, scope = '') ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/icuke/screen.rb', line 13

def exists?(text, scope = '')
  find_element(text, scope).any?
end

#find_slider_button(element) ⇒ Object



58
59
60
61
# File 'lib/icuke/screen.rb', line 58

def find_slider_button(element)
  percentage = 0.01 * element['value'].to_f
  calculate_percentage_with_adjustment(element.child, percentage)
end

#find_slider_percentage_location(element, percentage) ⇒ Object



63
64
65
66
# File 'lib/icuke/screen.rb', line 63

def find_slider_percentage_location(element, percentage)
  percentage = 0.01 * percentage
  calculate_percentage_with_adjustment(element.child, percentage)
end

#first_slider_element(label) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/icuke/screen.rb', line 48

def first_slider_element(label)
  element =
    @xml.xpath(
      %Q{//UISlider[@label="#{label}" and frame]},
      %Q{//*[@label="#{label}"]/../UISlider}
    ).first
  raise %Q{No element labelled "#{label}" found in: #{@xml}} unless element
  element
end

#first_tappable_element(label) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/icuke/screen.rb', line 37

def first_tappable_element(label)
  element =
    @xml.xpath(
      %Q{//*[#{trait(:button, :updates_frequently, :keyboard_key)} and @label="#{label}" and frame]},
      %Q{//*[#{trait(:link)} and @value="#{label}" and frame]},
      %Q{//*[@label="#{label}" and frame]}
    ).first
  raise %Q{No element labelled "#{label}" found in: #{@xml}} unless element
  element
end

#swipe_coordinates(direction) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/icuke/screen.rb', line 72

def swipe_coordinates(direction)
  modifier = [:up, :left].include?(direction) ? -1 : 1
  x, y = center_coordinates
  x2, y2 = x, y
  if [:up, :down].include?(direction)
    y2 = y + (y * modifier)
  else
    x2 = x + (x * modifier)
  end
  return x, y, x2, y2
end

#visible?(text, scope = '') ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
# File 'lib/icuke/screen.rb', line 17

def visible?(text, scope='')
  find_element(text, scope).each do |element|
    _x, _y = element_center(element)
    return true if _x >= self.x && _y >= self.y && _x < self.width && _y < self.height
  end
  return false
end