Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions server/cmd/api/api/playwright.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ const (
)

type playwrightDaemonRequest struct {
ID string `json:"id"`
Code string `json:"code"`
TimeoutMs int `json:"timeout_ms,omitempty"`
ID string `json:"id"`
Code string `json:"code"`
TimeoutMs int `json:"timeout_ms,omitempty"`
ResolveActivePage bool `json:"resolve_active_page"`
}

type playwrightDaemonResponse struct {
Expand Down Expand Up @@ -84,7 +85,7 @@ func (s *ApiService) ensurePlaywrightDaemon(ctx context.Context) error {
return fmt.Errorf("playwright daemon failed to start within %v", playwrightDaemonStartup)
}

func (s *ApiService) executeViaUnixSocket(ctx context.Context, code string, timeout time.Duration) (*playwrightDaemonResponse, error) {
func (s *ApiService) executeViaUnixSocket(ctx context.Context, code string, timeout time.Duration, resolveActivePage bool) (*playwrightDaemonResponse, error) {
conn, err := net.DialTimeout("unix", playwrightDaemonSocket, 2*time.Second)
if err != nil {
return nil, fmt.Errorf("failed to connect to daemon: %w", err)
Expand All @@ -97,9 +98,10 @@ func (s *ApiService) executeViaUnixSocket(ctx context.Context, code string, time

reqID := uuid.New().String()
req := playwrightDaemonRequest{
ID: reqID,
Code: code,
TimeoutMs: int(timeout.Milliseconds()),
ID: reqID,
Code: code,
TimeoutMs: int(timeout.Milliseconds()),
ResolveActivePage: resolveActivePage,
}

reqBytes, err := json.Marshal(req)
Expand Down Expand Up @@ -160,7 +162,8 @@ func (s *ApiService) ExecutePlaywrightCode(ctx context.Context, request oapi.Exe
}, nil
}

resp, err := s.executeViaUnixSocket(ctx, request.Body.Code, timeout)
resolveActivePage := request.Body.ResolveActivePage == nil || *request.Body.ResolveActivePage
resp, err := s.executeViaUnixSocket(ctx, request.Body.Code, timeout, resolveActivePage)
if err != nil {
log.Error("playwright execution failed", "error", err)
errorMsg := fmt.Sprintf("execution failed: %v", err)
Expand Down
11 changes: 11 additions & 0 deletions server/e2e/e2e_playwright_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ func TestPlaywrightExecuteAPI(t *testing.T) {
require.Zero(t, setupResult.FirstContextPageCount, "expected the first context to remain open without pages")
require.Equal(t, 2, setupResult.SecondContextPageCount, "expected a foreground page and a newer fallback page in the second context")

resolveActivePage := false
pageFreeRsp, err := client.ExecutePlaywrightCodeWithResponse(ctx, instanceoapi.ExecutePlaywrightCodeJSONRequestBody{
Code: `return browser.contexts().flatMap(browserContext => browserContext.pages()).length;`,
ResolveActivePage: &resolveActivePage,
})
require.NoError(t, err, "page-free request error: %v", err)
require.Equal(t, http.StatusOK, pageFreeRsp.StatusCode(), "page-free request returned %s body=%s", pageFreeRsp.Status(), string(pageFreeRsp.Body))
require.NotNil(t, pageFreeRsp.JSON200)
require.True(t, pageFreeRsp.JSON200.Success, "expected page-free request success=true")
require.EqualValues(t, 2, pageFreeRsp.JSON200.Result, "expected page-free request to list both pages in the second context")

crossContextRsp, err := client.ExecutePlaywrightCodeWithResponse(ctx, instanceoapi.ExecutePlaywrightCodeJSONRequestBody{
Code: `return { url: page.url(), contextPageUrls: page.context().pages().map(candidate => candidate.url()) };`,
})
Expand Down
Loading
Loading