/
action.go
169 lines (154 loc) · 4.38 KB
/
action.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
package cdp
import (
"context"
"errors"
"fmt"
"time"
proto "github.com/chromedp/cdproto/cdp"
"github.com/chromedp/chromedp"
"github.com/vogtp/som/pkg/core/log"
"github.com/vogtp/som/pkg/monitor/szenario"
)
func (cdp *Engine) Either(name string, option ...szenario.EitherOption) <-chan any {
cdp.muStep.Lock()
defer cdp.muStep.Unlock()
cdp.stepInfo.start(name)
defer cdp.stepInfo.end(name)
res := make(chan any)
for _, o := range option {
go func(o szenario.EitherOption) {
err := chromedp.Run(cdp.browser, o.Action)
if err != nil {
cdp.log.Debug("Unmached Option", "either", name, "option", o.ID, log.Error, err)
res <- err
return
}
cdp.log.Info("Selected option", "either", name, "option", o.ID)
res <- o.ID
}(o)
}
return res
}
// StepTimeout executes a Step with an timeout
func (cdp *Engine) StepTimeout(name string, timeout time.Duration, actions ...chromedp.Action) error {
defer cdp.endStepActions()
errChan := make(chan error)
go func() {
errChan <- cdp.step(name, actions...)
}()
select {
case err := <-errChan:
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
err = fmt.Errorf("step timeout (%v) reached", timeout)
cdp.ErrorScreenshot(err)
}
}
return err
case <-time.After(timeout):
return fmt.Errorf("step timeout %v reached", timeout)
}
}
// Step executes the actions given and records how long it takes
func (cdp *Engine) Step(name string, actions ...chromedp.Action) {
defer cdp.endStepActions()
if err := cdp.step(name, actions...); err != nil {
cdp.ErrorScreenshot(err)
panic(err)
}
}
func (cdp *Engine) step(name string, actions ...chromedp.Action) error {
cdp.muStep.Lock()
defer cdp.muStep.Unlock()
cdp.stepInfo.start(name)
defer cdp.stepInfo.end(name)
err := chromedp.Run(cdp.browser, actions...)
if errors.Is(err, context.Canceled) {
err = fmt.Errorf("%s timeout %v", cdp.szenario.Name(), cdp.timeout)
}
return err
}
// IsPresent checks if something is present
func (cdp *Engine) IsPresent(sel interface{}, opts ...chromedp.QueryOption) bool {
cdp.muStep.Lock()
defer cdp.muStep.Unlock()
p := make(chan bool)
go func() {
nodes := new([]*proto.Node)
if err := chromedp.Run(cdp.browser, chromedp.Nodes(sel, nodes, opts...)); err != nil {
if errors.Is(err, context.Canceled) {
p <- false
return
}
cdp.ErrorScreenshot(err)
p <- false
return
}
p <- len(*nodes) > 0
}()
select {
case res := <-p:
return res
case <-time.After(100 * time.Millisecond):
return false
}
}
// Body is used to check the content of the page
func (cdp *Engine) Body(checks ...szenario.CheckFunc) chromedp.Action {
return chromedp.ActionFunc(func(ctx context.Context) error {
var body string
if err := chromedp.Run(cdp.browser, chromedp.Text(`/html/body`, &body)); err != nil {
return err
}
var errTot error
for i := 1; i < 5; i++ {
errTot = nil
for _, f := range checks {
if err := f(&body); err != nil {
errTot = err
}
}
if errTot == nil {
return nil
}
time.Sleep(time.Duration(i) * 100 * time.Millisecond)
}
return errTot
})
}
// WaitForEver blocks until the timeout is reached
func (cdp *Engine) WaitForEver() {
//nolint:errcheck
chromedp.Run(cdp.browser, chromedp.WaitReady(`#ThisWillNotBeFoundAndWeWaitForEver`, chromedp.ByID))
}
// GetURL returns the current URL
func (cdp *Engine) GetURL() string {
var href string
if err := chromedp.Run(cdp.browser, chromedp.Evaluate(`window.location.href`, &href)); err != nil {
return fmt.Sprintf("Cannot get href: %v", err)
}
return href
}
// SetInputField sets a HTML input field and validates that it has been set
func (cdp *Engine) SetInputField(stepName string, sel interface{}, value string, opts ...func(*chromedp.Selector)) error {
cdp.Step(stepName,
chromedp.WaitReady(sel, opts...),
chromedp.WaitEnabled(sel, opts...),
chromedp.SendKeys(sel, value, opts...),
)
stepLabel := fmt.Sprintf("validating input of %q", stepName)
var checkVal string
for i := 0; i < 5; i++ {
err := cdp.StepTimeout(stepLabel, 10*time.Millisecond*time.Duration(i+1),
chromedp.Value(sel, &checkVal, opts...),
)
if err != nil {
cdp.log.Warn("Checking input value failed", "err", err, "step", stepName, "selector", sel)
}
fmt.Printf("%v Val: %v check val: %v err: %v\n", i, value, checkVal, err)
if value == checkVal {
return nil
}
}
return fmt.Errorf("step %s: failed to stet value of %v", stepName, sel)
}