Class: Capybara::Node::Element

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

Overview

A 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

Element also has access to HTML attributes and other properties of the element:

bar.value
bar.text
bar[:title]

See Also:

Instance Attribute Summary

Attributes inherited from Base

#base, #parent, #session

Instance Method Summary (collapse)

Methods inherited from Base

#synchronize

Methods included from Matchers

#has_button?, #has_checked_field?, #has_css?, #has_field?, #has_link?, #has_no_button?, #has_no_checked_field?, #has_no_css?, #has_no_field?, #has_no_link?, #has_no_select?, #has_no_selector?, #has_no_table?, #has_no_text?, #has_no_unchecked_field?, #has_no_xpath?, #has_select?, #has_selector?, #has_table?, #has_text?, #has_unchecked_field?, #has_xpath?

Methods included from Actions

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

Methods included from Finders

#find_button, #find_by_id, #find_field, #find_link, #query, #resolve

Constructor Details

- (Element) initialize(session, base, parent, selector)

A new instance of Element



25
26
27
28
29
# File 'lib/capybara/node/element.rb', line 25

def initialize(session, base, parent, selector)
  super(session, base)
  @parent = parent
  @selector = selector
end

Instance Method Details

- (String) [](attribute)

Retrieve the given attribute

element[:title] # => HTML title attribute

Parameters:

  • attribute (Symbol)

    The attribute to retrieve

Returns:

  • (String)

    The value of the attribute



56
57
58
# File 'lib/capybara/node/element.rb', line 56

def [](attribute)
  synchronize { base[attribute] }
end

- (Object) all(*args)



184
185
186
# File 'lib/capybara/node/element.rb', line 184

def all(*args)
  synchronize { super }
end

- (Boolean) checked?

Whether or not the element is checked.

Returns:

  • (Boolean)

    Whether the element is checked



127
128
129
# File 'lib/capybara/node/element.rb', line 127

def checked?
  synchronize { base.checked? }
end

- (Object) click

Click the Element



98
99
100
# File 'lib/capybara/node/element.rb', line 98

def click
  synchronize { base.click }
end

- (Object) drag_to(node)

Drag the element to the given other element.

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

Parameters:

  • node (Capybara::Element)

    The element to drag to



172
173
174
# File 'lib/capybara/node/element.rb', line 172

def drag_to(node)
  synchronize { base.drag_to(node.base) }
end

- (Object) find(*args)



176
177
178
# File 'lib/capybara/node/element.rb', line 176

def find(*args)
  synchronize { super }
end

- (Object) first(*args)



180
181
182
# File 'lib/capybara/node/element.rb', line 180

def first(*args)
  synchronize { super }
end

- (Object) inspect



194
195
196
197
198
# File 'lib/capybara/node/element.rb', line 194

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

- (Object) native

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



35
36
37
# File 'lib/capybara/node/element.rb', line 35

def native
  synchronize { base.native }
end

- (String) path

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

Returns:

  • (String)

    An XPath expression



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

def path
  synchronize { base.path }
end

- (Object) reload



188
189
190
191
192
# File 'lib/capybara/node/element.rb', line 188

def reload
  reloaded = parent.reload.first(@selector.name, @selector.locator, @selector.options)
  @base = reloaded.base if reloaded
  self
end

- (Object) select_option

Select this node if is an option element inside a select tag



82
83
84
# File 'lib/capybara/node/element.rb', line 82

def select_option
  synchronize { base.select_option }
end

- (Boolean) selected?

Whether or not the element is selected.

Returns:

  • (Boolean)

    Whether the element is selected



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

def selected?
  synchronize { base.selected? }
end

- (Object) set(value)

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

Parameters:

  • value (String)

    The new value



74
75
76
# File 'lib/capybara/node/element.rb', line 74

def set(value)
  synchronize { base.set(value) }
end

- (String) tag_name

The tag name of the element

Returns:

  • (String)

    The tag name of the element



106
107
108
# File 'lib/capybara/node/element.rb', line 106

def tag_name
  synchronize { base.tag_name }
end

- (String) text

The text of the element

Returns:

  • (String)

    The text of the element



43
44
45
# File 'lib/capybara/node/element.rb', line 43

def text
  synchronize { base.text }
end

- (Object) trigger(event)

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



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

def trigger(event)
  synchronize { base.trigger(event) }
end

- (Object) unselect_option

Unselect this node if is an option element inside a multiple select tag



90
91
92
# File 'lib/capybara/node/element.rb', line 90

def unselect_option
  synchronize { base.unselect_option }
end

- (String) value

The value of the form element

Returns:

  • (String)

    The value of the form element



64
65
66
# File 'lib/capybara/node/element.rb', line 64

def value
  synchronize { base.value }
end

- (Boolean) visible?

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



117
118
119
# File 'lib/capybara/node/element.rb', line 117

def visible?
  synchronize { base.visible? }
end