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

Copy Markdown View Source

Coverage gathers information about parts of JavaScript and CSS that were used by the page.

An example of using JavaScript coverage to produce Istanbul report for page load:

Note

Coverage APIs are only supported on Chromium-based browsers.

browser = Playwright.chromium(playwright) |> BrowserType.launch!()
page = Browser.new_page!(browser)
Page.coverage(page) |> Coverage.start_js_coverage!()
Page.goto!(page, "https://chromium.org")
coverage = Page.coverage(page) |> Coverage.stop_js_coverage!()

for entry <- coverage do
  # v8-to-istanbul is a Node package: convert the V8 ranges with a tool of your choice.
  IO.puts(JSON.encode!(%{url: entry.url, functions: entry.functions}))
end

Browser.close!(browser)

Since Playwright 1.11.

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

Summary

Types

t()

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

Functions

Returns coverage is started

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

Returns coverage is started

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

Returns the array of coverage reports for all stylesheets

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

Returns the array of coverage reports for all scripts

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

Types

start_css_coverage_options()

@type start_css_coverage_options() ::
  [{:reset_on_navigation, boolean()}]
  | %{optional(:reset_on_navigation) => boolean()}

Options of start_css_coverage/2, see its docs.

start_js_coverage_options()

@type start_js_coverage_options() ::
  [report_anonymous_scripts: boolean(), reset_on_navigation: boolean()]
  | %{
      optional(:report_anonymous_scripts) => boolean(),
      optional(:reset_on_navigation) => boolean()
    }

Options of start_js_coverage/2, see its docs.

t()

@type t() :: %Playwright.Coverage{_channel: term()}

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

Functions

start_css_coverage(coverage, options \\ [])

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

Returns coverage is started

Since Playwright 1.11.

Options

  • :reset_on_navigation (boolean/0) - Whether to reset coverage on every navigation. Defaults to true.

start_css_coverage!(coverage, options \\ [])

@spec start_css_coverage!(t(), start_css_coverage_options()) :: :ok

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

start_js_coverage(coverage, options \\ [])

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

Returns coverage is started

Note

Anonymous scripts are ones that don't have an associated url. These are scripts that are dynamically created on the page using eval or new Function. If :report_anonymous_scripts is set to true, anonymous scripts will have an empty string as their URL.

Since Playwright 1.11.

Options

  • :report_anonymous_scripts (boolean/0) - Whether anonymous scripts generated by the page should be reported. Defaults to false.
  • :reset_on_navigation (boolean/0) - Whether to reset coverage on every navigation. Defaults to true. Note that passing false does not guarantee that coverage persists through navigations, due to browser architecture limitations.

start_js_coverage!(coverage, options \\ [])

@spec start_js_coverage!(t(), start_js_coverage_options()) :: :ok

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

stop_css_coverage(coverage)

@spec stop_css_coverage(t()) ::
  {:ok,
   [
     %{
       :url => String.t(),
       optional(:text) => String.t(),
       ranges: [%{start: integer(), end: integer()}]
     }
   ]}
  | {:error, Playwright.error()}

Returns the array of coverage reports for all stylesheets

Note

CSS Coverage doesn't include dynamically injected style tags without sourceURLs.

Since Playwright 1.11.

stop_css_coverage!(coverage)

@spec stop_css_coverage!(t()) :: [
  %{
    :url => String.t(),
    optional(:text) => String.t(),
    ranges: [%{start: integer(), end: integer()}]
  }
]

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

stop_js_coverage(coverage)

@spec stop_js_coverage(t()) ::
  {:ok,
   [
     %{
       :url => String.t(),
       :scriptId => String.t(),
       optional(:source) => String.t(),
       functions: [
         %{
           functionName: String.t(),
           isBlockCoverage: boolean(),
           ranges: [
             %{count: integer(), startOffset: integer(), endOffset: integer()}
           ]
         }
       ]
     }
   ]}
  | {:error, Playwright.error()}

Returns the array of coverage reports for all scripts

Note

JavaScript Coverage doesn't include anonymous scripts by default. However, scripts with sourceURLs are reported.

Since Playwright 1.11.

stop_js_coverage!(coverage)

@spec stop_js_coverage!(t()) :: [
  %{
    :url => String.t(),
    :scriptId => String.t(),
    optional(:source) => String.t(),
    functions: [
      %{
        functionName: String.t(),
        isBlockCoverage: boolean(),
        ranges: [
          %{count: integer(), startOffset: integer(), endOffset: integer()}
        ]
      }
    ]
  }
]

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