# `Playwright.Coverage`
[🔗](https://github.com/ftes/playwright_ex/blob/v1.65.0-rc.3/lib/playwright/api/coverage.ex#L7)

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 {: .info}
>
> Coverage APIs are only supported on Chromium-based browsers.

```elixir
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}`).

# `start_css_coverage_options`

```elixir
@type start_css_coverage_options() ::
  [{:reset_on_navigation, boolean()}]
  | %{optional(:reset_on_navigation) =&gt; boolean()}
```

Options of `start_css_coverage/2`, see its docs.

# `start_js_coverage_options`

```elixir
@type start_js_coverage_options() ::
  [report_anonymous_scripts: boolean(), reset_on_navigation: boolean()]
  | %{
      optional(:report_anonymous_scripts) =&gt; boolean(),
      optional(:reset_on_navigation) =&gt; boolean()
    }
```

Options of `start_js_coverage/2`, see its docs.

# `t`

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

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

# `start_css_coverage`

```elixir
@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` (`t:boolean/0`) - Whether to reset coverage on every navigation. Defaults to `true`.

# `start_css_coverage!`

```elixir
@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`

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

Returns coverage is started

> #### Note {: .info}
>
> 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` (`t:boolean/0`) - Whether anonymous scripts generated by the page should be reported. Defaults to `false`.
* `:reset_on_navigation` (`t: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!`

```elixir
@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`

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

Returns the array of coverage reports for all stylesheets

> #### Note {: .info}
>
> CSS Coverage doesn't include dynamically injected style tags without sourceURLs.

*Since Playwright 1.11.*

# `stop_css_coverage!`

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

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

# `stop_js_coverage`

```elixir
@spec stop_js_coverage(t()) ::
  {:ok,
   [
     %{
       :url =&gt; String.t(),
       :scriptId =&gt; String.t(),
       optional(:source) =&gt; 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 {: .info}
>
> JavaScript Coverage doesn't include anonymous scripts by default. However, scripts with sourceURLs are
> reported.

*Since Playwright 1.11.*

# `stop_js_coverage!`

```elixir
@spec stop_js_coverage!(t()) :: [
  %{
    :url =&gt; String.t(),
    :scriptId =&gt; String.t(),
    optional(:source) =&gt; 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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
