Skip to content

Commit

Permalink
deploy: c6f85ed
Browse files Browse the repository at this point in the history
  • Loading branch information
canstand committed Dec 10, 2024
1 parent 145a993 commit 516a17e
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 22 deletions.
2 changes: 1 addition & 1 deletion assets/css/style.css.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions binding_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (b *bindingCallImpl) Call(f BindingCallFunction) {
if _, err := b.channel.Send("reject", map[string]interface{}{
"error": serializeError(r.(error)),
}); err != nil {
logger.Printf("could not reject BindingCall: %v\n", err)
logger.Error("could not reject BindingCall", "error", err)
}
}
}()
Expand All @@ -60,7 +60,7 @@ func (b *bindingCallImpl) Call(f BindingCallFunction) {
"result": serializeArgument(result),
})
if err != nil {
logger.Printf("could not resolve BindingCall: %v\n", err)
logger.Error("could not resolve BindingCall", "error", err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func (b *browserContextImpl) onRoute(route *routeImpl) {
return nil, err
}, true)
if err != nil {
logger.Printf("could not update interception patterns: %v\n", err)
logger.Error("could not update interception patterns", "error", err)
}
}
}
Expand Down Expand Up @@ -742,7 +742,7 @@ func (b *browserContextImpl) onWebSocketRoute(wr WebSocketRoute) {
b.Unlock()
_, err := wr.ConnectToServer()
if err != nil {
logger.Println(err)
logger.Error("could not connect to WebSocket server", "error", err)
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *channel) innerSendNoReply(method string, isInternal bool, options ...in
}, isInternal)
if err != nil {
// ignore error actively, log only for debug
logger.Printf("SendNoReply failed: %v\n", err)
logger.Error("SendNoReply failed", "error", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (f *frameImpl) ExpectNavigation(cb func() error, options ...FrameExpectNavi
err, ok := ev["error"]
if ok {
// Any failed navigation results in a rejection.
logger.Printf("navigated to %s error: %v", ev["url"].(string), err)
logger.Error("navigation error", "url", ev["url"].(string), "error", err)
return true
}
return matcher == nil || matcher.Matches(ev["url"].(string))
Expand Down
6 changes: 3 additions & 3 deletions har_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (r *harRouter) addContextRoute(context BrowserContext) error {
err := context.Route(r.urlOrPredicate, func(route Route) {
err := r.handle(route)
if err != nil {
logger.Println(err)
logger.Error("Error handling context route", "error", err)
}
})
if err != nil {
Expand All @@ -35,7 +35,7 @@ func (r *harRouter) addPageRoute(page Page) error {
err := page.Route(r.urlOrPredicate, func(route Route) {
err := r.handle(route)
if err != nil {
logger.Println(err)
logger.Error("Error handling page route", "error", err)
}
})
if err != nil {
Expand Down Expand Up @@ -84,7 +84,7 @@ func (r *harRouter) handle(route Route) error {
Headers: deserializeNameAndValueToMap(response.Headers),
})
case "error":
logger.Printf("har action error: %v\n", *response.Message)
logger.Error("har action error", "error", *response.Message)
fallthrough
case "noentry":
}
Expand Down
2 changes: 1 addition & 1 deletion page.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ func (p *pageImpl) onRoute(route *routeImpl) {
return nil, err
}, true)
if err != nil {
logger.Printf("could not update interception patterns: %v\n", err)
logger.Error("could not update interception patterns", "error", err)
}
}
}
Expand Down
21 changes: 12 additions & 9 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"log"
"log/slog"
"net/http"
"os"
"os/exec"
Expand All @@ -15,12 +16,10 @@ import (
"strings"
)

const (
playwrightCliVersion = "1.49.0"
)
const playwrightCliVersion = "1.49.0"

var (
logger = log.Default()
logger = slog.Default()
playwrightCDNMirrors = []string{
"https://playwright.azureedge.net",
"https://playwright-akamai.azureedge.net",
Expand Down Expand Up @@ -137,7 +136,7 @@ func (d *PlaywrightDriver) DownloadDriver() error {
return nil
}

d.log(fmt.Sprintf("Downloading driver to %s", d.options.DriverDirectory))
d.log("Downloading driver", "path", d.options.DriverDirectory)

body, err := downloadDriver(d.getDriverURLs())
if err != nil {
Expand Down Expand Up @@ -186,9 +185,9 @@ func (d *PlaywrightDriver) DownloadDriver() error {
return nil
}

func (d *PlaywrightDriver) log(s string) {
func (d *PlaywrightDriver) log(msg string, args ...any) {
if d.options.Verbose {
logger.Println(s)
logger.Info(msg, args...)
}
}

Expand Down Expand Up @@ -236,6 +235,7 @@ type RunOptions struct {
Verbose bool // default true
Stdout io.Writer
Stderr io.Writer
Logger *slog.Logger
}

// Install does download the driver and the browsers.
Expand Down Expand Up @@ -295,8 +295,11 @@ func transformRunOptions(options ...*RunOptions) (*RunOptions, error) {
}
if option.Stderr == nil {
option.Stderr = os.Stderr
} else {
logger.SetOutput(option.Stderr)
} else if option.Logger == nil {
log.SetOutput(option.Stderr)
}
if option.Logger != nil {
logger = option.Logger
}
return option, nil
}
Expand Down
46 changes: 46 additions & 0 deletions run_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,64 @@
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{
Expand Down
4 changes: 2 additions & 2 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (ws *webSocketImpl) onFrameSent(opcode float64, data string) {
if opcode == 2 {
payload, err := base64.StdEncoding.DecodeString(data)
if err != nil {
logger.Printf("could not decode WebSocket.onFrameSent payload: %v\n", err)
logger.Error("could not decode WebSocket.onFrameSent payload", "error", err)
return
}
ws.Emit("framesent", payload)
Expand All @@ -63,7 +63,7 @@ func (ws *webSocketImpl) onFrameReceived(opcode float64, data string) {
if opcode == 2 {
payload, err := base64.StdEncoding.DecodeString(data)
if err != nil {
logger.Printf("could not decode WebSocket.onFrameReceived payload: %v\n", err)
logger.Error("could not decode WebSocket.onFrameReceived payload", "error", err)
return
}
ws.Emit("framereceived", payload)
Expand Down

0 comments on commit 516a17e

Please sign in to comment.