Class: Capybara::Element

Inherits:
Node
  • Object
show all
Defined in:
lib/capybara/node.rb

Overview

A Capybara::Element represents a single element on the page. It is possible to interact with the contents of this element the same as with a document:

session = Capybara::Session.new(:rack_test, my_app)

bar = session.find('#bar')              # from Capybara::Node::Finders
bar.select('Baz', :from => 'Quox')        # from Capybara::Node::Actions

Elements also have access to HTML attributes and other properties of the element:

bar.value
bar.text
bar[:title]

See Also:

Instance Attribute Summary

Attributes inherited from Node

#base, #session

Instance Method Summary collapse

Methods inherited from Node

#initialize

Methods included from Node::Matchers

#has_button?, #has_checked_field?, #has_content?, #has_css?, #has_field?, #has_link?, #has_no_button?, #has_no_content?, #has_no_css?, #has_no_field?, #has_no_link?, #has_no_select?, #has_no_table?, #has_no_xpath?, #has_select?, #has_table?, #has_unchecked_field?, #has_xpath?

Methods included from Node::Actions

#attach_file, #check, #choose, #click_button, #click_link, #click_link_or_button, #drag, #fill_in, #select, #uncheck, #unselect

Methods included from Node::Finders

#all, #find, #find_button, #find_by_id, #find_field, #find_link, #locate

Constructor Details

This class inherits a constructor from Capybara::Node

Instance Method Details

#[](attribute) ⇒ String

Retrieve the given attribute

element[:title] # => HTML title attribute

Parameters:

  • attribute (Symbol)

    The attribute to retrieve

Returns:

  • (String)

    The value of the attribute



92
93
94
# File 'lib/capybara/node.rb', line 92

def [](attribute)
  base[attribute]
end

#clickObject

Click the Element



138
139
140
# File 'lib/capybara/node.rb', line 138

def click
  base.click
end

#drag_to(node) ⇒ Object

Drag the element to the given other element.

source = page.find('#foo')
target = page.find('#bar')
source.drag_to(target)

Parameters:



192
193
194
# File 'lib/capybara/node.rb', line 192

def drag_to(node)
  base.drag_to(node.base)
end

#inspectObject



196
197
198
199
200
# File 'lib/capybara/node.rb', line 196

def inspect
  %(#<Capybara::Element tag="#{tag_name}" path="#{path}">)
rescue NotSupportedByDriverError
  %(#<Capybara::Element tag="#{tag_name}">)
end

#nativeObject

Returns The native element from the driver, this allows access to driver specific methods.

Returns:

  • (Object)

    The native element from the driver, this allows access to driver specific methods



71
72
73
# File 'lib/capybara/node.rb', line 71

def native
  base.native
end

#pathString

An XPath expression describing where on the page the element can be found

Returns:

  • (String)

    An XPath expression



167
168
169
# File 'lib/capybara/node.rb', line 167

def path
  base.path
end

#select_option(option) ⇒ Object

Select the given option if the element is a select box

Parameters:

  • option (String)

    The option to select



120
121
122
# File 'lib/capybara/node.rb', line 120

def select_option(option)
  base.select_option(option)
end

#set(value) ⇒ Object

Set the value of the form element to the given value.

Parameters:

  • value (String)

    The new value



110
111
112
# File 'lib/capybara/node.rb', line 110

def set(value)
  base.set(value)
end

#tag_nameString

Returns The tag name of the element.

Returns:

  • (String)

    The tag name of the element



146
147
148
# File 'lib/capybara/node.rb', line 146

def tag_name
  base.tag_name
end

#textString

Returns The text of the element.

Returns:

  • (String)

    The text of the element



79
80
81
# File 'lib/capybara/node.rb', line 79

def text
  base.text
end

#trigger(event) ⇒ Object

Trigger any event on the current element, for example mouseover or focus events. Does not work in Selenium.

Parameters:

  • event (String)

    The name of the event to trigger



178
179
180
# File 'lib/capybara/node.rb', line 178

def trigger(event)
  base.trigger(event)
end

#unselect_option(option) ⇒ Object

Unselect the given option if the element is a select box

Parameters:

  • option (String)

    The option to unselect



130
131
132
# File 'lib/capybara/node.rb', line 130

def unselect_option(option)
  base.unselect_option(option)
end

#valueString

Returns The value of the form element.

Returns:

  • (String)

    The value of the form element



100
101
102
# File 'lib/capybara/node.rb', line 100

def value
  base.value
end

#visible?Boolean

Whether or not the element is visible. Not all drivers support CSS, so the result may be inaccurate.

Returns:

  • (Boolean)

    Whether the element is visible



157
158
159
# File 'lib/capybara/node.rb', line 157

def visible?
  base.visible?
end