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 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

Constructor Details

#initialize(session, base, parent, selector) ⇒ Element

Returns 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

#[](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



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

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

#all(*args) ⇒ Object



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

def all(*args)
  wait_until { super }
end

#checked?Boolean

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?
  wait_until { base.checked? }
end

#clickObject

Click the Element



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

def click
  wait_until { 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:

  • node (Capybara::Element)

    The element to drag to



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

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

#find(*args) ⇒ Object



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

def find(*args)
  wait_until { super }
end

#first(*args) ⇒ Object



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

def first(*args)
  wait_until { super }
end

#inspectObject



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

#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



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

def native
  wait_until { base.native }
end

#pathString

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
  wait_until { base.path }
end

#reloadObject



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

#select_optionObject

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
  wait_until { base.select_option }
end

#selected?Boolean

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?
  wait_until { base.selected? }
end

#set(value) ⇒ Object

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)
  wait_until { base.set(value) }
end

#tag_nameString

Returns 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
  wait_until { base.tag_name }
end

#textString

Returns 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
  wait_until { 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



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

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

#unselect_optionObject

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
  wait_until { base.unselect_option }
end

#valueString

Returns 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
  wait_until { 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



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

def visible?
  wait_until { base.visible? }
end