Class: TelluriumDriver

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

Overview

Provides added functionality to Selenium WebDriver

Author

Noah Prince ([email protected])

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ TelluriumDriver

Sets up this instance of TelluriumDriver

Options

  • :browser - “chrome”,“firefox”, “safari” or “internet explorer”

  • :version - [String] the version of the browser. Not needed for safari, chrome, or firefox.

  • :hub_ip - the IP address of the Selenium Grid hub to test on.

  • :hub_url - the full url address of the Selenium Grid hub to test on

    • WARNING: DO NOT USE BOTH hub_ip and hub_url

    • NOTE: If either hub_ip or hub_url is present, tests will not run locally

  • :caps - see code.google.com/p/selenium/wiki/DesiredCapabilities

    • Not necessary, but will give the browser any extra desired functionality

  • :timeout - Number of seconds for all Tellurium wait commands. Default 120

Examples

Run a local chrome instance

TelluriumDriver.new(browser: "chrome")

Run an IE10 instance on the grid with ip 192.168.1.1

TelluriumDriver.new(browser: "internet explorer", version: "10", hub_ip: 192.168.1.1)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tellurium_driver.rb', line 43

def initialize(opts = {})
  opts[:timeout] = 120 unless opts[:timeout]
  @wait = Selenium::WebDriver::Wait.new(:timeout=>opts[:timeout])
  TelluriumDriver.wait_for_document_ready=true;

  opts[:caps] ||= {}
  opts[:caps][:browserName] ||= opts[:browser]
  opts[:caps][:version] ||= opts[:version]

  is_local = !opts[:hub_ip] and !opts[:hub_url]
  
  if is_local
    @driver = Selenium::WebDriver.for(opts[:browser].to_sym,:desired_capabilities=>opts[:caps])
  else
    @driver = Selenium::WebDriver.for(:remote,:desired_capabilities=>opts[:caps],:url=> "http://#{opts[: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



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

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.



6
7
8
# File 'lib/tellurium_driver.rb', line 6

def wait_for_document_ready
  @wait_for_document_ready
end

Class Method Details

.before(names) ⇒ Object

:nodoc:



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

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 the attribute of another element to change

* NOTE: This method only takes ids

Attributes

  • id_to_click

  • id_to_change

  • value - the attribute to change on the second element

Examples

driver.click_and_wait_to_change("checkbox","score","text")


268
269
270
271
272
273
274
275
# File 'lib/tellurium_driver.rb', line 268

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

Clicks one element and waits for another to exist

Attributes

  • id1 - Id of element to click

  • id2 - Id of element to exist

OR
  • sym1 - :id, :css, or :name, etc for the element to click

  • id1 - The text corresponding with the symbol.

  • sym2 - :id, :css, or :name, etc for the element to wait to exist

  • id2 - The text corresponding with the symbol.



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/tellurium_driver.rb', line 375

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

Attributes

  • sym - :id, :css, or :name, etc

  • id - The text corresponding with the symbol.

  • selector_value - The value to change the selector to

Examples

driver.click_selector(:id,"age","21")


288
289
290
291
# File 'lib/tellurium_driver.rb', line 288

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 in a value for form selectors and waits for another element to change

* NOTE: This method only uses ids

Attributes

  • id_to_click

  • selector_value - The value to change the selector to

  • id_to_change

  • value - the attribute to change on the second element

Examples

Waits for the attribute “available” on the element with id: “drinks” to change after filling an age selector to 21

driver.click_selector_and_wait("age","21","drinks","available")


309
310
311
312
313
314
315
316
317
# File 'lib/tellurium_driver.rb', line 309

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

Closes the webdriver



469
470
471
# File 'lib/tellurium_driver.rb', line 469

def close
  driver.quit
end

#document_readyObject



473
474
475
# File 'lib/tellurium_driver.rb', line 473

def document_ready

end

#driverObject



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

def driver
  @driver
end

#form_fillout(hash) ⇒ Object

Fills out large forms via a supplied hash in the form id,value_to_send

Attributes

  • hash - A hash of input IDs associated with the value to send to the input

Examples

Fill out a login form

driver.form_fillout({"first_name"=>"Noah",
                     "last_name"=>"Prince",
                     "email"=>"[email protected]"})


410
411
412
413
414
415
416
# File 'lib/tellurium_driver.rb', line 410

def form_fillout(hash)
  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

:nodoc:



419
420
421
422
423
424
# File 'lib/tellurium_driver.rb', line 419

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

Fills out large numbers of selectors via a supplied hash in the form id,value_to_send

Attributes

  • hash - A hash of input IDs associated with the value to send to the input

Examples

Fill out a login form

driver.form_fillout({"age"=>"21","num_of_pets"=>"2","num_of_stars"=>"5"})


437
438
439
440
441
442
443
# File 'lib/tellurium_driver.rb', line 437

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

Goes to a given url, does not wait for load

Attributes

  • url - the url to visit



87
88
89
# File 'lib/tellurium_driver.rb', line 87

def go_to(url)
  driver.get url
end

#go_to_and_wait_to_load(url) ⇒ Object

Goes to a given url, waits for it to load

* NOTE: To see if loaded, waits for the title of the window to change

Attributes

  • url - the url to visit



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

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 over where an element should be and clicks. Useful for hitting hidden elements

Attributes

  • element - Selenium::WebDriver::Element

OR
  • sym - :id, :css, or :name, etc

  • id - The text corresponding with the symbol.



329
330
331
332
333
334
335
336
337
# File 'lib/tellurium_driver.rb', line 329

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_url_and_wait_for_element(url, sym, id) ⇒ Object

Goes to a given url, and waits for a given element to appear

Attributes

  • url - the url to visit

  • sym - :id, :name, :css, etc

  • id - The text corresponding with the symbol.

Examples

driver.load_url_and_wait_for_element(:id,"password")


115
116
117
118
119
120
# File 'lib/tellurium_driver.rb', line 115

def load_url_and_wait_for_element(url,sym,id)
  current_name = driver.title
  driver.get url

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

#send_keys(sym, id, value) ⇒ Object

Fills a text input of the given sym,id with a value

Attributes

  • sym - :id, :css, or :name, etc

  • id - The text corresponding with the symbol.

  • value - the string to send to the text input



346
347
348
349
350
351
352
353
# File 'lib/tellurium_driver.rb', line 346

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

:nodoc:



356
357
358
359
# File 'lib/tellurium_driver.rb', line 356

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

Waits for the element with specified identifiers to appear, then clicks it

Attributes

  • sym - :id, :name, :css, etc

  • id - The text corresponding with the symbol.

Examples

driver.wait_and_click(:name,"hello")


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/tellurium_driver.rb', line 207

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

Waits for the element specified by the identifiers then clicks where it should be

Attributes

  • sym - :id, :css, or :name, etc

  • id - The text corresponding with the symbol.



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/tellurium_driver.rb', line 451

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 on the page

Attributes

  • element - Selenium::WebDriver::Element to appear



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/tellurium_driver.rb', line 127

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 the given element to appear and clicks it

Attributes

  • element - Selenium::WebDriver::Element to appear



241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/tellurium_driver.rb', line 241

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

Similar to wait_to_disappear, but takes an element instead of identifiers

* WARNING: If the element disappears from the DOM, and isn't just hidden
           this will raise a stale reference error

Attributes

  • element - Selenium::WebDriver::Element to appear

Examples

element = driver.find_element(id: "foo")
driver.wait_for_element_to_disappear(element)


170
171
172
173
174
175
176
177
178
# File 'lib/tellurium_driver.rb', line 170

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

#wait_to_appear(sym, id) ⇒ Object

Waits for the element with specified identifiers to appear

Attributes

  • sym - :id, :name, :css, etc

  • id - The text corresponding with the symbol.

Examples

driver.wait_to_appear(:id,"hover-box")


190
191
192
193
194
195
# File 'lib/tellurium_driver.rb', line 190

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

Attributes

  • sym - :id, :name, :css, etc

  • id - The text corresponding with the symbol.

Examples

driver.wait_to_disappear(:id,"hover-box")


151
152
153
154
155
156
# File 'lib/tellurium_driver.rb', line 151

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