Class: TelluriumDriver

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

Overview

Provides added functionality to Selenium WebDriver

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TelluriumDriver

Returns a new instance of TelluriumDriver.

Parameters:

  • browser, (String)

    “chrome”,“firefox”, or “internet explorer”

  • version, (Integer)

    the version of the browser. nil for firefox or chrome, or “local” to run locally

  • hub_ip (String)

    the IP address of the Selenium Grid hub to test on

  • timeout, (Integer)

    the timeout to use on test



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tellurium_driver.rb', line 23

def initialize(*args)
  browser, version,hub_ip,timeout = args
  timeout = 120 unless timeout
  @wait = Selenium::WebDriver::Wait.new(:timeout=>timeout)
  TelluriumDriver.wait_for_document_ready=true;
  
  is_local = version.include?("local") if version and version.is_a? String
  is_ie = browser.include?("internet") && version
  is_chrome = browser.include?("chrome")
  is_firefox = browser.include?("firefox") 

  if is_chrome && is_local
    caps = {
      :browserName => "chrome",
      :idleTimeout => timeout
      # :screenshot => true
    }
    @driver = Selenium::WebDriver.for :chrome,:desired_capabilities=>caps
  elsif is_firefox && is_local
    caps = {
      :browserName => "firefox",
      :idleTimeout => timeout,
      :screenshot => true
    }
    @driver = Selenium::WebDriver.for :firefox, :desired_capabilities=>caps
  elsif is_ie
    caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer
    caps.version = version    
    @driver = Selenium::WebDriver.for(:remote,:desired_capabilities=>caps,:url=> "http://#{hub_ip}:4444/wd/hub")
  elsif is_chrome
    @driver = Selenium::WebDriver.for(:remote,:desired_capabilities=>:chrome,:url=> "http://#{hub_ip}:4444/wd/hub")
  elsif is_firefox
    @driver = Selenium::WebDriver.for(:remote,:desired_capabilities=>:firefox,:url=> "http://#{hub_ip}:4444/wd/hub")
  end

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



77
78
79
# File 'lib/tellurium_driver.rb', line 77

def method_missing(sym, *args, &block)
  @driver.send sym,*args,&block
end

Class Attribute Details

.wait_for_document_readyObject

Returns the value of attribute wait_for_document_ready.



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

def wait_for_document_ready
  @wait_for_document_ready
end

Class Method Details

.before(names) ⇒ Object

takes browser name, browser version, hub ip(optional)



8
9
10
11
12
13
14
15
16
# File 'lib/tellurium_driver.rb', line 8

def self.before(names)
  names.each do |name|
    m = instance_method(name)
    define_method(name) do |*args, &block|  
      yield
      m.bind(self).(*args, &block)
    end
  end
end

Instance Method Details

#click_and_wait_to_change(id_to_click, id_to_change, value) ⇒ Object

clicks one element and waits for another one to change it’s value.

Parameters:

  • id_to_click, (String)

    the id you want to click

  • id_to_change (String)

    you



107
108
109
110
111
112
113
114
# File 'lib/tellurium_driver.rb', line 107

def click_and_wait_to_change(id_to_click,id_to_change,value) 
  element_to_click = driver.find_element(:id, id_to_click)
  element_to_change = driver.find_element(:id, id_to_change)
  current_value = element_to_change.attribute(value.to_sym)

  element_to_click.click
  @wait.until { element_to_change.attribute(value.to_sym) != current_value }
end

#click_and_wait_to_exist(*args) ⇒ Object

takes the id you want to click, the id you want to exist.(Example, I click start-application and wait for the next dialogue box to exist



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/tellurium_driver.rb', line 261

def click_and_wait_to_exist(*args) 
   case args.size
   when 2 #takes just two id's
     id1,id2 = args
      element_to_click = driver.find_element(:id => id1)
      element_to_change = driver.find_element(:id => id2)

      element_to_click.click
      wait_for_element(element_to_change)

   when 4 #takes sym,sym2,id1,id2
     sym,sym2,id1,id2 = args
      element_to_click = driver.find_element(sym => id)
      element_to_change = driver.find_element(sym2 => id2)

      element_to_click.click
      wait_for_element(element_to_change)

   else
      error
   end
end

#click_selector(sym, id, selector_value) ⇒ Object

Fills in a value for form selectors

Parameters:

  • sym, (Symbol)

    usually :id, :css, or :name

  • selector_value, (String)

    the value to set the selector to



119
120
121
122
# File 'lib/tellurium_driver.rb', line 119

def click_selector(sym,id,selector_value)
  option = Selenium::WebDriver::Support::Select.new(driver.find_element(sym.to_sym,id))
  option.select_by(:text, selector_value)
end

#click_selector_and_wait(id_to_click, selector_value, id_to_change, value) ⇒ Object

Fills out a selector and waits for another id to change



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

def click_selector_and_wait(id_to_click,selector_value,id_to_change,value)
  element_to_change = driver.find_element(:id => id_to_change)
  current_value = element_to_change.attribute(value.to_sym)

  option = Selenium::WebDriver::Support::Select.new(driver.find_element(:id => id_to_click))
  option.select_by(:text, selector_value)
    
  @wait.until { element_to_change.attribute(value.to_sym) != current_value }
end

#closeObject



324
325
326
# File 'lib/tellurium_driver.rb', line 324

def close
  driver.quit
end

#document_readyObject



328
329
330
# File 'lib/tellurium_driver.rb', line 328

def document_ready

end

#driverObject



72
73
74
# File 'lib/tellurium_driver.rb', line 72

def driver
  @driver
end

#form_fillout(hash) ⇒ Object

takes a hash of ID’s with values to fillout



284
285
286
287
288
289
290
# File 'lib/tellurium_driver.rb', line 284

def form_fillout(hash) #takes a hash of ID's with values to fillout
  hash.each do |id,value|
    self.wait_to_appear(:id,id)
     self.send_keys(:id,id,value)
  end

end

#form_fillout_editable(selector, hash) ⇒ Object



292
293
294
295
296
297
# File 'lib/tellurium_driver.rb', line 292

def form_fillout_editable(selector,hash)
  hash.each do |id,value|
    name = "#{selector}\[#{id}\]"
    driver.execute_script("$('[data-field=\"#{name}\"]').editable('setValue', '#{value}');")
  end
end

#form_fillout_selector(hash) ⇒ Object



299
300
301
302
303
304
305
# File 'lib/tellurium_driver.rb', line 299

def form_fillout_selector(hash)
  hash.each do |id,value|
    option = Selenium::WebDriver::Support::Select.new(driver.find_element(:id => id))
    option.select_by(:text,value)
  end

end

#go_to(url) ⇒ Object

Parameters:

  • url, (String)

    the url to point the driver at



82
83
84
# File 'lib/tellurium_driver.rb', line 82

def go_to(url)
  driver.get url
end

#go_to_and_wait_to_load(url) ⇒ Object

Waits for the title to change after going to a url

Parameters:

  • url, (String)

    the url to go to



88
89
90
91
92
93
94
# File 'lib/tellurium_driver.rb', line 88

def go_to_and_wait_to_load(url)
  current_name = driver.title
  driver.get url

  #wait until the current title changes to see that you're at a new url
  @wait.until { driver.title != current_name }
end

#hover_click(*args) ⇒ Object

hovers where the element is and clicks. Workaround for timeout on click that happened with the new update to the showings app

Parameters:

  • element (SeleniumWebdriver::Element)

    OR can take sym,id



232
233
234
235
236
237
238
239
240
# File 'lib/tellurium_driver.rb', line 232

def hover_click(*args)
  if args.size == 1
  driver.action.click(element).perform
  else
    sym,id = args
    driver.action.click(driver.find_element(sym.to_sym,id)).perform
  end

end

#load_website_and_wait_for_element(url, arg1, id) ⇒ Object

takes a url, the symbol of what you wait to exist and the id/name/xpath, whatever of what you want to wait to exist



97
98
99
100
101
102
# File 'lib/tellurium_driver.rb', line 97

def load_website_and_wait_for_element(url,arg1,id)
  current_name = driver.title
  driver.get url

  @wait.until { driver.title != current_name and driver.find_elements(arg1, id).size > 0 }
end

#send_keys(sym, id, value) ⇒ Object

fills an input of the given sym,id with a value

Parameters:

  • sym (Symbol)
  • id (String)
  • value (String)


246
247
248
249
250
251
252
253
# File 'lib/tellurium_driver.rb', line 246

def send_keys(sym,id,value)
  #self.wait_and_click(sym, id)
  #driver.action.send_keys(driver.find_element(sym => id),value).perform
  wait_to_appear(sym,id)
  element = driver.find_element(sym,id)
  element.click
  element.send_keys(value)
end

#send_keys_leasing(sym, id, value) ⇒ Object



255
256
257
258
# File 'lib/tellurium_driver.rb', line 255

def send_keys_leasing(sym,id,value)
  self.wait_to_appear(sym,id)
  self.driver.find_element(sym,id).send_keys(value) 
end

#wait_and_click(sym, id) ⇒ Object

to change. (Example, I click start-application and wait for the next dialogue box to not be hidden)



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/tellurium_driver.rb', line 184

def wait_and_click(sym, id)
 found_element = false

  #if the specified symbol and id point to multiple elements, we want to find the first visible one
  #and click that
  @wait.until do
    driver.find_elements(sym,id).shuffle.each do |element|
      if element.displayed? and element.enabled?
        @element=element
        found_element = true
      end 
    
    end
   found_element   
  end

  i = 0

  begin
    @element.click
  rescue Exception => ex
    i+=1
    sleep(1)
    retry if i<20
    raise ex 
  end
  
end

#wait_and_hover_click(sym, id) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/tellurium_driver.rb', line 307

def wait_and_hover_click(sym,id)
  found = false
#wait until an element with sym,id is displayed. When it is, hover click it
  @wait.until {
    elements = driver.find_elements(sym, id)
    elements.each do |element| 
      if element.displayed?
        found = true
        @element = element
      end
  
     end
    found == true
  } 
  self.hover_click(@element)
end

#wait_for_element(element) ⇒ Object

Waits for an element to be displayed

Parameters:

  • (SeleniumWebdriver::Element)


137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/tellurium_driver.rb', line 137

def wait_for_element(element)
    @wait.until {
        bool = false

         if(element.displayed?)
            bool = true
            @element = element
            break
         end       

       bool == true
    }
end

#wait_for_element_and_click(element) ⇒ Object

Waits for an element to be displayed and click it

Parameters:

  • (SeleniumWebdriver::Element)


215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/tellurium_driver.rb', line 215

def wait_for_element_and_click(element)
  wait_for_element(element)
  
  i = 0
  begin
    @element.click
  rescue Exception => ex
    i+=1
    sleep(1)
    retry if i<20
    raise ex 
  end
  
end

#wait_for_element_to_disappear(element) ⇒ Object

waits for an element to disappear

Parameters:

  • element (SeleniumWebdriver::Element)


163
164
165
166
167
168
169
170
171
# File 'lib/tellurium_driver.rb', line 163

def wait_for_element_to_disappear(element)
  @wait.until {
    begin
      !element.displayed?
    rescue
      break
    end
  }
end

#wait_to_appear(sym, id) ⇒ Object

Parameters:

  • sym (Symbol)
  • id, (String)

    the string associated with the symbol to identify the elementw



175
176
177
178
179
180
# File 'lib/tellurium_driver.rb', line 175

def wait_to_appear(sym,id)
 @wait.until {
  element_arr = driver.find_elements(sym,id)
  element_arr.size > 0 and element_arr[0].displayed? #wait until the element both exists and is displayed
 }
end

#wait_to_disappear(sym, id) ⇒ Object

Waits for the element with specified identifiers to disappear

Parameters:

  • sym (Symbol)
  • id (String)


154
155
156
157
158
159
# File 'lib/tellurium_driver.rb', line 154

def wait_to_disappear(sym,id)
 @wait.until {
  element_arr = driver.find_elements(sym,id)
  element_arr.size > 0 and !element_arr[0].displayed? #wait until the element both exists and is displayed
}
end