Files
Ashley Harrison 2d1fb94f2d Playwright: Gate server start up with GRAFANA_URL env var (#108302)
* refactor so server is created unless GRAFANA_URL is provided

* update documentation

* don't swallow errors from the server process
2025-07-18 17:08:47 +01:00

107 lines
2.5 KiB
Go

package main
import (
"context"
"fmt"
"strings"
"dagger.io/dagger"
)
var (
// Locations in the container to write results to
testResultsDir = "/playwright-test-results"
htmlResultsDir = "/playwright-html-report"
blobResultsDir = "/playwright-blob-report"
)
type RunTestOpts struct {
GrafanaService *dagger.Service
FrontendContainer *dagger.Container
HostSrc *dagger.Directory
Shard string
HTMLReportExportDir string
BlobReportExportDir string
TestResultsExportDir string
}
func RunTest(
ctx context.Context,
d *dagger.Client,
opts RunTestOpts,
) (*dagger.Container, error) {
playwrightCommand := buildPlaywrightCommand(opts)
grafanaHost, err := opts.GrafanaService.Hostname(ctx)
if err != nil {
return nil, err
}
e2eContainer := opts.FrontendContainer.
WithWorkdir("/src").
WithDirectory("/src", opts.HostSrc).
WithMountedCache(".nx", d.CacheVolume("nx-cache")).
WithEnvVariable("CI", "true").
WithEnvVariable("GRAFANA_URL", fmt.Sprintf("http://%s:%d", grafanaHost, grafanaPort)).
WithServiceBinding(grafanaHost, opts.GrafanaService).
WithEnvVariable("bustcache", "1").
WithEnvVariable("PLAYWRIGHT_HTML_OPEN", "never").
WithEnvVariable("PLAYWRIGHT_HTML_OUTPUT_DIR", htmlResultsDir).
WithEnvVariable("PLAYWRIGHT_BLOB_OUTPUT_DIR", blobResultsDir).
WithExec(playwrightCommand, dagger.ContainerWithExecOpts{
Expect: dagger.ReturnTypeAny,
})
if opts.TestResultsExportDir != "" {
_, err := e2eContainer.Directory(testResultsDir).Export(ctx, opts.TestResultsExportDir)
if err != nil {
return nil, err
}
}
if opts.HTMLReportExportDir != "" {
_, err := e2eContainer.Directory(htmlResultsDir).Export(ctx, opts.HTMLReportExportDir)
if err != nil {
return nil, err
}
}
if opts.BlobReportExportDir != "" {
_, err := e2eContainer.Directory(blobResultsDir).Export(ctx, opts.BlobReportExportDir)
if err != nil {
return nil, err
}
}
return e2eContainer, nil
}
func buildPlaywrightCommand(opts RunTestOpts) []string {
playwrightReporters := []string{
"dot", // minimal output in shards
}
if opts.HTMLReportExportDir != "" {
playwrightReporters = append(playwrightReporters, "html")
}
if opts.BlobReportExportDir != "" {
playwrightReporters = append(playwrightReporters, "blob")
}
playwrightCommand := []string{
"yarn",
"e2e:playwright",
"--reporter",
strings.Join(playwrightReporters, ","),
"--output",
testResultsDir,
}
if opts.Shard != "" {
playwrightCommand = append(playwrightCommand, "--shard", opts.Shard)
}
return playwrightCommand
}