/
browser_steps.go
137 lines (111 loc) · 3.54 KB
/
browser_steps.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
package websteps
import (
"encoding/base64"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/spf13/afero"
"github.com/cbush06/godog/gherkin"
"github.com/cbush06/kosher/steps/steputils"
"github.com/sclevine/agouti/api"
)
func iMaximizeTheWindow(s *steputils.StepUtils) func() error {
return func() error {
if err := s.Page.Size(s.GetMaxWindowSize()); err != nil {
return fmt.Errorf("error encountered resizing window: %s", err)
}
return nil
}
}
func iWaitSeconds() func(int) error {
return func(seconds int) error {
time.Sleep(time.Duration(seconds) * time.Second)
return nil
}
}
func iTakeAScreenshot(s *steputils.StepUtils) func(*gherkin.Embeddings) error {
return func(e *gherkin.Embeddings) error {
now := time.Now()
fileName := "screenshot_" + now.Format("02Jan2006-150405.000.png")
filePath, _ := s.Settings.FileSystem.ResultsDir.RealPath(fileName)
if err := s.Page.Screenshot(filePath); err != nil {
return fmt.Errorf("error encountered while taking screenshot: %s", err)
}
imageBytes, _ := afero.ReadFile(s.Settings.FileSystem.ResultsDir, fileName)
e.AddEmbedding("image/png", base64.StdEncoding.EncodeToString(imageBytes))
return nil
}
}
func iSwitchToTheWindow(s *steputils.StepUtils) func(string) error {
return func(nth string) error {
var (
errMsg = fmt.Sprintf("error encountered while switching to [%s] window: ", nth) + "%s"
nthNumeric int
windowCount int
err error
)
// Determine how many windows there are
if windowCount, err = s.Page.WindowCount(); err != nil {
return fmt.Errorf(errMsg, fmt.Sprintf("error encountered determining number of open windows: %s", err))
}
// if something other than an integer was specified
if nthNumeric, err = strconv.Atoi(nth); err != nil {
// if "first" or "last" was specified
if strings.EqualFold(nth, "first") {
return iSwitchToWindowIndex(s, nth, 0)
}
if strings.EqualFold(nth, "last") {
return iSwitchToWindowIndex(s, nth, windowCount-1)
}
re := regexp.MustCompile(`\d+`)
var match string
if match = re.FindString(nth); len(match) < 1 {
return fmt.Errorf(errMsg, "no valid index specified for window")
}
nthNumeric, _ = strconv.Atoi(match)
}
if nthNumeric > windowCount {
return fmt.Errorf(errMsg, fmt.Sprintf("requested switch to window [%d], but only [%d] windows found", nthNumeric, windowCount))
}
return iSwitchToWindowIndex(s, nth, nthNumeric-1)
}
}
func iSwitchToWindowIndex(s *steputils.StepUtils, nth string, idx int) error {
var (
windows []*api.Window
errMsg = fmt.Sprintf("error encountered while switching to [%s] window: ", nth) + "%s"
err error
)
if windows, err = s.Page.Session().GetWindows(); err != nil {
return fmt.Errorf(errMsg, err)
}
return s.Page.Session().SetWindow(windows[idx])
}
func iReloadThePage(s *steputils.StepUtils) func() error {
return func() error {
return s.Page.Refresh()
}
}
func sendKeysToActiveElement(s *steputils.StepUtils) func(string) error {
return func(value string) error {
var (
activeEl *api.Element
interpolatedValue string
err error
errMsg = "error encountered while sending keys to active element: %s"
)
if activeEl, err = s.Page.Session().GetActiveElement(); err != nil {
return fmt.Errorf(errMsg, err)
}
// replace variables in value
if interpolatedValue, err = s.ReplaceVariables(value); err != nil {
return fmt.Errorf(errMsg, err)
}
if err = activeEl.Value(interpolatedValue); err != nil {
return fmt.Errorf(errMsg, err)
}
return nil
}
}