(t *testing.T, tc testCase, typ playwright.BrowserType, anubisURL string)
| 429 | } |
| 430 | |
| 431 | func executeTestCase(t *testing.T, tc testCase, typ playwright.BrowserType, anubisURL string) (action, error) { |
| 432 | deadline, _ := t.Deadline() |
| 433 | |
| 434 | browser, err := typ.Connect(buildBrowserConnect(typ.Name()), playwright.BrowserTypeConnectOptions{ |
| 435 | ExposeNetwork: playwright.String("<loopback>"), |
| 436 | }) |
| 437 | if err != nil { |
| 438 | return "", fmt.Errorf("could not connect to remote browser: %w", err) |
| 439 | } |
| 440 | defer browser.Close() |
| 441 | |
| 442 | ctx, err := browser.NewContext(playwright.BrowserNewContextOptions{ |
| 443 | AcceptDownloads: playwright.Bool(false), |
| 444 | ExtraHttpHeaders: map[string]string{ |
| 445 | "X-Real-Ip": tc.realIP, |
| 446 | }, |
| 447 | UserAgent: playwright.String(tc.userAgent), |
| 448 | }) |
| 449 | if err != nil { |
| 450 | return "", fmt.Errorf("could not create context: %w", err) |
| 451 | } |
| 452 | defer ctx.Close() |
| 453 | |
| 454 | page, err := ctx.NewPage() |
| 455 | if err != nil { |
| 456 | return "", fmt.Errorf("could not create page: %w", err) |
| 457 | } |
| 458 | defer page.Close() |
| 459 | |
| 460 | // Attempt challenge. |
| 461 | |
| 462 | start := time.Now() |
| 463 | _, err = page.Goto(anubisURL, playwright.PageGotoOptions{ |
| 464 | Timeout: pwTimeout(tc, deadline), |
| 465 | }) |
| 466 | if err != nil { |
| 467 | return "", pwFail(t, page, "could not navigate to test server: %v", err) |
| 468 | } |
| 469 | |
| 470 | hadChallenge := false |
| 471 | switch tc.action { |
| 472 | case actionChallenge: |
| 473 | // FIXME: This could race if challenge is completed too quickly. |
| 474 | checkImage(t, tc, deadline, page, "#image[src*=pensive], #image[src*=happy]") |
| 475 | hadChallenge = true |
| 476 | case actionDeny: |
| 477 | checkImage(t, tc, deadline, page, "#image[src*=sad]") |
| 478 | return actionDeny, nil |
| 479 | } |
| 480 | |
| 481 | // Ensure protected resource was provided. |
| 482 | |
| 483 | res, err := page.Locator("#anubis-test").TextContent(playwright.LocatorTextContentOptions{ |
| 484 | Timeout: pwTimeout(tc, deadline), |
| 485 | }) |
| 486 | end := time.Now() |
| 487 | if err != nil { |
| 488 | pwFail(t, page, "could not get text content: %v", err) |
no test coverage detected