-
Notifications
You must be signed in to change notification settings - Fork 17
/
actions_test.go
121 lines (108 loc) · 2.5 KB
/
actions_test.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
package chromedpundetected
import (
"context"
"errors"
"testing"
"time"
"github.com/chromedp/chromedp"
"github.com/stretchr/testify/require"
)
func TestRunCommand(t *testing.T) {
testRun(t,
n,
NewConfig(
WithTimeout(20*time.Second),
WithHeadless(),
),
func(ctx context.Context) error {
version := make(map[string]string)
err := chromedp.Run(ctx,
RunCommandWithRes("Browser.getVersion", nil, &version),
)
t.Log("Version:", version)
return err
},
)
}
func TestBlockURLs(t *testing.T) {
btn := `//button[@title="Akkoord"]`
testRun(t,
n,
NewConfig(
WithTimeout(20*time.Second),
WithHeadless(),
),
func(ctx context.Context) error {
if err := chromedp.Run(ctx,
chromedp.Navigate("https://www.nu.nl/"),
chromedp.WaitVisible(btn),
chromedp.Click(btn),
); err != nil {
return err
}
if err := chromedp.Run(ctx,
BlockURLs("*.nu.nl"),
chromedp.Navigate("https://www.nu.nl/"),
chromedp.WaitVisible(btn),
); err != nil && !errors.Is(err, context.DeadlineExceeded) {
return err
}
return nil
},
)
}
func TestCookiesExtract(t *testing.T) {
btn := `//button[@title="Akkoord"]`
testRun(t,
n,
NewConfig(
WithTimeout(20*time.Second),
WithHeadless(),
),
func(ctx context.Context) error {
var cookies []Cookie
if err := chromedp.Run(ctx,
chromedp.Navigate("https://www.nu.nl/"),
chromedp.WaitVisible(btn),
chromedp.Click(btn),
chromedp.Sleep(2*time.Second),
SaveCookies(&cookies),
); err != nil {
return err
}
require.Greater(t, len(cookies), 0, "cookies len > 0")
t.Log("Cookies:")
for _, c := range cookies {
t.Log(c.Name, "=", c.Value)
}
return nil
},
)
}
func TestRunMouse(t *testing.T) {
testRun(t,
n,
NewConfig(
WithTimeout(20*time.Second),
// WithHeadless(),
),
func(ctx context.Context) error {
sleep := time.Second * 1
return chromedp.Run(ctx,
chromedp.Navigate("https://www.example.com/"),
chromedp.Evaluate("document.body.style.cursor = 'pointer'", nil),
// chromedp.Evaluate(coordinateCode, nil),
chromedp.Sleep(sleep),
MoveMouseToPosition(200, 200, WithVisualizeMouse()),
chromedp.Sleep(sleep),
MoveMouseToPosition(200, 500, WithVisualizeMouse()),
chromedp.Sleep(sleep),
MoveMouseToPosition(600, 500, WithVisualizeMouse()),
chromedp.Sleep(sleep),
MoveMouseToPosition(600, 200, WithVisualizeMouse()),
chromedp.Sleep(sleep),
MoveMouseToPosition(200, 200, WithVisualizeMouse()),
)
},
)
}