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
Options of start_css_coverage/2, see its docs.
Options of start_js_coverage/2, see its docs.
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
@type start_css_coverage_options() :: [{:reset_on_navigation, boolean()}] | %{optional(:reset_on_navigation) => boolean()}
Options of start_css_coverage/2, see its docs.
@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.
@type t() :: %Playwright.Coverage{_channel: term()}
A Coverage handle. Compare handles with ==, their fields are internal.
Functions
@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 totrue.
@spec start_css_coverage!(t(), start_css_coverage_options()) :: :ok
Like start_css_coverage/2, but returns the value and raises on error.
@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 tofalse.:reset_on_navigation(boolean/0) - Whether to reset coverage on every navigation. Defaults totrue. Note that passingfalsedoes not guarantee that coverage persists through navigations, due to browser architecture limitations.
@spec start_js_coverage!(t(), start_js_coverage_options()) :: :ok
Like start_js_coverage/2, but returns the value and raises on error.
@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.
@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.
@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.
@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.