forked from Davincible/chromedp-undetected
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction_test.go
92 lines (81 loc) · 1.7 KB
/
action_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
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
},
)
}