-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathchrome_test.go
73 lines (59 loc) · 1.35 KB
/
chrome_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
package chromedpundetected
import (
"context"
"fmt"
"testing"
"time"
"github.com/chromedp/chromedp"
"github.com/hashicorp/go-multierror"
)
var n = 3
func TestChromedpundetected(t *testing.T) {
testRun(t,
n,
NewConfig(
WithTimeout(20*time.Second),
WithHeadless(),
),
func(ctx context.Context) error {
return chromedp.Run(ctx,
chromedp.Navigate("https://nowsecure.nl"),
chromedp.WaitVisible(`//div[@class="hystericalbg"]`),
)
},
)
t.Logf("Undetected!")
}
func testRun(t *testing.T, n int, cfg Config, run func(ctx context.Context) error) {
var gerr error
var success bool
// Attempt to run the tests multiple times, as in CI they are unstable.
for i := 0; i < n; i++ {
t.Logf("Attempt %d/%d", i+1, n)
ctx, cancel, err := New(cfg)
if err != nil {
gerr = multierror.Append(gerr, fmt.Errorf("create context: %w", err))
continue
}
if err = run(ctx); err != nil {
gerr = multierror.Append(gerr, fmt.Errorf("chromedp run: %w", err))
// Close Chrome instance.
ctxN, cancelN := context.WithTimeout(ctx, time.Second*10)
if err := chromedp.Cancel(ctxN); err != nil {
t.Logf("failed to cancel: %v", err)
}
cancelN()
cancel()
continue
}
success = true
if gerr != nil {
t.Logf("attempt %d, errors: %s", i+1, gerr.Error())
}
cancel()
break
}
if !success {
t.Fatal(gerr)
}
}