Playwright.ConsoleMessage (Playwright v1.65.0-rc.3)

Copy Markdown View Source

ConsoleMessage objects are dispatched by page via the Page.on(:console) event. For each console message logged in the page there will be corresponding event in the Playwright context.

# Listen for all console logs
Page.on(page, :console, &IO.puts(ConsoleMessage.text(&1)))

# Listen for all console events and handle errors
Page.on(page, :console, fn msg ->
  if ConsoleMessage.type(msg) == "error", do: IO.puts(~s(Error text: "#{ConsoleMessage.text(msg)}"))
end)

# Get the next console log
msg_promise = Page.wait_for_event(page, :console)
# Issue console.log inside the page
Page.evaluate!(page, "() => console.log('hello', 42, { foo: 'bar' })")
msg = Promise.await!(msg_promise)

# Deconstruct console log arguments
ConsoleMessage.args(msg) |> Enum.at(0) |> JSHandle.json_value!()
# => "hello"
ConsoleMessage.args(msg) |> Enum.at(1) |> JSHandle.json_value!()
# => 42

Since Playwright 1.8.

The examples alias the modules they use (alias Playwright.{Locator, Page}).

Summary

Types

t()

A ConsoleMessage handle. Compare handles with ==, their fields are internal.

Functions

List of arguments passed to a console function call. See also Page.on(:console).

Since Playwright 1.8.

The page that produced this console message, if any.

The text of the console message.

The timestamp of the console message in milliseconds since the Unix epoch.

Since Playwright 1.8.

The web worker or service worker that produced this console message, if any. Note that console messages from web workers also have non-nil Playwright.ConsoleMessage.page/1.

Types

t()

@type t() :: %Playwright.ConsoleMessage{
  _event: term(),
  _page: term(),
  _worker: term()
}

A ConsoleMessage handle. Compare handles with ==, their fields are internal.

Functions

args(console_message)

@spec args(t()) :: [Playwright.JSHandle.t()]

List of arguments passed to a console function call. See also Page.on(:console).

Since Playwright 1.8.

location(console_message)

@spec location(t()) :: %{
  url: String.t(),
  line: integer(),
  column: integer(),
  lineNumber: integer(),
  columnNumber: integer()
}

Since Playwright 1.8.

page(console_message)

@spec page(t()) :: nil | Playwright.Page.t()

The page that produced this console message, if any.

Since Playwright 1.34.

text(console_message)

@spec text(t()) :: String.t()

The text of the console message.

Since Playwright 1.8.

timestamp(console_message)

@spec timestamp(t()) :: number()

The timestamp of the console message in milliseconds since the Unix epoch.

Since Playwright 1.59.

type(console_message)

@spec type(t()) :: String.t()

Since Playwright 1.8.

worker(console_message)

@spec worker(t()) :: nil | Playwright.Worker.t()

The web worker or service worker that produced this console message, if any. Note that console messages from web workers also have non-nil Playwright.ConsoleMessage.page/1.

Since Playwright 1.57.