Class: Rufus::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/rufus/driver.rb

Instance Method Summary collapse

Constructor Details

#initializeDriver

Returns a new instance of Driver.



11
12
13
14
15
# File 'lib/rufus/driver.rb', line 11

def initialize
  raise 'No config.yml found' if !File.exists?('config.yml')
  @config = YAML.load(ERB.new(File.read('config.yml')).result)
  @url = url(@config)
end

Instance Method Details

#alert_shown?Boolean

Returns:

  • (Boolean)


141
142
143
144
145
146
147
148
# File 'lib/rufus/driver.rb', line 141

def alert_shown?
  all_elements.each do |element|
    if is_alert?(element)
      return true
    end
  end
  false
end

#all_elementsObject



162
163
164
# File 'lib/rufus/driver.rb', line 162

def all_elements
  elements_by_tag('UIAElement')
end

#buttonsObject



85
86
87
88
89
90
91
92
# File 'lib/rufus/driver.rb', line 85

def buttons
  buttons = []
  elements = elements_by_tag 'UIAButton'
  elements.each do |element|
    buttons << element.text
  end
  buttons
end

#cells(locator) ⇒ Object



44
45
46
47
48
# File 'lib/rufus/driver.rb', line 44

def cells(locator)
  element = find(locator)
  raise 'Expected view to be of type UIATableView' unless element.tag_name.eql? 'UIATableView'
  element.find_elements(:tag_name, 'UIATableCell')
end

#class_for(element) ⇒ Object



150
151
152
# File 'lib/rufus/driver.rb', line 150

def class_for(element)
  element.tag_name
end

#click(locator) ⇒ Object



50
51
52
# File 'lib/rufus/driver.rb', line 50

def click(locator)
  find(locator).click
end

#click_alert(button) ⇒ Object



135
136
137
138
139
# File 'lib/rufus/driver.rb', line 135

def click_alert(button)
  if alert_shown?
    click_alert_button(button)
  end
end

#configObject



26
27
28
# File 'lib/rufus/driver.rb', line 26

def config
  @config
end

#displayed?(locator) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rufus/driver.rb', line 62

def displayed?(locator)
  find(locator).displayed?
end

#elements_by_tag(name) ⇒ Object



166
167
168
# File 'lib/rufus/driver.rb', line 166

def elements_by_tag(name)
  driver.find_elements(:tag_name, name)
end

#enabled?(locator) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rufus/driver.rb', line 58

def enabled?(locator)
  find(locator).enabled?
end

#find(locator) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/rufus/driver.rb', line 34

def find(locator)
  how = locator.keys[0].to_sym
  what = locator[how]
  begin
    driver.find_element(how, what)
  rescue Selenium::WebDriver::Error::NoSuchElementError
    return nil
  end
end

#find_alert(locator) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/rufus/driver.rb', line 125

def find_alert(locator)
  alert = nil
  all_elements.each do |element|
    if is_alert?(element)
      alert = element if match?(element, locator[:name])
    end
  end
  alert
end

#labelsObject



103
104
105
106
107
108
109
110
# File 'lib/rufus/driver.rb', line 103

def labels
  labels = []
  elements = elements_by_tag 'UIAStaticText'
  elements.each do |element|
    labels << element.text
  end
  labels
end

#match?(element, name) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/rufus/driver.rb', line 154

def match?(element, name)
  element.attribute(:name).eql? name
end

#orientationObject



66
67
68
# File 'lib/rufus/driver.rb', line 66

def orientation
  driver.orientation.to_s
end

#page_sourceObject



158
159
160
# File 'lib/rufus/driver.rb', line 158

def page_source
  driver.page_source
end

#press_button(name) ⇒ Object



54
55
56
# File 'lib/rufus/driver.rb', line 54

def press_button name
  click(:name => name)
end

#quitObject



21
22
23
24
# File 'lib/rufus/driver.rb', line 21

def quit
  driver.quit
  @selenium = nil
end

#rotate(orientation) ⇒ Object



70
71
72
# File 'lib/rufus/driver.rb', line 70

def rotate(orientation)
  driver.rotate orientation
end

#scroll_to(locator) ⇒ Object



170
171
172
173
# File 'lib/rufus/driver.rb', line 170

def scroll_to(locator)
  id = find(locator).ref
  driver.execute_script 'mobile: scrollTo', {'element' => id}
end

#sequence(*names, times) ⇒ Object



81
82
83
# File 'lib/rufus/driver.rb', line 81

def sequence(*names, times)
  timed_sequence(names, times, 1)
end

#server_urlObject



30
31
32
# File 'lib/rufus/driver.rb', line 30

def server_url
  @url
end

#startObject



17
18
19
# File 'lib/rufus/driver.rb', line 17

def start
  driver.get @url
end

#text_fieldsObject



94
95
96
97
98
99
100
101
# File 'lib/rufus/driver.rb', line 94

def text_fields
  fields = []
  elements = elements_by_tag 'UIATextField'
  elements.each do |element|
    fields << element.text
  end
  fields
end

#timed_sequence(names, times, seconds) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rufus/driver.rb', line 112

def timed_sequence(names, times, seconds)
  current = 0
  until current == times
    names.each do |name|
      click(:name => name)
      sleep seconds

    end
    current += 1
    puts "sequence #{current} completed"
  end
end

#type(keys, name) ⇒ Object



74
75
76
77
78
79
# File 'lib/rufus/driver.rb', line 74

def type(keys, name)
  element = find(:name => name)
  element.click
  sleep 1
  element.send_keys keys
end