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

Copy Markdown View Source

API for collecting and saving Playwright traces. Playwright traces can be opened in Trace Viewer after Playwright script runs.

Start recording a trace before performing actions. At the end, stop tracing and save it to a file.

browser = Playwright.chromium(playwright) |> BrowserType.launch!()
context = Browser.new_context!(browser)
BrowserContext.tracing(context) |> Tracing.start!(screenshots: true, snapshots: true)
page = BrowserContext.new_page!(context)
Page.goto!(page, "https://playwright.dev")
assert Page.url(page) == "https://playwright.dev/"
BrowserContext.tracing(context) |> Tracing.stop!(path: "trace.zip")

Since Playwright 1.12.

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

Summary

Types

Options of group/3, see its docs.

Options of start_chunk/2, see its docs.

Options of start_har/3, see its docs.

Options of start/2, see its docs.

Options of stop_chunk/2, see its docs.

Options of stop/2, see its docs.

t()

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

Functions

Like group/3, but returns the value and raises on error.

Closes the last group created by Playwright.Tracing.group/3.

Like group_end/1, but returns the value and raises on error.

Start tracing. Disposing the returned Disposable stops tracing without saving the trace, similarly to calling Playwright.Tracing.stop/2 without a path.

Like start/2, but returns the value and raises on error.

Start a new trace chunk. Disposing the returned Disposable stops the chunk without saving it, similarly to calling Playwright.Tracing.stop_chunk/2 without a path. If you'd like to record multiple traces on the same BrowserContext, use Playwright.Tracing.start/2 once, and then create multiple trace chunks with Playwright.Tracing.start_chunk/2 and Playwright.Tracing.stop_chunk/2.

Like start_chunk/2, but returns the value and raises on error.

Start recording a HAR (HTTP Archive) of network activity in this context. The HAR file is written to disk when Playwright.Tracing.stop_har/1 is called, or when the returned Disposable is disposed.

Like start_har/3, but returns the value and raises on error.

Like stop/2, but returns the value and raises on error.

Stop the trace chunk. See Playwright.Tracing.start_chunk/2 for more details about multiple trace chunks.

Like stop_chunk/2, but returns the value and raises on error.

Stop HAR recording and save the HAR file to the path given to Playwright.Tracing.start_har/3.

Like stop_har/1, but returns the value and raises on error.

Types

group_options()

@type group_options() ::
  [
    {:location,
     %{
       :file => String.t(),
       optional(:line) => integer(),
       optional(:column) => integer()
     }}
  ]
  | %{
      optional(:location) => %{
        :file => String.t(),
        optional(:line) => integer(),
        optional(:column) => integer()
      }
    }

Options of group/3, see its docs.

start_chunk_options()

@type start_chunk_options() ::
  [name: String.t(), title: String.t()]
  | %{optional(:name) => String.t(), optional(:title) => String.t()}

Options of start_chunk/2, see its docs.

start_har_options()

@type start_har_options() ::
  [
    content: :omit | :embed | :attach,
    mode: :full | :minimal,
    resources_dir: String.t(),
    url_filter: String.t() | Regex.t() | Playwright.RegExp.t()
  ]
  | %{
      optional(:content) => :omit | :embed | :attach,
      optional(:mode) => :full | :minimal,
      optional(:resources_dir) => String.t(),
      optional(:url_filter) => String.t() | Regex.t() | Playwright.RegExp.t()
    }

Options of start_har/3, see its docs.

start_options()

@type start_options() ::
  [
    live: boolean(),
    name: String.t(),
    screenshots: boolean(),
    snapshots:
      boolean()
      | %{
          optional(:dom) => boolean(),
          optional(:aria) => boolean(),
          optional(:screen) => boolean()
        },
    sources: boolean(),
    title: String.t()
  ]
  | %{
      optional(:live) => boolean(),
      optional(:name) => String.t(),
      optional(:screenshots) => boolean(),
      optional(:snapshots) =>
        boolean()
        | %{
            optional(:dom) => boolean(),
            optional(:aria) => boolean(),
            optional(:screen) => boolean()
          },
      optional(:sources) => boolean(),
      optional(:title) => String.t()
    }

Options of start/2, see its docs.

stop_chunk_options()

@type stop_chunk_options() :: [{:path, String.t()}] | %{optional(:path) => String.t()}

Options of stop_chunk/2, see its docs.

stop_options()

@type stop_options() :: [{:path, String.t()}] | %{optional(:path) => String.t()}

Options of stop/2, see its docs.

t()

@type t() :: %Playwright.Tracing{
  conn: term(),
  guid: term(),
  initializer: term(),
  table: term(),
  type: term()
}

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

Functions

group(tracing, name, options \\ [])

@spec group(t(), String.t(), group_options()) ::
  {:ok, Playwright.Disposable.t()} | {:error, Playwright.error()}

Caution

Use test.step instead when available.

Creates a new group within the trace, assigning any subsequent API calls to this group, until Playwright.Tracing.group_end/1 is called. Groups can be nested and will be visible in the trace viewer.

Usage

# All actions between group and group_end
# will be shown in the trace viewer as a group.
Page.context(page) |> BrowserContext.tracing() |> Tracing.group!("Open Playwright.dev > API")
Page.goto!(page, "https://playwright.dev/")
Page.get_by_role(page, :link, name: "API") |> Locator.click!()
Page.context(page) |> BrowserContext.tracing() |> Tracing.group_end!()

Since Playwright 1.49.

Arguments

  • name - Group name shown in the trace viewer.

Options

  • :location (map/0) - Specifies a custom location for the group to be shown in the trace viewer. Defaults to the location of the Playwright.Tracing.group/3 call.
    • :file
    • :line
    • :column

group!(tracing, name, options \\ [])

@spec group!(t(), String.t(), group_options()) :: Playwright.Disposable.t()

Like group/3, but returns the value and raises on error.

group_end(tracing)

@spec group_end(t()) :: :ok | {:error, Playwright.error()}

Closes the last group created by Playwright.Tracing.group/3.

Since Playwright 1.49.

group_end!(tracing)

@spec group_end!(t()) :: :ok

Like group_end/1, but returns the value and raises on error.

start(tracing, options \\ [])

@spec start(t(), start_options()) ::
  {:ok, Playwright.Disposable.t()} | {:error, Playwright.error()}

Start tracing. Disposing the returned Disposable stops tracing without saving the trace, similarly to calling Playwright.Tracing.stop/2 without a path.

Usage

BrowserContext.tracing(context) |> Tracing.start!(screenshots: true, snapshots: true)
page = BrowserContext.new_page!(context)
Page.goto!(page, "https://playwright.dev")
assert Page.url(page) == "https://playwright.dev/"
BrowserContext.tracing(context) |> Tracing.stop!(path: "trace.zip")

Since Playwright 1.12.

Options

  • :live (boolean/0) - Since Playwright 1.59. When enabled, the trace is written to an unarchived file that is updated in real time as actions occur, instead of caching changes and archiving them into a zip file at the end. This is useful for live trace viewing during test execution.
  • :name (String.t/0) - If specified, intermediate trace files are going to be saved into the files with the given name prefix inside the :traces_dir directory specified in Playwright.BrowserType.launch/2. To specify the final trace zip file name, you need to pass path option to Playwright.Tracing.stop/2 instead.
  • :screenshots (boolean/0) - Whether to capture screenshots during tracing. Screenshots are used to build a timeline preview.
  • :snapshots (boolean/0 | map/0) - Which snapshots to capture on every action. Passing true is a shortcut for { dom: true }.

    • :dom - Capture DOM snapshot on every action and record network activity. Optional.
    • :aria - Capture aria snapshot of the page on every action. Optional.
    • :screen - Capture a screenshot of the page on every action. Optional.
  • :sources (boolean/0) - Since Playwright 1.17. Whether to include source files for trace actions.
  • :title (String.t/0) - Since Playwright 1.17. Trace name to be shown in the Trace Viewer.

start!(tracing, options \\ [])

@spec start!(t(), start_options()) :: Playwright.Disposable.t()

Like start/2, but returns the value and raises on error.

start_chunk(tracing, options \\ [])

@spec start_chunk(t(), start_chunk_options()) ::
  {:ok, Playwright.Disposable.t()} | {:error, Playwright.error()}

Start a new trace chunk. Disposing the returned Disposable stops the chunk without saving it, similarly to calling Playwright.Tracing.stop_chunk/2 without a path. If you'd like to record multiple traces on the same BrowserContext, use Playwright.Tracing.start/2 once, and then create multiple trace chunks with Playwright.Tracing.start_chunk/2 and Playwright.Tracing.stop_chunk/2.

Usage

BrowserContext.tracing(context) |> Tracing.start!(screenshots: true, snapshots: true)
page = BrowserContext.new_page!(context)
Page.goto!(page, "https://playwright.dev")

BrowserContext.tracing(context) |> Tracing.start_chunk!()
Page.get_by_text(page, "Get Started") |> Locator.click!()
# Everything between start_chunk and stop_chunk will be recorded in the trace.
BrowserContext.tracing(context) |> Tracing.stop_chunk!(path: "trace1.zip")

BrowserContext.tracing(context) |> Tracing.start_chunk!()
Page.goto!(page, "http://example.com")
# Save a second trace file with different actions.
BrowserContext.tracing(context) |> Tracing.stop_chunk!(path: "trace2.zip")

Since Playwright 1.15.

Options

  • :name (String.t/0) - Since Playwright 1.32. If specified, intermediate trace files are going to be saved into the files with the given name prefix inside the :traces_dir directory specified in Playwright.BrowserType.launch/2. To specify the final trace zip file name, you need to pass path option to Playwright.Tracing.stop_chunk/2 instead.
  • :title (String.t/0) - Since Playwright 1.17. Trace name to be shown in the Trace Viewer.

start_chunk!(tracing, options \\ [])

@spec start_chunk!(t(), start_chunk_options()) :: Playwright.Disposable.t()

Like start_chunk/2, but returns the value and raises on error.

start_har(tracing, path, options \\ [])

@spec start_har(t(), String.t(), start_har_options()) ::
  {:ok, Playwright.Disposable.t()} | {:error, Playwright.error()}

Start recording a HAR (HTTP Archive) of network activity in this context. The HAR file is written to disk when Playwright.Tracing.stop_har/1 is called, or when the returned Disposable is disposed.

Only one HAR recording can be active at a time per Tracing instance.

Usage

BrowserContext.tracing(context) |> Tracing.start_har!("trace.har")
page = BrowserContext.new_page!(context)
Page.goto!(page, "https://playwright.dev")
BrowserContext.tracing(context) |> Tracing.stop_har!()

Since Playwright 1.60.

Arguments

  • path - Path on the filesystem to write the HAR file to. If the file name ends with .zip, the HAR is saved as a zip archive with response bodies attached as separate files.

Options

  • :content (:omit | :embed | :attach) - Optional setting to control resource content management. If omit is specified, content is not persisted. If attach is specified, resources are persisted as separate files or entries in the ZIP archive. If embed is specified, content is stored inline the HAR file as per HAR specification. Defaults to :attach for .zip output files and to embed for all other file extensions.

  • :mode (:full | :minimal) - When set to minimal, only record information necessary for routing from HAR. This omits sizes, timing, page, cookies, security and other types of HAR information that are not used when replaying from HAR. Defaults to :full.

  • :resources_dir (String.t/0) - Only used together with content: 'attach'. When set, response bodies are placed in this directory instead of next to the HAR file. Not compatible with a .zip HAR file.
  • :url_filter (String.t/0 | Regex.t/0 | Playwright.RegExp.t/0) - A glob or regex pattern to filter requests that are stored in the HAR. Defaults to none.

start_har!(tracing, path, options \\ [])

@spec start_har!(t(), String.t(), start_har_options()) :: Playwright.Disposable.t()

Like start_har/3, but returns the value and raises on error.

stop(tracing, options \\ [])

@spec stop(t(), stop_options()) :: :ok | {:error, Playwright.error()}

Stop tracing.

Since Playwright 1.12.

Options

  • :path (String.t/0) - Export trace into the file with the given path.

stop!(tracing, options \\ [])

@spec stop!(t(), stop_options()) :: :ok

Like stop/2, but returns the value and raises on error.

stop_chunk(tracing, options \\ [])

@spec stop_chunk(t(), stop_chunk_options()) :: :ok | {:error, Playwright.error()}

Stop the trace chunk. See Playwright.Tracing.start_chunk/2 for more details about multiple trace chunks.

Since Playwright 1.15.

Options

stop_chunk!(tracing, options \\ [])

@spec stop_chunk!(t(), stop_chunk_options()) :: :ok

Like stop_chunk/2, but returns the value and raises on error.

stop_har(tracing)

@spec stop_har(t()) :: :ok | {:error, Playwright.error()}

Stop HAR recording and save the HAR file to the path given to Playwright.Tracing.start_har/3.

Since Playwright 1.60.

stop_har!(tracing)

@spec stop_har!(t()) :: :ok

Like stop_har/1, but returns the value and raises on error.