-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwait.go
54 lines (43 loc) · 949 Bytes
/
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
package marionette_client
import (
"errors"
"time"
)
type Waiter struct {
f Finder
d time.Duration // Milliseconds
}
type Finder interface {
FindElement(by By, value string) (*WebElement, error)
FindElements(by By, value string) ([]*WebElement, error)
}
func Wait(f Finder) *Waiter {
return &Waiter{f: f, d: time.Duration(1)}
}
func (w *Waiter) For(d time.Duration) *Waiter {
if d < 0 || d > time.Minute*10 {
w.d = time.Duration(time.Second)
return w
}
w.d = d
return w
}
func (w *Waiter) Until(f func(c Finder) (bool, *WebElement, error)) (bool, *WebElement, error) {
firstRun := true
delta := time.Now()
for time.Since(delta) < w.d || firstRun {
firstRun = false
ok, value, err := f(w.f)
if err != nil {
_, de := err.(DriverError)
if de {
return false, nil, err
}
}
if ok {
return true, value, err
}
time.Sleep(time.Second)
}
return false, nil, errors.New("condition never occurred")
}