-
Notifications
You must be signed in to change notification settings - Fork 4
/
navigation.go
180 lines (161 loc) · 4.48 KB
/
navigation.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
package browsersteps
import (
"errors"
"fmt"
"github.com/DATA-DOG/godog"
)
const (
scrollEndScript = `window.scrollTo(0, document.body.scrollHeight);`
scrollTopScript = `window.scrollTo(0, 0);`
)
func (b *BrowserSteps) iNavigateTo(browseURL string) error {
u, err := b.GetURL(browseURL)
if err != nil {
return err
}
return b.GetWebDriver().Get(u.String())
}
func (b *BrowserSteps) iSwitchToNewWindow() error {
wh, err := b.GetWebDriver().WindowHandles()
if err != nil {
return err
}
return b.GetWebDriver().SwitchWindow(wh[len(wh)-1])
}
func (b *BrowserSteps) iSwitchToMainWindow() error {
wh, err := b.GetWebDriver().WindowHandles()
if err != nil {
return err
}
return b.GetWebDriver().SwitchWindow(wh[0])
}
func (b *BrowserSteps) iCloseNewWindow() error {
wh, err := b.GetWebDriver().WindowHandles()
if err != nil {
return err
}
return b.GetWebDriver().CloseWindow(wh[len(wh)-1])
}
func (b *BrowserSteps) iSwitchToPreviousWindow() error {
current, err := b.GetWebDriver().CurrentWindowHandle()
if err != nil {
return err
}
wh, err := b.GetWebDriver().WindowHandles()
if err != nil {
return err
}
if len(wh) < 2 {
return errors.New("No previous window")
}
for i := 1; i < len(wh); i++ {
if current == wh[i] {
return b.GetWebDriver().CloseWindow(wh[i-1])
}
}
return errors.New("Abnormal exception: No window found")
}
func (b *BrowserSteps) iSwitchToWindowHaving(what, match string) error {
if what == "url" {
u, err := b.GetURL(match)
if err != nil {
return err
}
match = u.String()
}
current, err := b.GetWebDriver().CurrentWindowHandle()
if err != nil {
return err
}
wh, err := b.GetWebDriver().WindowHandles()
if err != nil {
return err
}
for i := 0; i < len(wh); i++ {
err := b.GetWebDriver().SwitchWindow(wh[i])
if err != nil {
return err
}
switch what {
case "url":
URL, err := b.GetWebDriver().CurrentURL()
if err != nil {
return err
}
if URL == match {
return nil
}
case "title":
title, err := b.GetWebDriver().Title()
if err != nil {
return err
}
if title == match {
return nil
}
default:
return fmt.Errorf("Invalid property. Found '%s', Expected 'url' or 'title'", what)
}
}
err = b.GetWebDriver().SwitchWindow(current)
if err != nil {
return err
}
return errors.New("Abnormal exception: No window found")
}
func (b *BrowserSteps) iResizeBrowserWindowTo(w, h int) error {
current, err := b.GetWebDriver().CurrentWindowHandle()
if err != nil {
return err
}
return b.GetWebDriver().ResizeWindow(current, w, h)
}
func (b *BrowserSteps) iMaximizeResizeBrowserWindow() error {
current, err := b.GetWebDriver().CurrentWindowHandle()
if err != nil {
return err
}
return b.GetWebDriver().MaximizeWindow(current)
}
func (b *BrowserSteps) iScrollTo(where string) error {
var script string
switch where {
case "top":
script = scrollTopScript
case "end":
script = scrollEndScript
default:
return fmt.Errorf("Invalid scroll direction. Got: '%s', allowed: 'top' or 'end'", where)
}
_, err := b.GetWebDriver().ExecuteScript(script, []interface{}{})
return err
}
func (b *BrowserSteps) iScrollToElement(selector, by string) error {
element, err := b.GetWebDriver().FindElement(by, selector)
if err != nil {
return err
}
pt, err := element.LocationInView()
if err != nil {
return err
}
_, err = b.GetWebDriver().ExecuteScript(
fmt.Sprintf("window.scrollTo(%d, %d);", pt.X, pt.Y), []interface{}{})
return err
}
func (b *BrowserSteps) buildNavigationSteps(s *godog.Suite) {
s.Step(`^I navigate to "([^"]*)"$`, b.iNavigateTo)
s.Step(`^I navigate forward$`, func() error { return b.GetWebDriver().Forward() })
s.Step(`^I navigate back$`, func() error { return b.GetWebDriver().Back() })
s.Step(`^I (reload|refresh)( the)? page$`, func() error { return b.GetWebDriver().Refresh() })
s.Step(`^I switch to new window$`, b.iSwitchToNewWindow)
s.Step(`^I switch to main window$`, b.iSwitchToMainWindow)
s.Step(`^I switch to previous window$`, b.iSwitchToPreviousWindow)
s.Step(`^I switch to window having (title|url) "(.*?)"$`, b.iSwitchToWindowHaving)
s.Step(`^I close new window$`, b.iCloseNewWindow)
s.Step(`^I resize browser window size to width (\d+) and height (\d+)$`, b.iResizeBrowserWindowTo)
s.Step(`^I resize browser window size to (\d+)x(\d+)$`, b.iResizeBrowserWindowTo)
s.Step(`^I maximize browser window$`, b.iMaximizeResizeBrowserWindow)
s.Step(`^I scroll to "([^"]*)" `+ByOption+`$`, b.iScrollToElement)
s.Step(`^I scroll to (top|end) of page$`, b.iScrollTo)
}