forked from sclevine/agouti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection_test.go
193 lines (164 loc) · 6.75 KB
/
selection_test.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package agouti_test
import (
"errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/sclevine/agouti"
"github.com/sclevine/agouti/api"
"github.com/sclevine/agouti/internal/element"
. "github.com/sclevine/agouti/internal/matchers"
"github.com/sclevine/agouti/internal/mocks"
)
var _ = Describe("Selection", func() {
var (
firstElement *mocks.Element
secondElement *api.Element
)
BeforeEach(func() {
firstElement = &mocks.Element{}
secondElement = &api.Element{}
})
Describe("#String", func() {
It("should return a string representation of the selection", func() {
selection := NewTestMultiSelection(nil, nil, "#selector")
Expect(selection.AllByXPath("#subselector").String()).To(Equal("selection 'CSS: #selector | XPath: #subselector'"))
})
})
Describe("#Elements", func() {
var (
selection *Selection
elementRepository *mocks.ElementRepository
)
BeforeEach(func() {
elementRepository = &mocks.ElementRepository{}
selection = NewTestSelection(nil, elementRepository, "#selector")
})
It("should return a []*api.Elements retrieved from the element repository", func() {
elements := []*api.Element{{ID: "first"}, {ID: "second"}}
elementRepository.GetCall.ReturnElements = []element.Element{elements[0], elements[1]}
Expect(selection.Elements()).To(Equal(elements))
})
Context("when retrieving the elements fails", func() {
It("should return an error", func() {
elementRepository.GetCall.Err = errors.New("some error")
_, err := selection.Elements()
Expect(err).To(MatchError("some error"))
})
})
})
Describe("#Count", func() {
var (
selection *MultiSelection
elementRepository *mocks.ElementRepository
)
BeforeEach(func() {
elementRepository = &mocks.ElementRepository{}
selection = NewTestMultiSelection(nil, elementRepository, "#selector")
elementRepository.GetCall.ReturnElements = []element.Element{firstElement, secondElement}
})
It("should successfully return the number of elements", func() {
Expect(selection.Count()).To(Equal(2))
})
Context("when the the session fails to retrieve the elements", func() {
It("should return an error", func() {
elementRepository.GetCall.Err = errors.New("some error")
_, err := selection.Count()
Expect(err).To(MatchError("failed to select elements from selection 'CSS: #selector': some error"))
})
})
})
Describe("#EqualsElement", func() {
var (
firstSelection *Selection
secondSelection *Selection
firstElementRepository *mocks.ElementRepository
secondElementRepository *mocks.ElementRepository
)
BeforeEach(func() {
firstElementRepository = &mocks.ElementRepository{}
firstElementRepository.GetExactlyOneCall.ReturnElement = firstElement
firstSelection = NewTestSelection(nil, firstElementRepository, "#first_selector")
secondElementRepository = &mocks.ElementRepository{}
secondElementRepository.GetExactlyOneCall.ReturnElement = secondElement
secondSelection = NewTestSelection(nil, secondElementRepository, "#second_selector")
})
It("should compare the selection elements for equality", func() {
firstSelection.EqualsElement(secondSelection)
Expect(firstElement.IsEqualToCall.Element).To(ExactlyEqual(secondElement))
})
It("should successfully return true if they are equal", func() {
firstElement.IsEqualToCall.ReturnEquals = true
Expect(firstSelection.EqualsElement(secondSelection)).To(BeTrue())
})
It("should successfully return false if they are not equal", func() {
firstElement.IsEqualToCall.ReturnEquals = false
Expect(firstSelection.EqualsElement(secondSelection)).To(BeFalse())
})
Context("when the provided object is a *MultiSelection", func() {
It("should not fail", func() {
multiSelection := NewTestMultiSelection(nil, secondElementRepository, "#multi_selector")
Expect(firstSelection.EqualsElement(multiSelection)).To(BeFalse())
Expect(firstElement.IsEqualToCall.Element).To(ExactlyEqual(secondElement))
})
})
Context("when the provided object is not a type of selection", func() {
It("should return an error", func() {
_, err := firstSelection.EqualsElement("not a selection")
Expect(err).To(MatchError("must be *Selection or *MultiSelection"))
})
})
Context("when there is an error retrieving elements from the selection", func() {
It("should return an error", func() {
firstElementRepository.GetExactlyOneCall.Err = errors.New("some error")
_, err := firstSelection.EqualsElement(secondSelection)
Expect(err).To(MatchError("failed to select element from selection 'CSS: #first_selector [single]': some error"))
})
})
Context("when there is an error retrieving elements from the other selection", func() {
It("should return an error", func() {
secondElementRepository.GetExactlyOneCall.Err = errors.New("some error")
_, err := firstSelection.EqualsElement(secondSelection)
Expect(err).To(MatchError("failed to select element from selection 'CSS: #second_selector [single]': some error"))
})
})
Context("when the session fails to compare the elements", func() {
It("should return an error", func() {
firstElement.IsEqualToCall.Err = errors.New("some error")
_, err := firstSelection.EqualsElement(secondSelection)
Expect(err).To(MatchError("failed to compare selection 'CSS: #first_selector [single]' to selection 'CSS: #second_selector [single]': some error"))
})
})
})
Describe("#MouseToElement", func() {
var (
selection *Selection
session *mocks.Session
elementRepository *mocks.ElementRepository
)
BeforeEach(func() {
elementRepository = &mocks.ElementRepository{}
elementRepository.GetExactlyOneCall.ReturnElement = secondElement
session = &mocks.Session{}
selection = NewTestSelection(session, elementRepository, "#selector")
})
It("should successfully instruct the session to move the mouse over the selection", func() {
Expect(selection.MouseToElement()).To(Succeed())
Expect(session.MoveToCall.Element).To(Equal(secondElement))
Expect(session.MoveToCall.Offset).To(BeNil())
})
Context("when the element repository fails to return exactly one element", func() {
It("should return an error", func() {
elementRepository.GetExactlyOneCall.Err = errors.New("some error")
err := selection.MouseToElement()
Expect(err).To(MatchError("failed to select element from selection 'CSS: #selector [single]': some error"))
})
})
Context("when the session fails to move the mouse to the element", func() {
It("should return an error", func() {
session.MoveToCall.Err = errors.New("some error")
err := selection.MouseToElement()
Expect(err).To(MatchError("failed to move mouse to element for selection 'CSS: #selector [single]': some error"))
})
})
})
})