Module: RWebSpec::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



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

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



5
6
7
# File 'lib/rwebspec-webdriver/element_locator.rb', line 5

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

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



10
11
12
# File 'lib/rwebspec-webdriver/element_locator.rb', line 10

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

#equal_pair(key, value) ⇒ Object



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

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



34
35
36
# File 'lib/rwebspec-webdriver/element_locator.rb', line 34

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

#find_by_tag(tag) ⇒ Object




44
45
46
# File 'lib/rwebspec-webdriver/element_locator.rb', line 44

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

#hidden_elementsObject



39
40
41
# File 'lib/rwebspec-webdriver/element_locator.rb', line 39

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

#lhs_for(key) ⇒ Object



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

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



15
16
17
# File 'lib/rwebspec-webdriver/element_locator.rb', line 15

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

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



19
20
21
# File 'lib/rwebspec-webdriver/element_locator.rb', line 19

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

#should_use_label_element?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/rwebspec-webdriver/element_locator.rb', line 48

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

#text_area_elementsObject



29
30
31
# File 'lib/rwebspec-webdriver/element_locator.rb', line 29

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

#text_field_elementsObject



25
26
27
# File 'lib/rwebspec-webdriver/element_locator.rb', line 25

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