Module: Capybara::Rc::Accessors

Included in:
Adapter
Defined in:
lib/capybara/rc/accessors.rb

Overview

Contributes Selenium-RC reader methods to the adapter.

Expects a ‘session` accessor to be provided where it is mixed in.

Constant Summary collapse

WAIT_FOR_CONDITION_DELAY_SECONDS =
0.2

Instance Method Summary collapse

Instance Method Details

#get_attribute(attribute_locator) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/capybara/rc/accessors.rb', line 81

def get_attribute(attribute_locator)
  match = (attribute_locator.match(/\A(.*)@(\w+)\Z/))
  if match
    element_locator = match[1]
    attribute_name = match[2]
    capybara_find_by_locator(element_locator)[attribute_name]
  else
    fail "Unable to parse attribute_locator #{attribute_locator.inspect}"
  end
end

#get_html_sourceObject



60
61
62
# File 'lib/capybara/rc/accessors.rb', line 60

def get_html_source
  session.source
end

#get_locationObject



64
65
66
# File 'lib/capybara/rc/accessors.rb', line 64

def get_location
  session.current_url
end

#get_select_options(locator) ⇒ Object



92
93
94
95
# File 'lib/capybara/rc/accessors.rb', line 92

def get_select_options(locator)
  capybara_find_select_by_locator(locator).
    all('option').map { |opt_elt| opt_elt.text.strip }
end

#get_selected_label(locator) ⇒ Object



97
98
99
# File 'lib/capybara/rc/accessors.rb', line 97

def get_selected_label(locator)
  capybara_get_selected_option_elements_by_locator(locator).first.text
end

#get_selected_value(locator) ⇒ Object



101
102
103
# File 'lib/capybara/rc/accessors.rb', line 101

def get_selected_value(locator)
  capybara_get_selected_option_elements_by_locator(locator).first.value
end

#get_table(table_cell_address) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/capybara/rc/accessors.rb', line 109

def get_table(table_cell_address)
  address_parts = table_cell_address.split(".")
  locator = address_parts[0..-3].join(".")
  row = address_parts[-2].to_i
  column = address_parts[-1].to_i

  table = capybara_find_by_locator(locator)
  row = table.all("tr")[row]
  cell = (row.all('th').to_a + row.all('td').to_a)[column]

  if cell
    cell.text
  else
    fail Capybara::ElementNotFound
  end
end

#get_text(locator) ⇒ Object



68
69
70
# File 'lib/capybara/rc/accessors.rb', line 68

def get_text(locator)
  capybara_find_by_locator(locator).text
end

#get_titleObject



56
57
58
# File 'lib/capybara/rc/accessors.rb', line 56

def get_title
  session.title
end

#get_value(locator) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/capybara/rc/accessors.rb', line 72

def get_value(locator)
  c_element = capybara_find_by_locator(locator)
  if (capybara_element_is_radio_or_checkbox?(c_element))
    c_element.checked? ? 'on' : 'off'
  else
    c_element.value.strip
  end
end

#get_xpath_count(xpath) ⇒ Object



105
106
107
# File 'lib/capybara/rc/accessors.rb', line 105

def get_xpath_count(xpath)
  session.all(:xpath, xpath).size
end

#is_checked(locator) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/capybara/rc/accessors.rb', line 12

def is_checked(locator)
  element = capybara_find_by_locator(locator)
  if capybara_element_is_radio_or_checkbox?(element)
    element.checked?
  else
    fail "#{locator.inspect} is not a checkable element"
  end
end

#is_editable(locator) ⇒ Object



52
53
54
# File 'lib/capybara/rc/accessors.rb', line 52

def is_editable(locator)
  !capybara_find_by_locator(locator).disabled?
end

#is_element_present(locator) ⇒ Boolean

Note that Capybara does not allow access to invisible elements, so this is equivalent to ‘is_visible`.

Returns:

  • (Boolean)


26
27
28
# File 'lib/capybara/rc/accessors.rb', line 26

def is_element_present(locator)
  capybara_has_locator?(locator)
end

#is_text_present(pattern) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/capybara/rc/accessors.rb', line 41

def is_text_present(pattern)
  page_text = session.text
  parsed = parse_selenium_rc_string_pattern(pattern)
  case parsed[:type]
  when :glob
    page_text.include?(parsed[:string])
  else
    fail "Don't know how to search a string using a #{parsed[:type].inspect} Selenium pattern"
  end
end

#is_visible(locator) ⇒ Boolean

Returns true if the locator matches an element. Capybara does not make invisible elements available at all, so there is no distinction between an invisible element and one that is not present. (This method returns false for both.)

Returns:

  • (Boolean)


37
38
39
# File 'lib/capybara/rc/accessors.rb', line 37

def is_visible(locator)
  capybara_has_locator?(locator)
end

#wait_for_condition(script, timeout_milliseconds) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/capybara/rc/accessors.rb', line 126

def wait_for_condition(script, timeout_milliseconds)
  begin
    Timeout.timeout(timeout_milliseconds / 1000.0) do
      until session.evaluate_script(script)
        sleep(WAIT_FOR_CONDITION_DELAY_SECONDS)
      end
    end
  rescue Timeout::Error
    fail "Condition did not become true within #{timeout_milliseconds}ms"
  end
end