1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Coverage — Chromium-only code coverage collection
//
// See: https://playwright.dev/docs/api/class-coverage
use crate::error::Result;
use crate::protocol::page::Page;
/// Options for `Coverage::start_js_coverage`.
///
/// See: <https://playwright.dev/docs/api/class-coverage#coverage-start-js-coverage>
#[derive(Debug, Default, Clone)]
pub struct StartJSCoverageOptions {
/// Whether to reset coverage on every navigation.
///
/// Defaults to `true`.
pub reset_on_navigation: Option<bool>,
/// Whether to report anonymous scripts generated by the page.
///
/// Defaults to `false`.
pub report_anonymous_scripts: Option<bool>,
}
/// Options for `Coverage::start_css_coverage`.
///
/// See: <https://playwright.dev/docs/api/class-coverage#coverage-start-css-coverage>
#[derive(Debug, Default, Clone)]
pub struct StartCSSCoverageOptions {
/// Whether to reset coverage on every navigation.
///
/// Defaults to `true`.
pub reset_on_navigation: Option<bool>,
}
/// A byte-offset range within a CSS stylesheet.
///
/// Used in [`CSSCoverageEntry`].
#[derive(Debug, Clone, serde::Deserialize)]
pub struct CoverageRange {
/// Start byte offset (inclusive).
pub start: usize,
/// End byte offset (exclusive).
pub end: usize,
}
/// A byte-offset range with hit count within a JavaScript function.
///
/// Used in [`JSFunctionCoverage`].
#[derive(Debug, Clone, serde::Deserialize)]
pub struct JSCoverageRange {
/// Start byte offset (inclusive).
#[serde(rename = "startOffset")]
pub start_offset: usize,
/// End byte offset (exclusive).
#[serde(rename = "endOffset")]
pub end_offset: usize,
/// Number of times this range was executed.
pub count: i64,
}
/// Per-function coverage data within a [`JSCoverageEntry`].
#[derive(Debug, Clone, serde::Deserialize)]
pub struct JSFunctionCoverage {
/// The function name (empty string for anonymous functions).
#[serde(rename = "functionName")]
pub function_name: String,
/// Whether this is block-level coverage (true) or function-level (false).
#[serde(rename = "isBlockCoverage")]
pub is_block_coverage: bool,
/// Covered byte-offset ranges within this function.
pub ranges: Vec<JSCoverageRange>,
}
/// A JavaScript coverage entry returned by `Coverage::stop_js_coverage`.
///
/// See: <https://playwright.dev/docs/api/class-coverage#coverage-stop-js-coverage>
#[derive(Debug, Clone, serde::Deserialize)]
pub struct JSCoverageEntry {
/// The URL of the script.
pub url: String,
/// The V8 script ID.
#[serde(rename = "scriptId")]
pub script_id: String,
/// The script source text.
pub source: Option<String>,
/// Per-function coverage data.
pub functions: Vec<JSFunctionCoverage>,
}
/// A CSS coverage entry returned by `Coverage::stop_css_coverage`.
///
/// See: <https://playwright.dev/docs/api/class-coverage#coverage-stop-css-coverage>
#[derive(Debug, Clone, serde::Deserialize)]
pub struct CSSCoverageEntry {
/// The URL of the stylesheet.
pub url: String,
/// The stylesheet source text.
pub text: Option<String>,
/// Byte-offset ranges of used CSS rules.
pub ranges: Vec<CoverageRange>,
}
/// Provides JavaScript and CSS code coverage collection (Chromium only).
///
/// Access via [`Page::coverage`].
///
/// Coverage collection is only supported in Chromium. Calling these methods on
/// Firefox or WebKit will return an error.
///
/// See: <https://playwright.dev/docs/api/class-coverage>
#[derive(Clone)]
pub struct Coverage {
page: Page,
}
impl Coverage {
pub(crate) fn new(page: Page) -> Self {
Self { page }
}
/// Starts collecting JavaScript coverage.
///
/// Must be called before navigating to the page(s) you want to measure.
///
/// # Errors
///
/// Returns error if coverage is already started, if the browser is not
/// Chromium, or if the RPC call fails.
///
/// See: <https://playwright.dev/docs/api/class-coverage#coverage-start-js-coverage>
pub async fn start_js_coverage(&self, options: Option<StartJSCoverageOptions>) -> Result<()> {
self.page.coverage_start_js(options).await
}
/// Stops JavaScript coverage collection and returns the entries.
///
/// # Errors
///
/// Returns error if coverage was not started or if the RPC call fails.
///
/// See: <https://playwright.dev/docs/api/class-coverage#coverage-stop-js-coverage>
pub async fn stop_js_coverage(&self) -> Result<Vec<JSCoverageEntry>> {
self.page.coverage_stop_js().await
}
/// Starts collecting CSS coverage.
///
/// # Errors
///
/// Returns error if coverage is already started, if the browser is not
/// Chromium, or if the RPC call fails.
///
/// See: <https://playwright.dev/docs/api/class-coverage#coverage-start-css-coverage>
pub async fn start_css_coverage(&self, options: Option<StartCSSCoverageOptions>) -> Result<()> {
self.page.coverage_start_css(options).await
}
/// Stops CSS coverage collection and returns the entries.
///
/// # Errors
///
/// Returns error if coverage was not started or if the RPC call fails.
///
/// See: <https://playwright.dev/docs/api/class-coverage#coverage-stop-css-coverage>
pub async fn stop_css_coverage(&self) -> Result<Vec<CSSCoverageEntry>> {
self.page.coverage_stop_css().await
}
}