# Browser API The `Browser` class is the main entry point for browser automation. > **Heads-up.** `browser.click()`, `browser.find()`, `browser.evaluate()` > and the other DOM-touching shortcuts target the focused page in > `browser.defaultContext` — i.e. `(await browser.activePage()).X(...)`. > They never reach into pages inside a context created by > `browser.newContext()`. See > [Getting Started — `browser.X()` vs `page.X()`](./getting-started.md#browserx-vs-pagex--when-to-use-which). ## Launching ```typescript import { Browser } from 'craftdriver'; const browser = await Browser.launch({ browserName: 'chrome' }); // With pre-loaded session state const browser = await Browser.launch({ browserName: 'chrome', storageState: './auth.json', }); ``` ### Launch options | Option | Type | Default | Description | |--------|------|---------|-------------| | `browserName` | `'chrome' \| 'chromium' \| 'firefox'` | `'chrome'` | Browser to launch | | `enableBiDi` | `boolean` | `true` | Use WebDriver BiDi. Set `false` only when running against a browser that does not support it. | | `storageState` | `string` | — | Path to a session-state JSON file to load on startup | | `mobileEmulation` | `MobileEmulation \| DeviceName` | — | Mobile device emulation settings (Chrome/Chromium only) | | `downloadsDir` | `string` | temp dir | Directory where downloaded files are saved | ## Navigation ```typescript // Navigate to URL — waits for the page load event (default) await browser.navigateTo('https://example.com'); // Wait only until DOMContentLoaded (faster — skips images/fonts) await browser.navigateTo('https://example.com', { waitUntil: 'domcontentloaded' }); // Wait until no requests are in-flight for 500 ms after load await browser.navigateTo('https://example.com', { waitUntil: 'networkidle' }); // Fire-and-forget — does not wait for any load event await browser.navigateTo('https://example.com', { waitUntil: 'none' }); // Get current URL const url = await browser.url(); // Get page title const title = await browser.title(); ``` ### `waitForLoadState(state?, opts?)` Wait for the page to reach a given load state after an action that triggers navigation (e.g. clicking a link or submitting a form). ```typescript // After an action that causes navigation: await browser.click('#submit'); await browser.waitForLoadState(); // default: 'load' await browser.waitForLoadState('domcontentloaded'); await browser.waitForLoadState('networkidle', { timeout: 10_000 }); ``` | State | Description | |-------|-------------| | `'load'` | Page `load` event fired (default) | | `'domcontentloaded'` | `DOMContentLoaded` fired — no waiting for images/fonts | | `'networkidle'` | `load` + no in-flight requests for 500 ms | Uses BiDi `browsingContext.load` / `browsingContext.domContentLoaded` events. ### History ```typescript await browser.goBack(); // history back await browser.goForward(); // history forward await browser.reload(); // reload the active page ``` All three proxy to the active page (`browser.activePage()`); use `page.goBack()` / `page.goForward()` / `page.reload()` directly when working with a specific tab. ### Page content ```typescript // Full document HTML (document.documentElement.outerHTML) const html = await browser.content(); // Replace the document with synthetic HTML and wait for the load event await browser.setContent('

Hello

'); // Faster — wait only for DOMContentLoaded await browser.setContent(html, { waitUntil: 'domcontentloaded' }); ``` Implementation: `setContent` navigates to a `data:text/html` URL so the new document goes through a real navigation and load events fire. ### Viewport ```typescript await browser.setViewportSize({ width: 1280, height: 720 }); ``` Uses BiDi `browsingContext.setViewport` to resize the **layout** viewport without changing the OS window. In Classic mode it falls back to `POST /session/{id}/window/rect`, which resizes the OS window — the inner viewport may end up a few pixels smaller because of browser chrome. For full mobile emulation (DPR, user-agent, touch), pass `mobileEmulation` to `Browser.launch` — see [Mobile emulation](./mobile-emulation.md). `setViewportSize` only changes the viewport box. ### Emulation `browser.emulate({...})` overrides `prefers-color-scheme`, `prefers-reduced-motion`, `forced-colors`, `navigator.language`, `Intl.*` timezone, and `navigator.onLine` for the current session. ```typescript await browser.emulate({ colorScheme: 'dark', locale: 'de-DE', timezoneId: 'Europe/Berlin', }); ``` `locale` and `timezoneId` work on Chrome, Chromium and Firefox. The CSS-media-feature fields and `offline` are Chromium-only — see [Emulation](./emulation.md) for the full option table and use cases. ## Element Interaction String selectors are CSS selectors. For semantic selectors, pass a `By.*` locator or use `browser.getByRole()`, `browser.getByText()`, `browser.getByLabel()`, and related helpers. ```typescript // Click an element await browser.click('#submit-btn'); await browser.click(By.text('Submit')); // Fill text input await browser.fill('#username', 'testuser'); // Clear input await browser.clear('#search'); ``` ### `getValue(selector, opts?)` Return the live `value` property of an ``, `