/
element_wait.go
183 lines (147 loc) · 3.67 KB
/
element_wait.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
package selenium
import (
"time"
"github.com/aleksslitvinovs/go-selenium/logger"
"github.com/pkg/errors"
)
// Waiter is a helper struct to wait for an element to be present, visible, etc.
type Waiter struct {
e *Element
timeout time.Duration
}
// WaitFor creates an instance of *Waiter with the provided timeout duration.
func (e *Element) WaitFor(timeout time.Duration) *Waiter {
return &Waiter{
e: e,
timeout: timeout,
}
}
// UntilIsPresent waits until the element is present.
func (w *Waiter) UntilIsPresent() *Element {
return waitPresent(w, true)
}
// UntilIsNotPresent waits until the element is not present.
func (w *Waiter) UntilIsNotPresent() *Element {
return waitPresent(w, false)
}
// UntilIsVisible waits until the element is visible.
func (w *Waiter) UntilIsVisible() *Element {
return waitCondition(w, w.e.isVisible, true, "visible")
}
// UntilIsNotVisible waits until the element is visible.
func (w *Waiter) UntilIsNotVisible() *Element {
return waitCondition(w, w.e.isVisible, false, "not visible")
}
// UntilIsEnabled waits until the element is enabled.
func (w *Waiter) UntilIsEnabled() *Element {
return waitCondition(w, w.e.isEnabled, true, "enabled")
}
// UntilIsNotEnabled waits until the element is not enabled.
func (w *Waiter) UntilIsNotEnabled() *Element {
return waitCondition(w, w.e.isEnabled, false, "not enabled")
}
// UntilIsSelected waits until the element is selected.
func (w *Waiter) UntilIsSelected() *Element {
return waitCondition(w, w.e.isSelected, true, "selected")
}
// UntilIsNotSelected waits until the element is not selected.
func (w *Waiter) UntilIsNotSelected() *Element {
return waitCondition(w, w.e.isSelected, false, "not selected")
}
func waitCondition(
w *Waiter,
condition func() (*response, error),
expected bool,
conditionName string,
) *Element {
startTime := time.Now()
endTime := startTime.Add(w.timeout)
intialSettings := *w.e.settings
w.e.settings.IgnoreNotFound = true
defer func() {
w.e.settings = &intialSettings
}()
for {
if endTime.Before(time.Now()) {
handleError(
nil,
errors.Errorf(
"Element %q is not %s after %s (time elapsed %dms)",
w.e.Selector,
conditionName,
w.timeout,
time.Since(startTime).Milliseconds(),
),
)
return w.e
}
res, err := condition()
if err != nil {
handleError(res, err)
return w.e
}
if res.Value != nil {
if v, ok := res.Value.(bool); ok {
if v == expected {
logger.Infof(
"Element %q is %s after %s (time elapsed %dms)",
w.e.Selector,
conditionName,
w.timeout,
time.Since(startTime).Milliseconds(),
)
return w.e
}
}
}
time.Sleep(w.e.settings.PollInterval.Duration)
}
}
func waitPresent(
w *Waiter,
bePresent bool,
) *Element {
startTime := time.Now()
endTime := startTime.Add(w.timeout)
intialSettings := *w.e.settings
w.e.settings.IgnoreNotFound = true
defer func() {
w.e.settings = &intialSettings
}()
conditionName := "present"
if !bePresent {
conditionName = "not present"
}
for {
if endTime.Before(time.Now()) {
handleError(
nil,
errors.Errorf(
"Element %q is not %s after %s (time elapsed %s)",
w.e.Selector,
conditionName,
w.timeout,
time.Since(startTime),
),
)
return w.e
}
id, err := w.e.findElement()
if err != nil {
handleError(nil, err)
return w.e
}
if id != "" && bePresent || id == "" && !bePresent {
logger.Infof(
"Element %q is %s after %s (time elapsed %dms)",
w.e.Selector,
conditionName,
w.timeout,
time.Since(startTime).Milliseconds(),
)
w.e.id = id
return w.e
}
time.Sleep(w.e.settings.PollInterval.Duration)
}
}