forked from atotto/webtester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.go
254 lines (225 loc) · 4.95 KB
/
browser.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package webtester
import (
"io/ioutil"
"net/url"
"strings"
"testing"
"time"
"github.com/bborbe/webdriver"
)
type Browser struct {
testing.TB
session *webdriver.Session
element webdriver.WebElement
}
func (b *Browser) Session() (session *webdriver.Session) {
return b.session
}
func (b *Browser) WebElement() (elem webdriver.WebElement) {
return b.element
}
func (b *Browser) Element() (elem *Element) {
return &Element{
TB: b.TB,
elem: b.element,
}
}
func (b *Browser) SetPageLoadTimeout(timeout time.Duration) {
b.Helper()
if err := b.session.SetTimeouts("page load", toMillisecond(timeout)); err != nil {
b.Fatal(err)
}
}
func toMillisecond(d time.Duration) int {
return int(d / time.Millisecond)
}
func (b *Browser) VisitTo(rawurl string) *Browser {
b.Helper()
if _, err := url.Parse(rawurl); err != nil {
b.Fatal(err)
}
if err := b.session.Url(rawurl); err != nil {
b.Fatal(err)
}
return b
}
func (b *Browser) WaitFor(target string) *Browser {
b.Helper()
using, value := splitTarget(b.TB, target)
var elem webdriver.WebElement
var err error
ok := wait(func() bool {
elem, err = b.session.FindElement(using, value)
return err == nil
})
if !ok {
b.Fatal(err)
}
b.element = elem
return b
}
func (b *Browser) WaitForText(target string, text string) *Browser {
b.Helper()
using, value := splitTarget(b.TB, target)
var elem webdriver.WebElement
var err error
ok := wait(func() bool {
elem, err = b.session.FindElement(using, value)
if err != nil {
return false
}
content, err := elem.Text()
if err != nil {
return false
}
if !strings.Contains(content, text) {
return false
}
return true
})
if !ok {
b.Fatal(err)
}
b.element = elem
return b
}
func splitTarget(tb testing.TB, target string) (using webdriver.FindElementStrategy, value string) {
tb.Helper()
tags := strings.SplitN(target, ":", 2)
if len(tags) != 2 {
tb.Fatal("expect target format `using:value`")
}
using, ok := toStrategy(tags[0])
if !ok {
tb.Fatalf("not supported: using=%s", using)
}
return using, tags[1]
}
func toStrategy(usingString string) (using webdriver.FindElementStrategy, ok bool) {
u := webdriver.FindElementStrategy(usingString)
switch u {
case webdriver.ClassName, webdriver.CSS_Selector, webdriver.ID, webdriver.Name, webdriver.LinkText, webdriver.PartialLinkText, webdriver.TagName, webdriver.XPath:
return u, true
case "class":
return webdriver.ClassName, true
case "css":
return webdriver.CSS_Selector, true
case "tag":
return webdriver.TagName, true
default:
return "", false
}
}
func (b *Browser) Expect(target string, text string) {
b.Helper()
b.Log("Deprecated")
using, value := splitTarget(b.TB, target)
var elems []webdriver.WebElement
var err error
ok := wait(func() bool {
elems, err = b.session.FindElements(using, value)
if err != nil {
return false
}
for _, elem := range elems {
actual, err := elem.Text()
if err != nil {
return false
}
if strings.Contains(actual, text) {
return true
}
}
return false
})
if !ok {
b.Log(err)
b.Fatalf("not found: %s", text)
}
}
func (b *Browser) MustFindElement(target string) *Element {
b.Helper()
using, value := splitTarget(b.TB, target)
elem, err := b.session.FindElement(using, value)
if err != nil {
b.Fatalf(`element "%s" notfound: %+v`, target, err)
}
b.element = elem
return b.Element()
}
func (b *Browser) MustFindElements(target string) []*Element {
b.Helper()
using, value := splitTarget(b.TB, target)
elems, err := b.session.FindElements(using, value)
if err != nil {
b.Fatalf(`elements "%s" notfound: %+v`, target, err)
}
if len(elems) != 0 {
b.element = elems[0]
}
es := make([]*Element, len(elems))
for i, elem := range elems {
es[i] = &Element{
TB: b.TB,
elem: elem,
}
}
return es
}
func (b *Browser) TakeScreenshot(name string) *Browser {
b.Helper()
buf, err := b.session.Screenshot()
if err != nil {
b.Fatal(err)
}
err = ioutil.WriteFile(name, buf, 0644)
if err != nil {
b.Fatal(err)
}
return b
}
func (b *Browser) TakeSource(name string) *Browser {
b.Helper()
str, err := b.session.Source()
if err != nil {
b.Fatal(err)
}
err = ioutil.WriteFile(name, []byte(str), 0644)
if err != nil {
b.Fatal(err)
}
return b
}
func (b *Browser) ExpectTransitTo(rawurl string) *Browser {
b.Helper()
expect, err := url.Parse(rawurl)
if err != nil {
b.Fatal(err)
}
ok := wait(func() bool {
ru, err := b.session.GetUrl()
if err != nil {
b.Log(err)
}
u, err := url.Parse(ru)
if err != nil {
b.Log(err)
}
return u.Path == expect.Path
})
if !ok {
b.Log(err)
b.Fatalf("not found: %s", rawurl)
}
return b
}
func (b *Browser) SetWindowSize(width, height int) {
b.session.GetCurrentWindowHandle().SetSize(webdriver.Size{Width: width, Height: height})
}
func (b *Browser) GetWindowSize() (width, height int, err error) {
size, err := b.session.GetCurrentWindowHandle().GetSize()
if err != nil {
return
}
return size.Width, size.Height, nil
}