/ tests / google-cdp.spec.ts
google-cdp.spec.ts
 1  import { test as base, chromium, expect, type Page } from '@playwright/test';
 2  
 3  const expectedWeekday = new Intl.DateTimeFormat('en-US', {
 4    weekday: 'long',
 5  }).format(new Date());
 6  
 7  // Custom test fixture that connects to an already-running Chrome via CDP
 8  const test = base.extend<{ cdpPage: Page }>({
 9    // eslint-disable-next-line no-empty-pattern
10    async cdpPage({}, use) {
11      const browser = await chromium.connectOverCDP('http://localhost:9222');
12      const context = browser.contexts()[0] ?? await browser.newContext();
13      const page = context.pages()[0] ?? await context.newPage();
14      await use(page);
15      // Don't close — we want the browser to stay open
16    },
17  });
18  
19  test.describe('Google via existing browser (CDP)', () => {
20    test('searching for today date shows the current weekday', async ({ cdpPage: page }) => {
21      await page.goto('https://www.google.com/?hl=en');
22  
23      // Handle consent dialog if it appears
24      const consentButton = page
25        .locator('button, [role="button"]')
26        .filter({ hasText: /accept all|i agree/i })
27        .first();
28  
29      if (await consentButton.isVisible({ timeout: 3000 }).catch(() => false)) {
30        await consentButton.click();
31      }
32  
33      const searchBox = page.locator('textarea[name="q"], input[name="q"]').first();
34      await expect(searchBox).toBeVisible();
35      await searchBox.fill("today's date");
36      await searchBox.press('Enter');
37  
38      await expect(page).toHaveURL(/\/search/);
39      await expect(page.locator('body')).toContainText(expectedWeekday, { timeout: 10000 });
40    });
41  });