-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
run_test.go
189 lines (168 loc) · 4.22 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package playwright
import (
"bufio"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync"
"testing"
"github.com/mitchellh/go-ps"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRunOptionsRedirectStderr(t *testing.T) {
r, w := io.Pipe()
var output string
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
buf := bufio.NewReader(r)
for {
line, _, err := buf.ReadLine()
if err == io.EOF {
break
}
output += string(line)
}
_ = r.Close()
}()
driverPath := t.TempDir()
options := &RunOptions{
Stderr: w,
DriverDirectory: driverPath,
Browsers: []string{},
Verbose: true,
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
}))
defer ts.Close()
t.Setenv("PLAYWRIGHT_DOWNLOAD_HOST", ts.URL)
driver, err := NewDriver(options)
require.NoError(t, err)
err = driver.Install()
require.Error(t, err)
require.NoError(t, w.Close())
wg.Wait()
assert.Contains(t, output, "Downloading driver")
require.Contains(t, output, fmt.Sprintf("path=%s", driverPath))
}
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 getBrowserName() != "chromium" {
t.Skip("chromium 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)
}
func TestGetNodeExecutable(t *testing.T) {
// When PLAYWRIGHT_NODEJS_PATH is set, use that path.
err := os.Setenv("PLAYWRIGHT_NODEJS_PATH", "envDir/node.exe")
require.NoError(t, err)
executable := getNodeExecutable("testDirectory")
assert.Equal(t, "envDir/node.exe", executable)
err = os.Unsetenv("PLAYWRIGHT_NODEJS_PATH")
require.NoError(t, err)
executable = getNodeExecutable("testDirectory")
assert.Contains(t, executable, "testDirectory")
}
// find and kill playwright process
func killPlaywrightProcess() error {
all, err := ps.Processes()
if err != nil {
return err
}
for _, process := range all {
if process.Executable() == "node" || process.Executable() == "node.exe" {
if process.PPid() == os.Getpid() {
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"
}