Module: RWebSpec::WebDriver::ElementLocator

Included in:
WebBrowser
Defined in:
lib/rwebspec-webdriver/element_locator.rb

Constant Summary collapse

BUTTON_VALID_TYPES =
%w[button reset submit image]
CHECK_BOX_TYPES =
%w(checkbox)
RADIO_TYPES =
%w(radio)
TEXT_FILED_TYPES =

TextField, TextArea

%w(text)
FILE_FIELD_TYPES =
%w(file)
HIDDEN_TYPES =
%w(hidden)

Instance Method Summary collapse

Instance Method Details

#attribute_expression(selectors) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/rwebspec-webdriver/element_locator.rb', line 76

def attribute_expression(selectors)
  selectors.map do |key, val|
    if val.kind_of?(Array)
      "(" + val.map { |v| equal_pair(key, v) }.join(" or ") + ")"
    else
      equal_pair(key, val)
    end
  end.join(" and ")
end

#button_elementsObject



7
8
9
# File 'lib/rwebspec-webdriver/element_locator.rb', line 7

def button_elements
  find_elements(:xpath, ".//button | .//input[#{attribute_expression :type => BUTTON_VALID_TYPES}]")
end

#check_box_elements(how, what, opts = []) ⇒ Object



12
13
14
# File 'lib/rwebspec-webdriver/element_locator.rb', line 12

def check_box_elements(how, what, opts = [])
  find_elements(:xpath, ".//input[#{attribute_expression :type => CHECK_BOX_TYPES}]")
end

#equal_pair(key, value) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/rwebspec-webdriver/element_locator.rb', line 54

def equal_pair(key, value)
  # we assume :label means a corresponding label element, not the attribute
  if key == :label && should_use_label_element?
    "@id=//label[normalize-space()='#{value}']/@for"
  else
    "#{lhs_for(key)}='#{value}'"
  end
end

#file_field_elementsObject



36
37
38
# File 'lib/rwebspec-webdriver/element_locator.rb', line 36

def file_field_elements
  find_elements(:xpath, ".//input[#{attribute_expression :type => FILE_FIELD_TYPES}]")
end

#find_by_tag(tag) ⇒ Object




46
47
48
# File 'lib/rwebspec-webdriver/element_locator.rb', line 46

def find_by_tag(tag)
  find_elements(:tag_name, tag)
end

#hidden_elementsObject



41
42
43
# File 'lib/rwebspec-webdriver/element_locator.rb', line 41

def hidden_elements
  find_elements(:xpath, ".//input[#{attribute_expression :type => HIDDEN_TYPES}]")
end

#lhs_for(key) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rwebspec-webdriver/element_locator.rb', line 63

def lhs_for(key)
  case key
  when :text, 'text'
    'normalize-space()'
  when :href
    # TODO: change this behaviour?
    'normalize-space(@href)'
  else
    "@#{key.to_s.gsub("_", "-")}"
  end
end

#radio_elements(how, what, opts = []) ⇒ Object



17
18
19
# File 'lib/rwebspec-webdriver/element_locator.rb', line 17

def radio_elements(how, what, opts = [])
  find_elements(:xpath, ".//input[#{attribute_expression :type => RADIO_TYPES}]")
end

#select_elements(how, what, opts = []) ⇒ Object



21
22
23
# File 'lib/rwebspec-webdriver/element_locator.rb', line 21

def select_elements(how, what, opts = [])
  find_elements(:xpath, ".//input[#{attribute_expression :type => RADIO_TYPES}]")
end

#should_use_label_element?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/rwebspec-webdriver/element_locator.rb', line 50

def should_use_label_element?
  @selector[:tag_name] != "option" rescue false
end

#text_area_elementsObject



31
32
33
# File 'lib/rwebspec-webdriver/element_locator.rb', line 31

def text_area_elements
  find_elements(:xpath, ".//textarea")
end

#text_field_elementsObject



27
28
29
# File 'lib/rwebspec-webdriver/element_locator.rb', line 27

def text_field_elements
  find_elements(:xpath, ".//input[#{attribute_expression :type => TEXT_FILED_TYPES}]")
end