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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
pub use crate::imp::browser_type::{RecordHar, RecordVideo};
use crate::{
api::{browser::Browser, browser_context::BrowserContext, playwright::DeviceDescriptor},
imp::{
browser_type::{
BrowserType as Impl, ConnectOverCdpArgs, LaunchArgs, LaunchPersistentContextArgs
},
core::*,
prelude::*,
utils::{
BrowserChannel, ColorScheme, Geolocation, HttpCredentials, ProxySettings, Viewport
}
},
Error
};
#[derive(Debug, Clone)]
pub struct BrowserType {
inner: Weak<Impl>
}
impl BrowserType {
pub(crate) fn new(inner: Weak<Impl>) -> Self { Self { inner } }
/// Returns browser name. For example: `'chromium'`, `'webkit'` or `'firefox'`.
/// # Errors
/// Returns error only if this function is called after object is disposed.
pub fn name(&self) -> Result<String, Error> { Ok(upgrade(&self.inner)?.name().into()) }
/// A path where Playwright expects to find a bundled browser executable.
/// # Errors
/// Returns error only if this function is called after object is disposed.
pub fn executable(&self) -> Result<PathBuf, Error> {
Ok(upgrade(&self.inner)?.executable().into())
}
/// launch [`Browser`]
/// Returns the browser instance.
///
/// You can use `ignoreDefaultArgs` to filter out `--mute-audio` from default arguments:
///
/// ```js
/// const browser = await chromium.launch({ // Or 'firefox' or 'webkit'.
/// ignoreDefaultArgs: ['--mute-audio']
/// });
/// ```
///
/// > **Chromium-only** Playwright can also be used to control the Google Chrome or Microsoft Edge browsers, but it works
/// best with the version of Chromium it is bundled with. There is no guarantee it will work with any other version. Use
/// `executablePath` option with extreme caution.
/// >
/// > If Google Chrome (rather than Chromium) is preferred, a
/// [Chrome Canary](https://www.google.com/chrome/browser/canary.html) or
/// [Dev Channel](https://www.chromium.org/getting-involved/dev-channel) build is suggested.
/// >
/// > Stock browsers like Google Chrome and Microsoft Edge are suitable for tests that require proprietary media codecs for
/// video playback. See
/// [this article](https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/) for other
/// differences between Chromium and Chrome.
/// [This article](https://chromium.googlesource.com/chromium/src/+/lkgr/docs/chromium_browser_vs_google_chrome.md)
/// describes some differences for Linux users.
pub fn launcher(&self) -> Launcher<'_, '_, '_> { Launcher::new(self.inner.clone()) }
/// launch_persistent_context [`BrowserContext`]
/// Returns the persistent browser context instance.
///
/// Launches browser that uses persistent storage located at `userDataDir` and returns the only context. Closing this
/// context will automatically close the browser.
/// user_data_dir: Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for
/// [Chromium](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md#introduction) and
/// [Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#User_Profile). Note that Chromium's user
/// data directory is the **parent** directory of the "Profile Path" seen at `chrome://version`.
pub fn persistent_context_launcher<'a>(
&self,
user_data_dir: &'a Path
) -> PersistentContextLauncher<'a, '_, '_, '_, '_, '_, '_, '_, '_, '_, '_> {
PersistentContextLauncher::new(self.inner.clone(), user_data_dir)
}
/// This methods attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.
///
/// The default browser context is accessible via [`method: Browser.contexts`].
///
/// > NOTE: Connecting over the Chrome DevTools Protocol is only supported for Chromium-based browsers.
/// A CDP websocket endpoint or http url to connect to. For example `http://localhost:9222/` or
/// `ws://127.0.0.1:9222/devtools/browser/387adf4c-243f-4051-a181-46798f4a46f4`.
pub fn connect_over_cdp_builder<'a>(&self, endpoint_url: &'a str) -> ConnectOverCdpBuilder<'a> {
ConnectOverCdpBuilder::new(self.inner.clone(), endpoint_url)
}
// connect
// launch_server
}
/// [`BrowserType::launcher`]
pub struct Launcher<'a, 'b, 'c> {
inner: Weak<Impl>,
args: LaunchArgs<'a, 'b, 'c>
}
impl<'a, 'b, 'c> Launcher<'a, 'b, 'c> {
pub async fn launch(self) -> Result<Browser, Arc<Error>> {
let Self { inner, args } = self;
let r = upgrade(&inner)?.launch(args).await?;
Ok(Browser::new(r))
}
fn new(inner: Weak<Impl>) -> Self {
Launcher {
inner,
args: LaunchArgs::default()
}
}
setter! {
/// Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is
/// resolved relative to the current working directory. Note that Playwright only works with the bundled Chromium, Firefox
/// or WebKit, use at your own risk.
executable: Option<&'a Path>,
/// Additional arguments to pass to the browser instance. The list of Chromium flags can be found
/// [here](http://peter.sh/experiments/chromium-command-line-switches/).
args: Option<&'b [String]>,
/// If `true`, Playwright does not pass its own configurations args and only uses the ones from `args`. Dangerous option;
/// use with care. Defaults to `false`.
ignore_all_default_args: Option<bool>,
/// Close the browser process on Ctrl-C. Defaults to `true`.
handle_sigint: Option<bool>,
/// Close the browser process on SIGTERM. Defaults to `true`.
handle_sigterm: Option<bool>,
/// Close the browser process on SIGHUP. Defaults to `true`.
handle_sighup: Option<bool>,
/// Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to
/// disable timeout.
timeout: Option<f64>,
/// **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless`
/// option will be set `false`.
devtools: Option<bool>,
/// Network proxy settings.
proxy: Option<ProxySettings>,
/// If specified, accepted downloads are downloaded into this directory. Otherwise, temporary directory is created and is
/// deleted when browser is closed.
downloads: Option<&'c Path>,
/// Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
slowmo: Option<f64>,
/// Specify environment variables that will be visible to the browser. Defaults to `process.env`.
env: Option<Map<String, Value>>,
/// Whether to run browser in headless mode. More details for
/// [Chromium](https://developers.google.com/web/updates/2017/04/headless-chrome) and
/// [Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode). Defaults to `true` unless the
/// `devtools` option is `true`.
headless: Option<bool>,
/// Enable Chromium sandboxing. Defaults to `false`.
chromium_sandbox: Option<bool>,
/// Firefox user preferences. Learn more about the Firefox user preferences at
/// [`about:config`](https://support.mozilla.org/en-US/kb/about-config-editor-firefox).
firefox_user_prefs: Option<Map<String, Value>>,
channel: Option<BrowserChannel>
}
//#[doc = "If `true`, Playwright does not pass its own configurations args and only uses the ones from `args`. If an array is\ngiven, then filters out the given default arguments. Dangerous option; use with care. Defaults to `false`."]
// ignore_default_args: Option<NotImplementedYet>,
//#[doc = "Logger sink for Playwright logging."]
// logger: Option<Logger>,
}
/// [`BrowserType::persistent_context_launcher`]
///
/// Has launch args and context args
pub struct PersistentContextLauncher<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k> {
inner: Weak<Impl>,
args: LaunchPersistentContextArgs<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k>
}
impl<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k>
PersistentContextLauncher<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k>
{
pub async fn launch(self) -> Result<BrowserContext, Arc<Error>> {
let Self { inner, args } = self;
let r = upgrade(&inner)?.launch_persistent_context(args).await?;
Ok(BrowserContext::new(r))
}
fn new(inner: Weak<Impl>, user_data_dir: &'a Path) -> Self {
Self {
inner,
args: LaunchPersistentContextArgs::new(user_data_dir)
}
}
pub fn set_device(self, device: &'e DeviceDescriptor) -> Self {
DeviceDescriptor::set_persistent_context(device, self)
}
setter! {
/// Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is
/// resolved relative to the current working directory. **BEWARE**: Playwright is only guaranteed to work with the bundled
/// Chromium, Firefox or WebKit, use at your own risk.
executable: Option<&'b Path>,
/// Additional arguments to pass to the browser instance. The list of Chromium flags can be found
/// [here](http://peter.sh/experiments/chromium-command-line-switches/).
args: Option<&'c [String]>,
/// If `true`, Playwright does not pass its own configurations args and only uses the ones from `args`. Dangerous option;
/// use with care. Defaults to `false`.
ignore_all_default_args: Option<bool>,
/// Close the browser process on SIGHUP. Defaults to `true`.
handle_sighup: Option<bool>,
/// Close the browser process on Ctrl-C. Defaults to `true`.
handle_sigint: Option<bool>,
/// Close the browser process on SIGTERM. Defaults to `true`.
handle_sigterm: Option<bool>,
/// Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to
/// disable timeout.
timeout: Option<f64>,
/// Specify environment variables that will be visible to the browser. Defaults to `process.env`.
env: Option<Map<String, Value>>,
/// Whether to run browser in headless mode. More details for
/// [Chromium](https://developers.google.com/web/updates/2017/04/headless-chrome) and
/// [Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode). Defaults to `true` unless the
/// `devtools` option is `true`.
headless: Option<bool>,
/// **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless`
/// option will be set `false`.
devtools: Option<bool>,
/// Network proxy settings.
proxy: Option<ProxySettings>,
/// If specified, accepted downloads are downloaded into this directory. Otherwise, temporary directory is created and is
/// deleted when browser is closed.
downloads: Option<&'d Path>,
/// Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
/// Defaults to 0.
slowmo: Option<f64>,
/// Emulates consistent viewport for each page. Defaults to an 1280x720 viewport. `null` disables the default viewport.
viewport: Option<Option<Viewport>>,
/// Does not enforce fixed viewport, allows resizing window in the headed mode.
no_viewport: Option<bool>,
/// Emulates consistent window screen size available inside web page via `window.screen`. Is only used when the `viewport`
/// is set.
screen: Option<Viewport>,
/// Whether to ignore HTTPS errors during navigation. Defaults to `false`.
ignore_https_errors: Option<bool>,
/// Whether or not to enable JavaScript in the context. Defaults to `true`.
js_enabled: Option<bool>,
/// Toggles bypassing page's Content-Security-Policy.
bypass_csp: Option<bool>,
/// Specific user agent to use in this context.
user_agent: Option<&'e str>,
/// Specify user locale, for example `en-GB`, `de-DE`, etc. Locale will affect `navigator.language` value, `Accept-Language`
/// request header value as well as number and date formatting rules.
locale: Option<&'f str>,
/// Changes the timezone of the context. See
/// [ICU's metaZones.txt](https://cs.chromium.org/chromium/src/third_party/icu/source/data/misc/metaZones.txt?rcl=faee8bc70570192d82d2978a71e2a615788597d1)
/// for a list of supported timezone IDs.
timezone_id: Option<&'g str>,
geolocation: Option<Geolocation>,
/// A list of permissions to grant to all pages in this context. See [`method: BrowserContext.grantPermissions`] for more
/// details.
permissions: Option<&'h [String]>,
/// An object containing additional HTTP headers to be sent with every request. All header values must be strings.
extra_http_headers: Option<HashMap<String, String>>,
/// Whether to emulate network being offline. Defaults to `false`.
offline: Option<bool>,
/// Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
http_credentials: Option<&'i HttpCredentials>,
/// Specify device scale factor (can be thought of as dpr). Defaults to `1`.
device_scale_factor: Option<f64>,
/// Whether the `meta viewport` tag is taken into account and touch events are enabled. Defaults to `false`. Not supported
/// in Firefox.
is_mobile: Option<bool>,
/// Specifies if viewport supports touch events. Defaults to false.
has_touch: Option<bool>,
/// Emulates `'prefers-colors-scheme'` media feature, supported values are `'light'`, `'dark'`, `'no-preference'`. See
/// [`method: Page.emulateMedia`] for more details. Defaults to `'light'`.
color_scheme: Option<ColorScheme>,
/// Whether to automatically download all the attachments. Defaults to `false` where all the downloads are canceled.
accept_downloads: Option<bool>,
/// Enable Chromium sandboxing. Defaults to `true`.
chromium_sandbox: Option<bool>,
/// Enables video recording for all pages into `recordVideo.dir` directory. If not specified videos are not recorded. Make
/// sure to await [`method: BrowserContext.close`] for videos to be saved.
record_video: Option<RecordVideo<'j>>,
/// Enables [HAR](http://www.softwareishard.com/blog/har-12-spec) recording for all pages into `recordHar.path` file. If not
/// specified, the HAR is not recorded. Make sure to await [`method: BrowserContext.close`] for the HAR to be saved.
record_har: Option<RecordHar<'k>>,
channel: Option<BrowserChannel>
}
//#[doc = "If `true`, Playwright does not pass its own configurations args and only uses the ones from `args`. Dangerous option;\nuse with care."]
// ignore_default_args: Option<Vec<String>>,
//#[doc = "Logger sink for Playwright logging."] logger: Option<Logger>,
//#[doc = "Optional setting to control whether to omit request content from the HAR. Defaults to `false`."]
// record_har_omit_content: Option<bool>,
//#[doc = "Enables [HAR](http://www.softwareishard.com/blog/har-12-spec) recording for all pages into the specified HAR file on the\nfilesystem. If not specified, the HAR is not recorded. Make sure to call [`method: BrowserContext.close`] for the HAR to\nbe saved."]
// record_har_path: Option<path>,
//#[doc = "Enables video recording for all pages into the specified directory. If not specified videos are not recorded. Make sure\nto call [`method: BrowserContext.close`] for videos to be saved."]
// record_video_dir: Option<path>,
//#[doc = "Dimensions of the recorded videos. If not specified the size will be equal to `viewport` scaled down to fit into\n800x800. If `viewport` is not configured explicitly the video size defaults to 800x450. Actual picture of each page will\nbe scaled down if necessary to fit the specified size."]
// record_video_size: Option<NotImplementedYet>,
//#[doc = "**DEPRECATED** Use `recordVideo` instead."] video_size: Option<NotImplementedYet>,
//#[doc = "**DEPRECATED** Use `recordVideo` instead."] videos_path: Option<path>,
}
pub struct ConnectOverCdpBuilder<'a> {
inner: Weak<Impl>,
args: ConnectOverCdpArgs<'a>
}
impl<'a> ConnectOverCdpBuilder<'a> {
pub async fn connect_over_cdp(self) -> ArcResult<Browser> {
let Self { inner, args } = self;
let r = upgrade(&inner)?.connect_over_cdp(args).await?;
Ok(Browser::new(r))
}
fn new(inner: Weak<Impl>, endpoint_url: &'a str) -> Self {
Self {
inner,
args: ConnectOverCdpArgs::new(endpoint_url)
}
}
setter! {
/// Additional HTTP headers to be sent with web socket connect request. Optional.
headers: Option<HashMap<String, String>>,
/// Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to
/// disable timeout.
timeout: Option<f64>,
/// Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
/// Defaults to 0.
slowmo: Option<f64>
}
}