-
Notifications
You must be signed in to change notification settings - Fork 5
/
element.go
94 lines (82 loc) · 1.57 KB
/
element.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
package webtester
import (
"reflect"
"runtime"
"strings"
"testing"
"github.com/bborbe/webdriver"
)
type Element struct {
testing.TB
elem webdriver.WebElement
}
func (e *Element) WebElement() (elem webdriver.WebElement) {
return e.elem
}
func (e *Element) Click() *Element {
e.Helper()
if err := e.elem.Click(); err != nil {
e.Fatalf("click failed: %+v", err)
}
return e
}
func (e *Element) Clear() *Element {
if err := e.elem.Clear(); err != nil {
e.Fatalf(`clear failed: %+v`, err)
}
return e
}
func (e *Element) Input(text string) *Element {
if err := e.elem.SendKeys(text); err != nil {
e.Fatalf(`input text "%s" failed: %+v`, text, err)
}
return e
}
func (e *Element) VerifyText(fn func(string, string) bool, expect string) *Element {
e.Helper()
actual, err := e.elem.Text()
if err != nil {
e.Fatal(err)
}
if !fn(actual, expect) {
name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
ss := strings.Split(name, ".")
if len(ss) == 2 {
name = ss[1]
}
e.Fatalf("want %s %s, got %s", strings.ToLower(name), expect, actual)
}
return e
}
func (e *Element) WaitForEnabled() *Element {
e.Helper()
var enabled bool
var err error
ok := wait(func() bool {
enabled, err = e.elem.IsEnabled()
if err != nil {
e.Fatal(err)
}
return enabled
})
if !ok {
e.Fatal(err)
}
return e
}
func (e *Element) WaitForDisabled() *Element {
e.Helper()
var enabled bool
var err error
ok := wait(func() bool {
enabled, err = e.elem.IsEnabled()
if err != nil {
e.Fatal(err)
}
return !enabled
})
if !ok {
e.Fatal(err)
}
return e
}