forked from bunsenapp/go-selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_driver_helpers.go
55 lines (47 loc) · 1.09 KB
/
remote_driver_helpers.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
package goselenium
import "time"
// Until represents a function that will be continuously repeated until it
// succeeds or a timeout is reached.
type Until func(w WebDriver) bool
// UntilElementPresent attempts to locate an element on the page. It is
// determined as existing if the state is 'Success' and the error is nil.
func UntilElementPresent(by By) Until {
return func(w WebDriver) bool {
_, err := w.FindElement(by)
return err == nil
}
}
// UntilURLIs checks whether or not the page's URL has changed.
func UntilURLIs(url string) Until {
return func(w WebDriver) bool {
resp, err := w.CurrentURL()
return err == nil && resp.URL == url
}
}
func (s *seleniumWebDriver) Wait(u Until, timeout time.Duration, sleep time.Duration) bool {
response := make(chan bool, 1)
quit := make(chan bool, 1)
go func() {
outer:
for {
select {
case <-quit:
break outer
default:
e := u(s)
if e {
response <- true
break outer
}
}
time.Sleep(sleep)
}
}()
select {
case r := <-response:
return r
case <-time.After(timeout):
close(quit)
return false
}
}