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
pub use crate::imp::playwright::DeviceDescriptor;
use crate::{
api::{browser_type::BrowserType, selectors::Selectors},
imp::{core::*, playwright::Playwright as Impl, prelude::*},
Error
};
use std::{io, process::Command};
pub struct Playwright {
driver: Driver,
_conn: Connection,
inner: Weak<Impl>
}
fn run(driver: &Driver, args: &'static [&'static str]) -> io::Result<()> {
let status = Command::new(driver.executable()).args(args).status()?;
if !status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Exit with {}", status)
));
}
Ok(())
}
impl Playwright {
pub async fn initialize() -> Result<Playwright, Error> {
let driver = Driver::install()?;
Self::with_driver(driver).await
}
pub async fn with_driver(driver: Driver) -> Result<Playwright, Error> {
let conn = Connection::run(&driver.executable())?;
let p = Impl::wait_initial_object(&conn).await?;
Ok(Self {
driver,
_conn: conn,
inner: p
})
}
pub fn prepare(&self) -> io::Result<()> { run(&self.driver, &["install"]) }
pub fn install_chromium(&self) -> io::Result<()> { run(&self.driver, &["install", "chromium"]) }
pub fn install_firefox(&self) -> io::Result<()> { run(&self.driver, &["install", "firefox"]) }
pub fn install_webkit(&self) -> io::Result<()> { run(&self.driver, &["install", "webkit"]) }
pub fn chromium(&self) -> BrowserType {
let inner = weak_and_then(&self.inner, |rc| rc.chromium());
BrowserType::new(inner)
}
pub fn firefox(&self) -> BrowserType {
let inner = weak_and_then(&self.inner, |rc| rc.firefox());
BrowserType::new(inner)
}
pub fn webkit(&self) -> BrowserType {
let inner = weak_and_then(&self.inner, |rc| rc.webkit());
BrowserType::new(inner)
}
pub fn driver(&mut self) -> &mut Driver { &mut self.driver }
pub fn selectors(&self) -> Selectors {
let inner = weak_and_then(&self.inner, |rc| rc.selectors());
Selectors::new(inner)
}
pub fn devices(&self) -> Vec<DeviceDescriptor> {
upgrade(&self.inner)
.map(|x| x.devices().to_vec())
.unwrap_or_default()
}
}