forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_test.go
141 lines (129 loc) · 3.27 KB
/
run_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package playwright
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/mitchellh/go-ps"
"github.com/stretchr/testify/require"
)
func TestDriverInstall(t *testing.T) {
driverPath := t.TempDir()
driver, err := NewDriver(&RunOptions{
DriverDirectory: driverPath,
Browsers: []string{getBrowserName()},
Verbose: true})
if err != nil {
t.Fatalf("could not start driver: %v", err)
}
browserPath := t.TempDir()
err = os.Setenv("PLAYWRIGHT_BROWSERS_PATH", browserPath)
if err != nil {
t.Fatalf("could not set PLAYWRIGHT_BROWSERS_PATH: %v", err)
}
defer os.Unsetenv("PLAYWRIGHT_BROWSERS_PATH")
err = driver.Install()
if err != nil {
t.Fatalf("could not install driver: %v", err)
}
err = driver.Uninstall()
if err != nil {
t.Fatalf("could not uninstall driver: %v", err)
}
}
func TestDriverDownloadHostEnv(t *testing.T) {
driverPath := t.TempDir()
driver, err := NewDriver(&RunOptions{
DriverDirectory: driverPath,
SkipInstallBrowsers: true,
})
if err != nil {
t.Fatalf("could not start driver: %v", err)
}
uri := ""
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
uri = r.URL.String()
w.WriteHeader(404)
}))
defer ts.Close()
err = os.Setenv("PLAYWRIGHT_DOWNLOAD_HOST", ts.URL)
if err != nil {
t.Fatalf("could not set PLAYWRIGHT_DOWNLOAD_HOST: %v", err)
}
defer os.Unsetenv("PLAYWRIGHT_DOWNLOAD_HOST")
err = driver.Install()
if err == nil || !strings.Contains(err.Error(), "404 Not Found") || !strings.Contains(uri, "/builds/driver") {
t.Fatalf("PLAYWRIGHT_DOWNLOAD_HOST do not work: %v", err)
}
}
func TestShouldNotHangWhenPlaywrightUnexpectedExit(t *testing.T) {
if runtime.GOOS != "windows" || getBrowserName() != "chromium" {
t.Skip("chromium on windows only")
return
}
pw, err := Run()
require.NoError(t, err)
defer func() {
_ = pw.Stop()
}()
browser, err := pw.Chromium.Launch()
require.NoError(t, err)
context, err := browser.NewContext()
require.NoError(t, err)
err = killPlaywrightProcess()
require.NoError(t, err)
_, err = context.NewPage()
require.Error(t, err)
}
// find and kill playwright process, only for Windows
func killPlaywrightProcess() error {
all, err := ps.Processes()
if err != nil {
return err
}
for _, process := range all {
if process.Executable() == "node" || process.Executable() == "node.exe" {
parent, err := ps.FindProcess(process.PPid())
if err != nil {
return err
}
if parent.Executable() == "cmd.exe" {
grandpa, err := ps.FindProcess(parent.PPid())
if err != nil {
return err
}
if strings.HasPrefix(grandpa.Executable(), "__debug_bin") || grandpa.Executable() == filepath.Base(os.Args[0]) {
if err := killProcessByPid(parent.Pid()); err != nil {
return err
}
if err := killProcessByPid(process.Pid()); err != nil {
return err
}
return nil
}
}
}
}
return fmt.Errorf("playwright process not found")
}
func killProcessByPid(pid int) error {
process, err := os.FindProcess(pid)
if err != nil {
return err
}
if err := process.Kill(); err != nil {
return err
}
return nil
}
func getBrowserName() string {
browserName, hasEnv := os.LookupEnv("BROWSER")
if hasEnv {
return browserName
}
return "chromium"
}