Class: Selenium::WebDriver::Driver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_locatorObject (readonly)

Returns the value of attribute last_locator.



98
99
100
# File 'lib/oats/oats_selenium_api.rb', line 98

def last_locator
  @last_locator
end

#no_oats_debugObject

Returns the value of attribute no_oats_debug.



97
98
99
# File 'lib/oats/oats_selenium_api.rb', line 97

def no_oats_debug
  @no_oats_debug
end

#oselObject

Returns the value of attribute osel.



97
98
99
# File 'lib/oats/oats_selenium_api.rb', line 97

def osel
  @osel
end

Instance Method Details

#alert(msg = nil, action = 'accept') ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/oats/oats_selenium_api.rb', line 114

def alert(msg = nil, action = 'accept')
  return unless webdriver?
#    selenium.sleep 1
  alert = switch_to.alert;
  txt = alert.text
  if msg
    oats_debug "#{action}ing alert [#{txt}]"
    #        Oats.assert txt =~ /#{msg}/, "Alert text does not match input msg: [#{msg}]"
    Oats.assert(txt == msg, "Alert text does not match input msg: [#{msg}]")
  end
  alert.send(action);
end

#checked?(locator) ⇒ Boolean Also known as: is_checked

Returns:

  • (Boolean)


138
139
140
141
142
143
# File 'lib/oats/oats_selenium_api.rb', line 138

def checked?(locator)
  oats_debug "checked? #{locator}"
  return is_checked_orig(locator) ? locator : false unless webdriver?
  loc = element?(locator)
  loc.element.selected? ? loc : false
end

#chrome?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/oats/oats_selenium_api.rb', line 163

def chrome?
  Oats.data('selenium.browser_type') =~ /chrome/
end

#click(locator) ⇒ Object Also known as: mouse_down



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/oats/oats_selenium_api.rb', line 146

def click(locator)
  oats_debug "click #{locator}"
  #      if webdriver?
  el = element?(locator)
  Oats.assert el, "No element is found at locator #{locator}"
  el.click
  #      else
  #        click_orig(locator)
  #        el = locator
  #      end
  el
end

#cookiesObject



619
620
621
622
623
624
625
626
# File 'lib/oats/oats_selenium_api.rb', line 619

def cookies
  all_my_cookies = selenium.manage.all_cookies
  cookies_str = ""
  all_my_cookies.each do |my_cookie|
    cookies_str = cookies_str + my_cookie[:name].to_s + "=" + my_cookie[:value].to_s + "; "
  end
  return cookies_str
end

#delete_all_cookiesObject



632
633
634
# File 'lib/oats/oats_selenium_api.rb', line 632

def delete_all_cookies
  selenium.manage.delete_all_cookies
end


628
629
630
# File 'lib/oats/oats_selenium_api.rb', line 628

def delete_cookie(cookie, options)
  selenium.manage.delete_cookie(cookie)
end

#element?(*locator_args) ⇒ Boolean

Somehow the rescue below doesn’t work if I move this code to Oats::Selenium::Api Returns first Locator matching locator or nil. See find_element for ehow choices. Input parameter is one or an array of locator inputs.

Returns:

  • (Boolean)


556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/oats/oats_selenium_api.rb', line 556

def element?(*locator_args)
  return @last_locator if locator_args.empty?
  found = nil
  locator_args.each do |loc|
    begin
      if loc.instance_of?(String) or loc.instance_of?(Array)
        locator = Oats::Locator.new(loc)
      else
        locator = loc
      end
      locator.element = find_element(locator.ehow, locator.what)
      if locator.element
        found = locator
      end
    rescue Selenium::WebDriver::Error::NoSuchElementError
    end
  end
  return @last_locator = found
end

#element_size(locator) ⇒ Object



610
611
612
613
# File 'lib/oats/oats_selenium_api.rb', line 610

def element_size(locator)
  el_size = selenium.find_element(:xpath, locator).size
  img_size = el_size.width.to_s + "x" + el_size.height.to_s
end

#elements(*locator_args) ⇒ Object

Returns array of elements matching locator. Last locator is first matching elemnet



578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/oats/oats_selenium_api.rb', line 578

def elements(*locator_args)
  return @last_locator if locator_args.empty?
  if locator_args[0].instance_of?(Oats::Locator)
    locator = locator_args[0]
  else
    locator = Oats::Locator.new(*locator_args)
  end
  elements = find_elements(locator.ehow, locator.what)
  #    elements.empty?
  locator.element = elements
  @last_locator = locator
  return elements

end

#firefox?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/oats/oats_selenium_api.rb', line 167

def firefox?
  Oats.data('selenium.browser_type') =~ /firefox/
end

#get_attribute(locator) ⇒ Object



547
548
549
550
551
# File 'lib/oats/oats_selenium_api.rb', line 547

def get_attribute(locator)
  oats_debug "get_attribute #{locator}"
  loc, val = locator.split(/\/@/)
  element?(loc).attribute(val)
end


615
616
617
# File 'lib/oats/oats_selenium_api.rb', line 615

def get_cookie_by_name(cookie)
  selenium.manage.cookie_named(cookie)
end

#get_row(locator, col_tag) ⇒ Object



237
238
239
240
241
# File 'lib/oats/oats_selenium_api.rb', line 237

def get_row(locator,col_tag)
  locator = locator + '//' + col_tag
  arr = elements(locator).collect{|el| el.text}
  return arr
end

#get_table(locator) ⇒ Object



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

def get_table(locator)
  arr = []
  selenium.wait_for_element("//div[@id='#{locator}']")
  arr[0] = get_row("//div[@id='#{locator}']//thead/tr",'th[not(contains(@style,"display: none;"))]')
  for row in 1..100
    #        loc = "//div[@id='#{locator}']//tbody/tr[#{row}]"
    row_array = get_row("//div[@id='#{locator}']//tbody/tr[#{row}]",'td[not(contains(@style,"display: none;"))]')
    break if row_array.empty?
    arr[row] = row_array
  end
  return arr
end

#gettagid(locator, select_tag = 'option', attribute = 'text') ⇒ Object

Raises:



214
215
216
217
218
219
# File 'lib/oats/oats_selenium_api.rb', line 214

def gettagid(locator, select_tag = 'option', attribute = 'text')
  oats_debug "Store #{locator} ids by tag_name #{select_tag}"
  el = element?(locator)
  raise OatsTestError, "Could not find #{locator} among options:" if !el
  el.find_elements(:tag_name,select_tag).collect { |opt| opt.attribute(attribute)}
end

#ie?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/oats/oats_selenium_api.rb', line 159

def ie?
  Oats.data('selenium.browser_type') =~ /ie/
end

#login(*args) ⇒ Object



109
110
111
112
# File 'lib/oats/oats_selenium_api.rb', line 109

def (*args)
  osel.(*args)
  return self
end

#logoutObject



104
105
106
107
# File 'lib/oats/oats_selenium_api.rb', line 104

def logout
  osel.logout
  return self
end


598
599
600
# File 'lib/oats/oats_selenium_api.rb', line 598

def navigate_back
  selenium.navigate.back
end

#oats_assert_text(text_string) ⇒ Object

Makes an Oats.assert based on text? existence.



128
129
130
# File 'lib/oats/oats_selenium_api.rb', line 128

def oats_assert_text(text_string)
  Oats.assert $selenium.text?(text_string), "Missing message: #{text_string}"
end

#oats_debug(output) ⇒ Object



491
492
493
494
# File 'lib/oats/oats_selenium_api.rb', line 491

def oats_debug(output)
  options ||= {}
  Oats.debug('Webdriver '+output) unless @no_oats_debug
end

#open(url) ⇒ Object



226
227
228
229
230
231
232
233
234
235
# File 'lib/oats/oats_selenium_api.rb', line 226

def open(url)
  url = Oats::Oselenium.remote_webdriver_map_file_path(url)
  if @osel..nil? or @osel. == @osel.base_url or @osel. =~ /http.*/
    oats_debug "opening URL [#{url}]"
  else
    url = @osel. + url
    oats_debug "opening non-base URL [#{url}]"
  end
  navigate.to(url)
end

#refreshObject



533
534
535
536
# File 'lib/oats/oats_selenium_api.rb', line 533

def refresh
  oats_debug "refresh page"
  navigate.refresh
end

#run_script(*args) ⇒ Object



543
544
545
# File 'lib/oats/oats_selenium_api.rb', line 543

def run_script(*args)
  execute_script(*args)
end

#scroll(locator) ⇒ Object

Scrolls till element is visible.



133
134
135
136
# File 'lib/oats/oats_selenium_api.rb', line 133

def scroll(locator)
  loc = element?(locator)
  loc.location_once_scrolled_into_view
end

#select(locator, value, noscript = nil, select_tag = 'option') ⇒ Object

select

Raises:



172
173
174
175
176
177
178
179
180
181
182
183
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
212
# File 'lib/oats/oats_selenium_api.rb', line 172

def select(locator, value, noscript = nil, select_tag = 'option')
  oats_debug "select #{value} at #{locator} by tag_name #{select_tag}"
  #      unless webdriver?
  #        select_orig(locator, value)
  #        return locator
  #      end
  attribute, val = value.split(/=/)
  options = []
  if val
    attribute = 'text' if attribute == 'label'
  else
    val = attribute
    attribute = 'text'
  end
  if val.index("*")
    regex = true
  end
  el = element?(locator)
  if el
    el.find_elements(:tag_name,select_tag).each do |opt|
      attr_value = opt.attribute(attribute)
      match = if regex
        attr_value =~ /#{val}/
      else
        attr_value == val
      end
      #          if attr_value == val
      #              el.click
      if match
        loc = locator.to_s.split(/'/)
        opt.click
        unless noscript
          selenium.run_script("$('##{loc[1]}').change()") if selenium.ie? and loc[1]
        end
        return opt
      end
      options.push attr_value
    end
  end
  raise OatsTestError, "Could not find #{val} among options: #{options.inspect}"
end

#select_frame(locator) ⇒ Object



593
594
595
596
# File 'lib/oats/oats_selenium_api.rb', line 593

def select_frame(locator)
  fid = selenium.get_attribute locator+'/@id'
  selenium.switch_to.frame(fid)
end

#sleep(seconds, browser = nil) ⇒ Object



496
497
498
499
500
501
502
503
# File 'lib/oats/oats_selenium_api.rb', line 496

def sleep(seconds, browser = nil)
  return if Oats.data('selenium.no_extra_sleep')
  browser_match = browser.nil? or Oats.data('selenium.browser_type') =~ /#{browser}/
  if browser_match
    oats_debug "sleep #{seconds} extra seconds"
    Kernel.sleep seconds
  end
end

#style(locator, prop_value) ⇒ Object



636
637
638
# File 'lib/oats/oats_selenium_api.rb', line 636

def style(locator, prop_value)
  element(locator).style(prop_value)
end

#switch_to_default_contentObject



606
607
608
# File 'lib/oats/oats_selenium_api.rb', line 606

def switch_to_default_content
  selenium.switch_to.default_content
end

#text(locators) ⇒ Object

Returns nil, text, or array of texts found at locator(s)



303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/oats/oats_selenium_api.rb', line 303

def text(locators)
  #      txt = if webdriver?
  texts = elements(locators).collect{|el| el.text}
  txt = case texts.size
  when 0 then nil
  when 1 then texts[0]
  else texts
  end
  #      else
  #        text_orig(locators)
  #      end
  oats_debug "text(s) at #{locators}: #{txt.inspect}"
  txt
end

#text?(value, *locators) ⇒ Boolean

Returns locator if /value/ =~ text in any of the locators, or

false if element is not found, nil otherwise

Returns:

  • (Boolean)


257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/oats/oats_selenium_api.rb', line 257

def text?(value, *locators)
  locators.flatten!
  found = nil
  value_reg = value.instance_of?(Regexp) ? value : /#{value}/
  if locators.empty?
    Oats.warn "Please include a locator to more speficifically locate text: [#{value}]"
    #        if webdriver?
    locators = [Locator.new('body',:tag_name)]
    #        else
    #          found = is_text_orig(value)
    #        end
  end
  locator = false
  found = locators.find do |loc|
    #        if webdriver?
    elems = elements(loc)
    if elems and !elems.empty?
      elems.find { |el|
        if el.displayed?
          locator = true
          value_reg =~ el.text
        end
      }
    end
    #        else
    #          if loc
    #            begin
    #              if element?(loc) and visible?(loc)
    #                locator = true
    #                value_reg =~ text_orig(loc)
    #              end
    #            rescue  Selenium::CommandError => exc
    #              raise unless exc.message =~ /Element .* not found/
    #            end
    #          end
    #        end
  end if found.nil?
  oats_debug('text? '+(found ? 'found' : 'could not locate' ) +" [#{value}] at #{locators}")
  if found
    found
  else
    locator ? nil : false
  end
end

#type(locator, value, retype = false, delay = false) ⇒ Object Also known as: key_press

Complete the text in locator to value if necessary and return locator If webdriver, set retype to true if you want to clear the input and retype. If you just want to type value without clearing the element, set retype=>nil.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/oats/oats_selenium_api.rb', line 321

def type(locator, value, retype = false, delay = false)
  value = value.to_s
  #      if webdriver?
  locator = element?(locator)
  el = locator.element
  if retype
    xtra = value
    locator.element.clear
  else
    old = el[:value]
    xtra = value.sub(/^#{old}/,'')
    el.clear if xtra == value and old != '' and !retype.nil?
  end
  if xtra == ''
    if old == ''
      oats_debug "type skipping already existing '#{value}' at #{locator}"
    elsif xtra == value
      oats_debug "type cleared already existing '#{old}' at #{locator}"
    end
  else
    oats_debug "type '#{xtra}' at #{locator}"
    if delay
      xtra = xtra.split(//)
      for i in 0..xtra.length
#          selenium.sleep 1
        el.send_keys(xtra[i])
      end
    else
      el.send_keys(xtra)
    end
  end
  #      else
  #        locator ||= @last_locator
  #        oats_debug "type '#{value}' at #{locator}"
  #        type_orig(locator, value)
  #      end
  return locator
end

#type_keys(locator, value, retype = false) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/oats/oats_selenium_api.rb', line 367

def type_keys(locator, value, retype = false)
  #      if webdriver?
  type(locator, value, retype)
  #      else
  #        if retype
  #          oats_debug "type_keys cleared already existing text at #{locator} "
  #          type_orig(locator,"")
  #        end
  #        oats_debug "type_keys '#{value}' at #{locator} "
  #        type_keys_orig(locator, value)
  #      end
end

#type_return(locator) ⇒ Object

Press the return key into the input field



381
382
383
384
385
386
387
# File 'lib/oats/oats_selenium_api.rb', line 381

def type_return(locator)
  #      if webdriver?
  type(locator,"\n")
  #      else
  #        key_press(locator||@last_locator, "\\13")
  #      end
end

#wait_and_click(locator, options = {}) ⇒ Object

Waits as in wait_for_element and then clicks on the locator If wait_for_element succeeds, returns locator, else nil



391
392
393
394
395
396
# File 'lib/oats/oats_selenium_api.rb', line 391

def wait_and_click(locator, options = {})
#    options[:extra_seconds] ||= 1 if selenium.chrome?
  loc = wait_for_element(locator, options)
  return nil unless loc
  click(loc)
end

#wait_and_mouse_over(locator) ⇒ Object

Clicks on the element if running webdriver, else does a selenium mouse_over after waiting for element.



399
400
401
402
403
404
405
406
# File 'lib/oats/oats_selenium_api.rb', line 399

def wait_and_mouse_over(locator)
  #      if webdriver?
  wait_and_click(locator)
  #      else
  #        wait_for_element(locator)
  #        selenium.mouse_over(locator)
  #      ends
end

#wait_and_select(locator, value, noscript = nil, select_tag = 'option') ⇒ Object

wait_for_element followed by a select



222
223
224
# File 'lib/oats/oats_selenium_api.rb', line 222

def wait_and_select(locator, value, noscript = nil, select_tag = 'option')
  select(wait_for_element(locator), value, noscript, select_tag)
end

#wait_and_type(locator, value, options = nil, delay = nil) ⇒ Object



360
361
362
363
364
365
# File 'lib/oats/oats_selenium_api.rb', line 360

def wait_and_type(locator, value,  options = nil, delay = nil)
  options ||= {}
  retype =  options.delete(:retype) if options.key?(:retype)
  loc = wait_for_element(locator, options)
  type(loc, value, retype, delay)
end

#wait_for_element(locators, *options) ⇒ Object

Returns Locator if found in any of the locator(s array), or raises OatsTestError Sets selenium.last_locator and selenium.last_element See Oats.wait_until for definition of options hash, Waits for options if specified



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/oats/oats_selenium_api.rb', line 438

def wait_for_element(locators, *options)
  locators = [locators] unless locators.instance_of?(Array)
  wait_opts = {}
  while o = options.shift
    if o.instance_of?(Hash)
      wait_opts = o
    else
      locators.push o
    end
  end
  wait_opts[:message] ||= "Could not find locator: #{locators}"
  extra_seconds = wait_opts.delete(:extra_seconds) if wait_opts[:extra_seconds]
  time = wait_opts[:seconds] ? "#{wait_opts[:seconds].to_s} seconds " : ''
  oats_debug "wait #{time}for #{locators}"
  @last_locator = nil
  found = nil
  save_debug = @no_oats_debug
  Oats.wait_until wait_opts do
    locators.find do |loc|
      found = element?(loc)
      #          if webdriver?
      found = nil if found and not found.element.displayed?
      #          else
      #            begin
      #              if found and selenium.visible?(loc)
      #                found = loc
      #              else
      #                found = nil
      #              end
      #            rescue  Selenium::CommandError => exc
      #              found = nil unless exc.message =~ /Element .* not found/
      #            end
      #          end
      if found and block_given?
        @no_oats_debug = true
        yield found
      else
        found
      end
    end
  end
  @no_oats_debug = save_debug
  oats_debug "found locator: #{found}" if found and locators.size > 1
  if extra_seconds && !Oats.data('selenium.no_extra_sleep')
    oats_debug "is waiting #{extra_seconds} extra seconds for #{locators}"
    sleep extra_seconds
  end
  @last_locator = found if found
  return found
ensure
  @no_oats_debug = save_debug
end

#wait_for_page_to_loadObject



602
603
604
# File 'lib/oats/oats_selenium_api.rb', line 602

def wait_for_page_to_load
  return
end

#wait_for_text(value, locators, options = nil) ⇒ Object

Waits using Oats.wait_until the value matches any of the locators If succeeds returns locator, else registers Oats.error and returns false options

:is_anywhere => true  Will return true if text appears anywhere on the page


412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/oats/oats_selenium_api.rb', line 412

def wait_for_text(value, locators, options = nil)
  options ||= {}
  skip_error =  options.delete(:skip_error) if options.key?(:skip_error)
  is_anywhere =  options.delete(:is_anywhere) if options.key?(:is_anywhere)
  is_anywhere = false if webdriver?
  oats_debug "wait_for_text #{value.inspect}"
  loc = wait_for_element(locators, options) do |l|
    is_anywhere ? text?(value) : text?(value, l)
  end
  if loc
    text?(value, loc)
  elsif skip_error == 'fail'
    if loc == element?(locators)
      actual = text(loc)
      Oats.error "Expected #{value}, but received #{actual}" unless actual =~ /#{value}/
    else
      Oats.error "Missing text #{value}"
    end
  end
end

#wait_until_loaded(options = {}) ⇒ Object



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/oats/oats_selenium_api.rb', line 505

def wait_until_loaded(options = {})
  #    puts '------------------'
  #    src = selenium.get_html_source
  #    puts src
  #    puts '------------------'
  options[:message] = "Did not finish loading"
  Oats.wait_until(options) do
    if selenium.element?("//div[@class = 'loading' and not (contains(@style, 'display: none'))]")
      oats_debug 'noticed loading...'
      true
    else
      oats_debug 'waiting to see loading...'
      false
    end
  end
  Oats.wait_until(options) do
    if selenium.element?("//div[@class = 'loading' and not (contains(@style, 'display: none'))]")
      oats_debug 'finished loading...'
      false
      oats_debug 'still loading...'
    else
      true
    end
  end
end

#webdriver?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/oats/oats_selenium_api.rb', line 100

def webdriver?
  true
end