Skip to content

Browser

David Ortner edited this page Jan 12, 2024 · 30 revisions

Browser represents an instance of a Happy DOM browser. The API is similar to the API for Puppeteer and Playwright.

Signature

class Browser implements IBrowser

Example 1

import { Browser } from "happy-dom";

const browser = new Browser({
   navigator: {
      userAgent: 'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/2.0.0'
   }
});
const page = browser.newPage();

await page.goto("https://example.com");

// Wait for all operations to complete
await page.waitUntilComplete();

await browser.close();

Example 2

import { Browser } from "happy-dom";

const browser = new Browser();
const page = browser.newPage();

page.url = 'https://example.com';
page.content = '<html><body>Hello world!</body></html>';

// Outputs "Hello world!"
console.log(page.mainFrame.document.body.textContent);

await browser.close();

Constructor

Parameter Type Description
options? object Browser options.
options.settings? IOptionalBrowserSettings Browser settings.
options.console? Console Console object. Each page will create an instance of VirtualConsole by default if not specified.

Properties

Property Modifiers Type Description
contexts readonly BrowserContext[] Browser contexts. Defaults to one main context.
settings readonly IBrowserSettings Browser settings. The settings can be modified runtime.
console readonly Console Console object sent in to the constructor.
defaultContext readonly BrowserContext Default context.

Methods

Method Return type Description
close() Promise<void> Closes the browser.
waitUntilComplete() Promise<void> Waits for all ongoing operations to complete. This includes operations done inside loaded pages, such as loading resources and executing scripts.
abort() Promise<void> Aborts all ongoing operations.
newIncognitoContext() BrowserContext Creates a new incognito context.
newPage() BrowserPage Creates a new page.

Clone this wiki locally