Tab

Struct Tab 

Source
pub struct Tab { /* private fields */ }

Implementations§

Source§

impl Tab

Source

pub fn get_port(&self) -> u16

Returns chromederiver server port

Source

pub fn get_id(&self) -> &str

Returns tab id

Source

pub async fn active(&self) -> Result<()>

Do tab active

Source

pub async fn open<S>(&mut self, url: S) -> Result<()>
where S: Into<String>,

Open URL-address

Source

pub async fn inject<D: DeserializeOwned>(&self, script: &str) -> Result<D>

Inject JavaScript to window tab

Examples found in repository?
examples/test_run.rs (lines 29-35)
6async fn main() -> Result<()> {
7    let free_port = std::net::TcpListener::bind("127.0.0.1:0")?.local_addr()?.port();
8    let chrome_path = path!("bin/chromedriver/chromedriver.exe");
9    let session_path = path!("%/ChromeDriver/Profile");
10
11    let session = Session::run(
12        free_port, 
13        chrome_path,
14        Some(session_path),
15        false   // headless mode
16    ).await?;
17    println!("[INFO]: session launched on port [{free_port}]");
18
19    // Tab 1: Normal page (fast close test)
20    let tab1 = session.open("https://example.com").await?;
21    let tab1 = tab1.lock().await;
22    println!("[INFO]: tab1: form page loaded");
23
24    sleep(Duration::from_secs(2)).await;
25
26    // Tab 2: Page with beforeunload handler (blocks close)
27    let tab2 = session.open("https://html-online.com/editor/").await?;
28    let tab2 = tab2.lock().await;
29    tab2.inject::<()>(r#"
30        window.addEventListener('beforeunload', function(e) {
31            e.preventDefault();
32            e.returnValue = 'Are you sure?';
33            return 'Are you sure?';
34        });
35    "#).await?;
36    println!("[INFO]: tab2: beforeunload hook installed");
37
38    sleep(Duration::from_secs(2)).await;
39
40    // Tab 3: Alert + Confirm scenario (multiple retries)
41    let tab3 = session.open("https://httpbin.org/html").await?;
42    let tab3 = tab3.lock().await;
43    tab3.inject::<()>(r#"
44        // Delayed alert 3s after close attempt
45        setTimeout(() => {
46            alert('Late alert!');
47        }, 3000);
48        
49        // Confirm after 1s
50        setTimeout(() => {
51            if (confirm('Close tab?')) {
52                console.log('User confirmed');
53            }
54        }, 1000);
55    "#).await?;
56    println!("[INFO]: tab3: delayed alert + confirm injected");
57
58    sleep(Duration::from_secs(2)).await;
59
60    println!("\n[TEST]: Closing tab1 (should be ✅ fast)");
61    tab1.close().await?;
62    println!("[✅] tab1 closed successfully\n");
63
64    println!("[TEST]: Closing tab2 (should trigger ⚠️ beforeunload retry)");
65    tab2.close().await?;
66    println!("[✅] tab2 closed (retry worked)\n");
67
68    println!("[TEST]: Closing tab3 (should trigger ⚠️ alert/confirm retries)");
69    tab3.close().await?;
70    println!("[✅] tab3 closed (multiple retries succeeded)\n");
71
72    // Verify remaining handles
73    let handles = session.get_tabs_ids().await?;
74    println!("[INFO]: Remaining tabs: {}", handles.len());
75
76    sleep(Duration::from_secs(1)).await;
77    
78    session.close().await?;
79    println!("[INFO]: Session closed");
80
81    Ok(())
82}
Source

pub async fn close(&self) -> Result<()>

Close window tab

Examples found in repository?
examples/test_run.rs (line 61)
6async fn main() -> Result<()> {
7    let free_port = std::net::TcpListener::bind("127.0.0.1:0")?.local_addr()?.port();
8    let chrome_path = path!("bin/chromedriver/chromedriver.exe");
9    let session_path = path!("%/ChromeDriver/Profile");
10
11    let session = Session::run(
12        free_port, 
13        chrome_path,
14        Some(session_path),
15        false   // headless mode
16    ).await?;
17    println!("[INFO]: session launched on port [{free_port}]");
18
19    // Tab 1: Normal page (fast close test)
20    let tab1 = session.open("https://example.com").await?;
21    let tab1 = tab1.lock().await;
22    println!("[INFO]: tab1: form page loaded");
23
24    sleep(Duration::from_secs(2)).await;
25
26    // Tab 2: Page with beforeunload handler (blocks close)
27    let tab2 = session.open("https://html-online.com/editor/").await?;
28    let tab2 = tab2.lock().await;
29    tab2.inject::<()>(r#"
30        window.addEventListener('beforeunload', function(e) {
31            e.preventDefault();
32            e.returnValue = 'Are you sure?';
33            return 'Are you sure?';
34        });
35    "#).await?;
36    println!("[INFO]: tab2: beforeunload hook installed");
37
38    sleep(Duration::from_secs(2)).await;
39
40    // Tab 3: Alert + Confirm scenario (multiple retries)
41    let tab3 = session.open("https://httpbin.org/html").await?;
42    let tab3 = tab3.lock().await;
43    tab3.inject::<()>(r#"
44        // Delayed alert 3s after close attempt
45        setTimeout(() => {
46            alert('Late alert!');
47        }, 3000);
48        
49        // Confirm after 1s
50        setTimeout(() => {
51            if (confirm('Close tab?')) {
52                console.log('User confirmed');
53            }
54        }, 1000);
55    "#).await?;
56    println!("[INFO]: tab3: delayed alert + confirm injected");
57
58    sleep(Duration::from_secs(2)).await;
59
60    println!("\n[TEST]: Closing tab1 (should be ✅ fast)");
61    tab1.close().await?;
62    println!("[✅] tab1 closed successfully\n");
63
64    println!("[TEST]: Closing tab2 (should trigger ⚠️ beforeunload retry)");
65    tab2.close().await?;
66    println!("[✅] tab2 closed (retry worked)\n");
67
68    println!("[TEST]: Closing tab3 (should trigger ⚠️ alert/confirm retries)");
69    tab3.close().await?;
70    println!("[✅] tab3 closed (multiple retries succeeded)\n");
71
72    // Verify remaining handles
73    let handles = session.get_tabs_ids().await?;
74    println!("[INFO]: Remaining tabs: {}", handles.len());
75
76    sleep(Duration::from_secs(1)).await;
77    
78    session.close().await?;
79    println!("[INFO]: Session closed");
80
81    Ok(())
82}

Trait Implementations§

Source§

impl Clone for Tab

Source§

fn clone(&self) -> Tab

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Tab

§

impl !RefUnwindSafe for Tab

§

impl Send for Tab

§

impl Sync for Tab

§

impl Unpin for Tab

§

impl !UnwindSafe for Tab

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,