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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2026 Paul Adamson
// Licensed under the Apache License, Version 2.0
//
// Download protocol object
//
// Represents a file download triggered by the page.
// Downloads are dispatched via page.on('download') events.
use crate::error::Result;
use crate::server::channel_owner::ChannelOwner;
use serde_json::json;
use std::path::PathBuf;
use std::sync::Arc;
/// Download represents a file download triggered by the page.
///
/// Downloads are dispatched via the page.on('download') event.
/// The download will be automatically deleted when the browser context closes.
///
/// NOTE: Unlike other protocol objects, Download is a wrapper around the Artifact
/// protocol object. The URL and suggested_filename come from the download event params,
/// while the actual file operations are delegated to the underlying Artifact.
///
/// See: <https://playwright.dev/docs/api/class-download>
#[derive(Clone)]
pub struct Download {
/// Reference to the underlying Artifact protocol object
artifact: Arc<dyn ChannelOwner>,
/// URL from download event params
url: String,
/// Suggested filename from download event params
suggested_filename: String,
/// Back-reference to the Page that triggered this download
page: crate::protocol::Page,
}
impl Download {
/// Creates a new Download from an Artifact and event params
///
/// This is NOT created via the object factory, but rather constructed
/// directly from the download event params which contain {url, suggestedFilename, artifact}.
///
/// # Arguments
///
/// * `artifact` - The Artifact protocol object (from event params)
/// * `url` - Download URL (from event params)
/// * `suggested_filename` - Suggested filename (from event params)
/// * `page` - The Page that triggered this download
pub(crate) fn from_artifact(
artifact: Arc<dyn ChannelOwner>,
url: String,
suggested_filename: String,
page: crate::protocol::Page,
) -> Self {
Self {
artifact,
url,
suggested_filename,
page,
}
}
/// Returns the [`Page`](crate::protocol::Page) that triggered this download.
///
/// See: <https://playwright.dev/docs/api/class-download#download-page>
pub fn page(&self) -> &crate::protocol::Page {
&self.page
}
/// Returns the download URL.
///
/// See: <https://playwright.dev/docs/api/class-download#download-url>
pub fn url(&self) -> &str {
&self.url
}
/// Returns the suggested filename for the download.
///
/// This is typically the server-suggested filename from the Content-Disposition
/// header or the HTML download attribute.
///
/// See: <https://playwright.dev/docs/api/class-download#download-suggested-filename>
pub fn suggested_filename(&self) -> &str {
&self.suggested_filename
}
/// Returns the underlying Artifact's channel for protocol communication
fn channel(&self) -> &crate::server::channel::Channel {
self.artifact.channel()
}
/// Returns the path to the downloaded file after it completes.
///
/// This method waits for the download to finish if necessary.
/// Returns an error if the download fails or is canceled.
///
/// See: <https://playwright.dev/docs/api/class-download#download-path>
pub async fn path(&self) -> Result<Option<PathBuf>> {
#[derive(serde::Deserialize)]
struct PathResponse {
value: Option<String>,
}
let result: PathResponse = self.channel().send("path", json!({})).await?;
Ok(result.value.map(PathBuf::from))
}
/// Saves the download to the specified path.
///
/// This method can be safely called while the download is still in progress.
/// The file will be copied to the specified location after the download completes.
///
/// # Example
///
/// ```ignore
/// # use playwright_rs::protocol::Download;
/// # async fn example(download: Download) -> Result<(), Box<dyn std::error::Error>> {
/// download.save_as("/path/to/save/file.pdf").await?;
/// # Ok(())
/// # }
/// ```
///
/// See: <https://playwright.dev/docs/api/class-download#download-save-as>
pub async fn save_as(&self, path: impl AsRef<std::path::Path>) -> Result<()> {
let path_str = path
.as_ref()
.to_str()
.ok_or_else(|| crate::error::Error::InvalidArgument("Invalid path".to_string()))?;
self.channel()
.send_no_result("saveAs", json!({ "path": path_str }))
.await?;
Ok(())
}
/// Cancels the download.
///
/// After calling this method, `failure()` will return an error message.
///
/// See: <https://playwright.dev/docs/api/class-download#download-cancel>
pub async fn cancel(&self) -> Result<()> {
self.channel().send_no_result("cancel", json!({})).await?;
Ok(())
}
/// Deletes the downloaded file.
///
/// The download must be finished before calling this method.
///
/// See: <https://playwright.dev/docs/api/class-download#download-delete>
pub async fn delete(&self) -> Result<()> {
self.channel().send_no_result("delete", json!({})).await?;
Ok(())
}
/// Returns the download error message if it failed, otherwise None.
///
/// See: <https://playwright.dev/docs/api/class-download#download-failure>
pub async fn failure(&self) -> Result<Option<String>> {
#[derive(serde::Deserialize)]
struct FailureResponse {
error: Option<String>,
}
let result: FailureResponse = self.channel().send("failure", json!({})).await?;
Ok(result.error)
}
}
impl std::fmt::Debug for Download {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Download")
.field("url", &self.url())
.field("suggested_filename", &self.suggested_filename())
.finish()
}
}