use std::ops::Deref;
use crate::Result;
use crate::browser::{Browser, Tab};
use crate::launcher::BrowserOptions;
pub struct Page {
browser: Browser,
tab: Tab,
}
impl Page {
pub async fn new() -> Result<Self> {
Self::from_browser(Browser::launch_default().await?).await
}
pub async fn headless() -> Result<Self> {
Self::with(BrowserOptions::new().headless(true)).await
}
pub async fn with(opts: BrowserOptions) -> Result<Self> {
Self::from_browser(Browser::launch(opts).await?).await
}
pub async fn connect(ws_url: &str) -> Result<Self> {
Self::from_browser(Browser::connect(ws_url).await?).await
}
pub async fn connect_with(ws_url: &str, opts: BrowserOptions) -> Result<Self> {
Self::from_browser(Browser::connect_with(ws_url, opts).await?).await
}
pub async fn from_browser(browser: Browser) -> Result<Self> {
let tab = browser.latest_tab().await?;
Ok(Self { browser, tab })
}
pub fn browser(&self) -> &Browser {
&self.browser
}
pub fn tab(&self) -> &Tab {
&self.tab
}
pub async fn new_tab(&self, url: Option<&str>) -> Result<Tab> {
self.browser.new_tab(url).await
}
pub async fn quit(&self) -> Result<()> {
self.browser.quit().await
}
}
impl Deref for Page {
type Target = Tab;
fn deref(&self) -> &Tab {
&self.tab
}
}