-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4.3_component_collection_spec.rb
63 lines (50 loc) · 2.21 KB
/
4.3_component_collection_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true
# Page under test: todo_lists.html
# ToDoListItem class: same as in previous example, but without network delays
class ToDoListItem4_3 < WatirPump::Component
span_reader :label, role: 'name'
link_clicker :remove, role: 'rm'
end
# ToDoList class: same as in previous example, but without network delays
class ToDoList4_3 < WatirPump::Component
div_reader :title, role: 'title'
text_field_writer :item_name, role: 'new_item'
button_clicker :submit, role: 'add'
components :items, ToDoListItem4_3, :lis
query :values, -> { items.map(&:label) }
def [](label)
items.find { |i| i.label == label }
end
end
# A decorator class for the collection of components declared in the Page class below.
# Adds [] "operator" to allow access to an individual ToDoList by its title
class CollectionIndexedByTitle4_3 < WatirPump::ComponentCollection
def [](title)
find { |l| l.title == title }
end
end
class ToDoListPage4_3 < WatirPump::Page
uri '/todo_lists.html'
# Collection of components, this time located by a lambda
components :todo_lists, ToDoList4_3, -> { root.divs(role: 'todo_list') }
# Use `decorate` to wrap given method (here: `todo_lists`) with a decorator class
# that will provide some additional features to the object returned initially.
# Here the decorator class `CollectionIndexedByTitle4_3` modifies behavior of [] operator.
decorate :todo_lists, CollectionIndexedByTitle4_3
end
RSpec.describe ToDoListPage4_3 do
it 'fills the form' do
ToDoListPage4_3.open do
# There are three ToDoLists on the page.
# The collection can be accessed through `todo_lists` method.
# Thanks to decoration they can be accessed by the list title, instead of integer array index.
todo_lists['Groceries'].fill_form!(item_name: 'Pineapple')
expect(todo_lists['Groceries'].values).to include 'Pineapple'
todo_lists['Work'].fill_form!(item_name: 'Implement class ElementDecorator')
expect(todo_lists['Work'].values).to include 'Implement class ElementDecorator'
todo_lists['Groceries']['Bread'].remove
expect(todo_lists['Groceries'].values).not_to include 'Bread'
# @see example 4.4 for even more elegant API
end
end
end