Class: Bauxite::Selector

Inherits:
Object
  • Object
show all
Includes:
SelectorModule
Defined in:
lib/bauxite/core/selector.rb,
lib/bauxite/selectors/sid.rb,
lib/bauxite/selectors/attr.rb,
lib/bauxite/selectors/json.rb,
lib/bauxite/selectors/frame.rb,
lib/bauxite/selectors/smart.rb,
lib/bauxite/selectors/window.rb

Overview

– Copyright © 2014 Patricio Zavolinsky

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Instance Method Summary collapse

Methods included from SelectorModule

#find, #initialize

Instance Method Details

#attr(arg, &block) ⇒ Object

:category: Selector Methods



35
36
37
38
# File 'lib/bauxite/selectors/attr.rb', line 35

def attr(arg, &block)
  data = arg.split(':', 2)
  selenium_find(:css, "[#{data[0]}='#{data[1]}']", &block)
end

#frame(arg, &block) ⇒ Object

:category: Selector Methods



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bauxite/selectors/frame.rb', line 48

def frame(arg, &block)
  delimiter = arg[0]
  items = arg[1..-1].split(delimiter, 2)
  find(items[0]) do |f|
    begin
      @ctx.driver.switch_to.frame f
      find(items[1], &block)
    ensure
      @ctx.driver.switch_to.default_content
    end
  end
end

#json(arg) {|element| ... } ⇒ Object

:category: Selector Methods

Yields:

  • (element)

Raises:

  • (Selenium::WebDriver::Error::NoSuchElementError)


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bauxite/selectors/json.rb', line 49

def json(arg, &block)
  source = JSON::parse(@ctx.driver.page_source.sub(/^<html[^>]*>.*<pre>/, '').sub(/<\/pre>.*<\/html>$/, ''))
  element = _json_find(source, arg)
  raise Selenium::WebDriver::Error::NoSuchElementError, "Cannot find JSON element: #{arg}" unless element

  element = element.to_s
  def element.text; self; end
  def element.tag_name; 'json'; end
  yield element if block_given?
  element
end

#sid(arg, &block) ⇒ Object

:category: Selector Methods



35
36
37
# File 'lib/bauxite/selectors/sid.rb', line 35

def sid(arg, &block)
  selenium_find(:css, "[id$='#{arg.gsub("'", "\\'")}']", &block)
end

#smart(arg, &block) ⇒ Object

:category: Selector Methods



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bauxite/selectors/smart.rb', line 46

def smart(arg, &block)
  b = lambda { |e| e }
  target   = _smart_try_find { sid(arg, &b)                    }
  target ||= _smart_try_find { selenium_find(:name, arg)       }
  target ||= _smart_try_find { selenium_find(:class_name, arg) }
  target ||= _smart_try_find { attr("id*:"+arg, &b)            }
  target ||= _smart_try_find { attr("placeholder:"+arg, &b)    }
  quoted_arg = "'"+arg.gsub("'", "\\'")+"'"
  target ||= _smart_try_find do
    selenium_find(:css, ["input[type='checkbox'][value=#{quoted_arg}]",
      "input[type='radio'][value=#{quoted_arg}]",
      "input[type='button'][value=#{quoted_arg}]",
      "input[type='submit'][value=#{quoted_arg}]"
      ].join(',')
    )
  end
  return yield target if target
  
  target = selenium_find(:xpath, "//*[contains(text(), '#{arg.gsub("'", "\\'")}')]")
  return yield target unless target.tag_name.downcase == 'label'
  label = target
  id = label['for']
  return yield selenium_find(:id, id) if id
  
  target = _smart_try_find { label.find_element(:css, "input, select, textarea, button") }
  return yield target if target
end

#window(arg, &block) ⇒ Object

:category: Selector Methods



47
48
49
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
# File 'lib/bauxite/selectors/window.rb', line 47

def window(arg, &block)
  current = @ctx.driver.window_handle
  
  delimiter = arg[0]
  name,child = arg[1..-1].split(delimiter, 2)
  
  pattern = /#{name}/
  if name =~ /^\/.*\/[imxo]*$/
    pattern = eval(name)
  end

  match = @ctx.driver.window_handles.find do |h|
    @ctx.driver.switch_to.window h
    @ctx.driver.current_url =~ pattern
  end
  
  name = match if match
  begin
    @ctx.driver.switch_to.window name
  rescue StandardError => e
    @ctx.driver.switch_to.window current
    raise Bauxite::Errors::AssertionError, "Cannot find a window matching '#{name}' (either by name exact match or by url regex)." + e.message 
  end
  
  begin
    find(child, &block)
  ensure
    @ctx.driver.switch_to.window current
  end
end