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
// **************************************************
// *  Author: Iceyee                                *
// *  Mail: iceyee.studio@qq.com                    *
// *  Git: https://github.com/iceyee                *
// **************************************************
//

//! - @see [tokio](../tokio/index.html)
//! - @see [iceyee_time](../iceyee_time/index.html)
//! - @see [thirtyfour](../thirtyfour/index.html)

// Use.

use cookie::SameSite;
use thirtyfour::error::WebDriverError;
use thirtyfour::error::WebDriverResult;
use thirtyfour::By;
use thirtyfour::ChromeCapabilities;
use thirtyfour::Cookie;
use thirtyfour::WebDriver;
use thirtyfour::WebElement;
use tokio::io::AsyncWriteExt;

// Enum.

// Trait.

// Struct.

// Function.

pub async fn chrome(driver_url: Option<&str>, headless: bool) -> WebDriverResult<WebDriver> {
    let mut options: ChromeCapabilities = ChromeCapabilities::new();
    options.set_ignore_certificate_errors()?;
    options.add_chrome_arg("--no-sandbox")?;
    if headless {
        options.set_headless()?;
    }
    println!("打开浏览器.");
    println!("{options:?}");
    let driver: WebDriver =
        WebDriver::new(driver_url.unwrap_or("http://localhost:9515"), options).await?;
    return Ok(driver);
}

pub async fn wait_url(driver: &WebDriver, url: &str, equal: bool) -> WebDriverResult<()> {
    println!("wait_url '{url}'.");
    let mut stdout = tokio::io::stdout();
    for x in 0..60 {
        stdout
            .write_all(b"\r")
            .await
            .expect("iceyee_webdriver/lib.rs 081");
        stdout
            .write_all(x.to_string().as_bytes())
            .await
            .expect("iceyee_webdriver/lib.rs 049");
        stdout.flush().await.expect("iceyee_webdriver/lib.rs 177");
        if (driver.current_url().await?.as_str() == url && equal)
            || (driver.current_url().await?.as_str() != url && !equal)
        {
            stdout
                .write_all(b"\r")
                .await
                .expect("iceyee_webdriver/lib.rs 081");
            stdout.flush().await.expect("iceyee_webdriver/lib.rs 713");
            iceyee_time::sleep(1_000).await;
            return Ok(());
        }
        iceyee_time::sleep(1_000).await;
    }
    return Err(WebDriverError::Timeout("".to_string()));
}

pub async fn wait_ready(driver: &WebDriver) -> WebDriverResult<()> {
    println!("wait_ready.");
    let mut stdout = tokio::io::stdout();
    for x in 0..60 {
        stdout
            .write_all(b"\r")
            .await
            .expect("iceyee_webdriver/lib.rs 081");
        stdout
            .write_all(x.to_string().as_bytes())
            .await
            .expect("iceyee_webdriver/lib.rs 049");
        stdout.flush().await.expect("iceyee_webdriver/lib.rs 177");
        if driver.status().await?.ready {
            stdout
                .write_all(b"\r")
                .await
                .expect("iceyee_webdriver/lib.rs 081");
            stdout.flush().await.expect("iceyee_webdriver/lib.rs 713");
            iceyee_time::sleep(1_000).await;
            return Ok(());
        }
        iceyee_time::sleep(1_000).await;
    }
    return Err(WebDriverError::Timeout("".to_string()));
}

pub async fn wait_element(driver: &WebDriver, css: &str, number: usize) -> WebDriverResult<()> {
    println!("wait_element '{css}' {number}");
    let mut stdout = tokio::io::stdout();
    for x in 0..60 {
        stdout
            .write_all(b"\r")
            .await
            .expect("iceyee_webdriver/lib.rs 081");
        stdout
            .write_all(x.to_string().as_bytes())
            .await
            .expect("iceyee_webdriver/lib.rs 049");
        stdout.flush().await.expect("iceyee_webdriver/lib.rs 177");
        if number <= driver.find_all(By::Css(css)).await?.len() {
            stdout
                .write_all(b"\r")
                .await
                .expect("iceyee_webdriver/lib.rs 081");
            stdout.flush().await.expect("iceyee_webdriver/lib.rs 713");
            return Ok(());
        }
        iceyee_time::sleep(1_000).await;
    }
    return Err(WebDriverError::Timeout("".to_string()));
}

pub async fn get_element(
    driver: &WebDriver,
    css: &str,
    index: usize,
) -> WebDriverResult<WebElement> {
    return Ok(driver.find_all(By::Css(css)).await?.remove(index));
}

pub async fn add_cookie(
    driver: &WebDriver,
    key: &str,
    value: &str,
    domain: &str,
) -> WebDriverResult<()> {
    let mut cookie = Cookie::new(key.to_string(), value.to_string());
    cookie.set_domain(domain.to_string());
    cookie.set_path("/");
    cookie.set_same_site(Some(SameSite::None));
    return driver.add_cookie(cookie.clone()).await;
}

pub async fn set_cookie(driver: &WebDriver, cookie: &str, domain: &str) -> WebDriverResult<()> {
    driver.delete_all_cookies().await?;
    for x in cookie.split(";") {
        let mut y = x.split("=");
        if let Some(key) = y.next() {
            let key = key.trim().to_string();
            if let Some(value) = y.next() {
                let value = value.trim().to_string();
                let mut cookie = Cookie::new(key, value);
                cookie.set_domain(domain.to_string());
                cookie.set_path("/");
                cookie.set_same_site(Some(SameSite::None));
                driver.add_cookie(cookie.clone()).await?;
            }
        }
    }
    return Ok(());
}

pub async fn get_cookie(driver: &WebDriver) -> WebDriverResult<(String, String)> {
    let mut output_1: String = String::new();
    let mut output_2: String = String::new();
    for cookie in driver.get_all_cookies().await? {
        output_1.push_str(format!("{}={}; ", cookie.name(), cookie.value()).as_str());
        output_2.push_str(
            format!(
                "\r\ndocument.cookie='{}={}; path=/;' ",
                cookie.name(),
                cookie.value()
            )
            .as_str(),
        );
    }
    return Ok((output_1, output_2));
}
//
// pub async fn wait_ready(driver: &WebDriver) -> WebDriverResult<()> {
//     return Ok(());
// }
//
// pub async fn wait_ready(driver: &WebDriver) -> WebDriverResult<()> {
//     return Ok(());
// }
//
// pub async fn wait_ready(driver: &WebDriver) -> WebDriverResult<()> {
//     return Ok(());
// }