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
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};

/// Entry point
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 {
    /// Installs playwright driver to "$CACHE_DIR/.ms-playwright/playwright-rust/driver"
    pub async fn initialize() -> Result<Playwright, Error> {
        let driver = Driver::install()?;
        Self::with_driver(driver).await
    }

    /// Constructs from installed playwright driver
    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
        })
    }

    /// Runs $ playwright install
    pub fn prepare(&self) -> io::Result<()> { run(&self.driver, &["install"]) }

    /// Runs $ playwright install chromium
    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"]) }

    /// Launcher
    pub fn chromium(&self) -> BrowserType {
        let inner = weak_and_then(&self.inner, |rc| rc.chromium());
        BrowserType::new(inner)
    }

    /// Launcher
    pub fn firefox(&self) -> BrowserType {
        let inner = weak_and_then(&self.inner, |rc| rc.firefox());
        BrowserType::new(inner)
    }

    /// Launcher
    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)
    }

    /// Returns a dictionary of devices to be used with [`method: Browser.newContext`] or [`method: Browser.newPage`].
    ///
    /// ```js
    /// const { webkit, devices } = require('playwright');
    /// const iPhone = devices['iPhone 6'];
    ///
    /// (async () => {
    ///  const browser = await webkit.launch();
    ///  const context = await browser.newContext({
    ///    ...iPhone
    ///  });
    ///  const page = await context.newPage();
    ///  await page.goto('http://example.com');
    ///  // other actions...
    ///  await browser.close();
    /// })();
    /// ```
    pub fn devices(&self) -> Vec<DeviceDescriptor> {
        upgrade(&self.inner)
            .map(|x| x.devices().to_vec())
            .unwrap_or_default()
    }

    pub fn device(&self, name: &str) -> Option<DeviceDescriptor> {
        let inner = self.inner.upgrade()?;
        let device = inner.device(name)?;
        Some(device.to_owned())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    crate::runtime_test!(failure_status_code, {
        let mut p = Playwright::initialize().await.unwrap();
        let err = run(p.driver(), &["nonExistentArg"]);
        assert!(err.is_err());
        if let Some(e) = err.err() {
            assert_eq!(e.kind(), io::ErrorKind::Other);
        }
    });
}