Class: Capybara::UI::List
- Includes:
- Enumerable
- Defined in:
- lib/capybara/ui/widgets/list.rb
Overview
Use a List when you want to treat repeating elements as a unit.
Usage
Consider the following HTML:
<ul id="colors">
<li>Red <span class="pt">Vermelho</span></li>
<li>Green <span class="pt">Verde</span></li>
<li>Blue <span class="pt">Azul</span></li>
</ul>
You can then define the following widget:
class Colors < Capybara::UI::List
root '#colors'
item 'li'
end
Now you’ll be able to iterate over each item:
# prints:
# Red Vermelho
# Green Verde
# Blue Azul
(:colors).each do |e|
puts e
end
This is the same as doing the following in Capybara:
all('#colors li').each do |e|
puts e.text.strip
end
Note that, by default, the root selector of a List is ul
and the list item selector is li
. So you could wrap the <ul> above simply by using the following:
class Colors < Capybara::UI::List
end
Narrowing items
You can define the root selector for your list items using the ::item macro:
class PortugueseColors < Capybara::UI::List
root '#colors
item '.pt'
end
If you iterate over this list you get the following:
# prints:
# Vermelho
# Verde
# Azul
(:portuguese_colors).each do |e|
puts e
end
You can make a list out of any repeating elements, as long as you can define parent and child selectors.
<div id="not-a-list-colors">
<div class="child">Red</div>
<div class="child">Green</div>
<div class="child">Blue</div>
</div>
You can define the following widget:
class NotAListColors < Capybara::UI::List
root '#not-a-list-colors'
item '.child'
end
Direct Known Subclasses
Constant Summary
Constants included from Capybara::UI
Class Attribute Summary collapse
Attributes inherited from Widget
Class Method Summary collapse
-
.item(selector, type = ListItem, &block) ⇒ Object
Configures the List item selector and class.
Instance Method Summary collapse
- #count ⇒ Object
-
#empty? ⇒ Boolean
TODO: Convert value to primitive data structures.
- #exclude?(element) ⇒ Boolean
- #include?(element) ⇒ Boolean
- #length ⇒ Object
- #size ⇒ Object
- #to_row ⇒ Object
- #to_table ⇒ Object
- #value ⇒ Object
Methods inherited from Widget
action, #class?, #classes, #click, #double_click, filter, filter?, find_all_in, find_in, #has_action?, #hover, #html, #id, #initialize, not_present_in?, present_in?, #right_click, root, selector, #text, #to_cell, #to_s, widget_delegator
Methods included from Widgets::DSL
Methods included from WidgetParts::Container
#has_widget?, #not_visible?, #visible?, #widget, #widgets
Methods included from Capybara::UI
Methods included from Constructors
Methods included from WidgetParts::Struct
Constructor Details
This class inherits a constructor from Capybara::UI::Widget
Class Attribute Details
.item_factory ⇒ Object
143 144 145 |
# File 'lib/capybara/ui/widgets/list.rb', line 143 def item_factory @item_factory ||= WidgetClass.new('li', ListItem) end |
Class Method Details
.item(selector, type = ListItem, &block) ⇒ Object
Configures the List item selector and class.
Usage
Given the following HTML:
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
In its most basic form, allows you to configure the list item selector, using the default list item class (Capybara::UI::ListItem):
class Numbers < Capybara::UI::List
root 'ul'
item 'li'
end
Extending the list item class
You can define the list item class for the current List:
class Number < Capybara::UI::Widget
# ...
end
class Numbers < Capybara::UI::List
root 'ul'
item 'li', Number
end
(:numbers).first.class < Number #=> true
Alternatively, you can extend the list item type inline. This is useful when you want to add small extensions to the default list item class. The extensions will apply only to list items of the current List.
class Numbers < Capybara::UI::List
root 'ul'
item 'li' do
def upcase
text.upcase
end
end
(:numbers).first.upcase #=> "ONE"
end
137 138 139 |
# File 'lib/capybara/ui/widgets/list.rb', line 137 def item(selector, type = ListItem, &block) self.item_factory = WidgetClass.new(selector, type, &block) end |
Instance Method Details
#count ⇒ Object
148 149 150 |
# File 'lib/capybara/ui/widgets/list.rb', line 148 def count items.count end |
#empty? ⇒ Boolean
TODO: Convert value to primitive data structures.
153 154 155 |
# File 'lib/capybara/ui/widgets/list.rb', line 153 def empty? items.empty? end |
#exclude?(element) ⇒ Boolean
157 158 159 |
# File 'lib/capybara/ui/widgets/list.rb', line 157 def exclude?(element) ! include?(element) end |
#include?(element) ⇒ Boolean
161 162 163 |
# File 'lib/capybara/ui/widgets/list.rb', line 161 def include?(element) value.include?(element) end |
#length ⇒ Object
165 166 167 |
# File 'lib/capybara/ui/widgets/list.rb', line 165 def length items.length end |
#size ⇒ Object
169 170 171 |
# File 'lib/capybara/ui/widgets/list.rb', line 169 def size items.size end |
#to_row ⇒ Object
173 174 175 |
# File 'lib/capybara/ui/widgets/list.rb', line 173 def to_row items.map(&:to_cell) end |
#to_table ⇒ Object
177 178 179 |
# File 'lib/capybara/ui/widgets/list.rb', line 177 def to_table items.map(&:to_row) end |
#value ⇒ Object
181 182 183 |
# File 'lib/capybara/ui/widgets/list.rb', line 181 def value items.map(&:value) end |