Skip to content

Commit b866add

Browse files
committed
feat(scaletest/dashboard): write screenshot on error
1 parent 015e564 commit b866add

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

scaletest/dashboard/chromedp.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package dashboard
22

33
import (
44
"context"
5+
"fmt"
56
"net/url"
67
"os"
8+
"path/filepath"
79
"time"
810

911
"github.com/chromedp/cdproto/cdp"
@@ -221,6 +223,26 @@ func visitMainPage(ctx context.Context, u *url.URL) error {
221223
return chromedp.Run(ctx, chromedp.Navigate(u.String()))
222224
}
223225

226+
func screenshot(ctx context.Context, name string) (string, error) {
227+
var buf []byte
228+
if err := chromedp.Run(ctx, chromedp.CaptureScreenshot(&buf)); err != nil {
229+
return "", xerrors.Errorf("capture screenshot: %w", err)
230+
}
231+
fname := fmt.Sprintf("scaletest-dashboard-%s-%s.png", name, time.Now().Format("20060102-150405"))
232+
pwd := os.Getenv("PWD")
233+
fpath := filepath.Join(pwd, fname)
234+
f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY, 0644)
235+
if err != nil {
236+
return "", xerrors.Errorf("open file: %w", err)
237+
}
238+
defer f.Close()
239+
if _, err := f.Write(buf); err != nil {
240+
return "", xerrors.Errorf("write file: %w", err)
241+
}
242+
243+
return fpath, nil
244+
}
245+
224246
// pick chooses a random element from a slice.
225247
// If the slice is empty, it returns the zero value of the type.
226248
func pick[T any](s []T, randIntn func(int) int) T {

scaletest/dashboard/run.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func (r *Runner) Run(ctx context.Context, _ string, _ io.Writer) error {
7373
l, act, err := r.cfg.ActionFunc(cdpCtx, r.cfg.Logger, r.cfg.RandIntn, actionCompleteByDeadline)
7474
if err != nil {
7575
r.cfg.Logger.Error(ctx, "calling ActionFunc", slog.Error(err))
76+
sPath, sErr := screenshot(cdpCtx, me.Username)
77+
if sErr != nil {
78+
r.cfg.Logger.Error(ctx, "screenshot failed", slog.Error(sErr))
79+
}
80+
r.cfg.Logger.Info(ctx, "screenshot saved", slog.F("path", sPath))
7681
continue
7782
}
7883
start := time.Now()
@@ -83,6 +88,11 @@ func (r *Runner) Run(ctx context.Context, _ string, _ io.Writer) error {
8388
r.metrics.IncErrors(string(l))
8489
//nolint:gocritic
8590
r.cfg.Logger.Error(ctx, "action failed", slog.F("label", l), slog.Error(err))
91+
sPath, sErr := screenshot(cdpCtx, me.Username+"-"+string(l))
92+
if sErr != nil {
93+
r.cfg.Logger.Error(ctx, "screenshot failed", slog.Error(sErr))
94+
}
95+
r.cfg.Logger.Info(ctx, "screenshot saved", slog.F("path", sPath))
8696
} else {
8797
//nolint:gocritic
8898
r.cfg.Logger.Info(ctx, "action success", slog.F("label", l))

0 commit comments

Comments
 (0)