forked from mattsse/chromiumoxide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.rs
83 lines (70 loc) · 2.43 KB
/
evaluate.rs
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
use futures::StreamExt;
use chromiumoxide::browser::{Browser, BrowserConfig};
use chromiumoxide_cdp::cdp::js_protocol::runtime::{
CallArgument, CallFunctionOnParams, EvaluateParams,
};
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (mut browser, mut handler) = Browser::launch(BrowserConfig::builder().build()?).await?;
let handle = async_std::task::spawn(async move {
while let Some(h) = handler.next().await {
match h {
Ok(_) => continue,
Err(_) => break,
}
}
});
let page = browser.new_page("about:blank").await?;
let sum: usize = page.evaluate("1 + 2").await?.into_value()?;
assert_eq!(sum, 3);
println!("1 + 2 = {sum}");
let mult: usize = page
.evaluate("() => { return 21 * 2; }")
.await?
.into_value()?;
assert_eq!(mult, 42);
println!("21 * 2 = {mult}");
let promise_div: usize = page
.evaluate("() => Promise.resolve(100 / 25)")
.await?
.into_value()?;
assert_eq!(promise_div, 4);
println!("100 / 25 = {promise_div}");
let call = CallFunctionOnParams::builder()
.function_declaration("(a,b) => { return a + b;}")
.argument(CallArgument::builder().value(serde_json::json!(1)).build())
.argument(CallArgument::builder().value(serde_json::json!(2)).build())
.build()
.unwrap();
let sum: usize = page.evaluate_function(call).await?.into_value()?;
assert_eq!(sum, 3);
println!("1 + 2 = {sum}");
let sum: usize = page
.evaluate_expression("((a,b) => {return a + b;})(1,2)")
.await?
.into_value()?;
assert_eq!(sum, 3);
println!("1 + 2 = {sum}");
let val: usize = page
.evaluate_function("async function() {return 42;}")
.await?
.into_value()?;
assert_eq!(val, 42);
println!("42 = {val}");
let eval = EvaluateParams::builder().expression("() => {return 42;}");
// this will fail because the `EvaluationResult` returned by the browser will be
// of type `Function`
assert!(page
.evaluate(eval.clone().build().unwrap())
.await?
.into_value::<usize>()
.is_err());
let val: usize = page
.evaluate(eval.eval_as_function_fallback(true).build().unwrap())
.await?
.into_value()?;
assert_eq!(val, 42);
browser.close().await?;
handle.await;
Ok(())
}