Class: Calabash::Extras::Walker

Inherits:
Object
  • Object
show all
Defined in:
lib/calabash-extras/walker.rb

Constant Summary collapse

MAX_INT =
(2**(0.size * 8 -2) -1)

Instance Method Summary collapse

Constructor Details

#initialize(sleep_interval, matrix, log_callback) ⇒ Walker

Returns a new instance of Walker.



9
10
11
12
13
# File 'lib/calabash-extras/walker.rb', line 9

def initialize(sleep_interval, matrix, log_callback)
  @sleep_interval = sleep_interval
  @matrix = matrix
  @logger = log_callback
end

Instance Method Details

#find_path(start, finish) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/calabash-extras/walker.rb', line 50

def find_path(start, finish)
  distances = {}
  previous = {}
  nodes = PriorityQueue.new

  @matrix.each_key do |point|
    if point == start
      distances[point] = 0
      nodes[point] = 0
    else
      distances[point] = MAX_INT
      nodes[point] = MAX_INT
    end
    previous[point] = nil
  end

  while nodes
    smallest = nodes.delete_min_return_key

    if smallest == finish
      path = []
      while previous[smallest]
        path.push(smallest)
        smallest = previous[smallest]
      end
      return path.push(start).reverse
    end

    if smallest.nil? or distances[smallest] == MAX_INT
      break
    end

    @matrix[smallest].each_key do |neighbor|
      alt = distances[smallest] + 1
      if alt < distances[neighbor]
        distances[neighbor] = alt
        previous[neighbor] = smallest
        nodes[neighbor] = alt
      end
    end
  end
  distances.inspect
end

#get_current_pageObject



35
36
37
38
39
# File 'lib/calabash-extras/walker.rb', line 35

def get_current_page
  sleep @sleep_interval
  all_page_elements = @matrix.keys.first.all_elements
  @matrix.keys.find { |page| page.match all_page_elements} || raise('Unable to determine current page') #todo replace with match method
end

#go(to) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/calabash-extras/walker.rb', line 15

def go(to)
  start_point = get_current_page

  @end_point = to

  raise 'Page "%s" does not exist' % [to] if @matrix[to].nil?

  return if start_point == @end_point

  path = find_path start_point, @end_point

  raise 'Unable to find path from "%s" to "%s"' % [start_point, @end_point] if path.last != @end_point

  resolve_path path
end

#lastObject



31
32
33
# File 'lib/calabash-extras/walker.rb', line 31

def last
  @end_point
end

#resolve_path(path) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/calabash-extras/walker.rb', line 41

def resolve_path(path)
  raise 'Empty path given' if path.empty?

  for i in 0..(path.length - 2)
    @logger.call('transition from %s to %s' % [path[i], path[i+1]])
    @matrix[path[i]][path[i+1]].call
  end
end