forked from rust-headless-chrome/rust-headless-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.rs
987 lines (855 loc) · 31.6 KB
/
simple.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
#![allow(unused_variables)]
use std::sync::{Arc, Mutex};
use std::thread::sleep;
use std::time::{Duration, Instant};
use anyhow::Result;
use base64::Engine;
use headless_chrome::protocol::cdp::Browser::WindowState;
use headless_chrome::protocol::cdp::Fetch::events::RequestPausedEvent;
use headless_chrome::protocol::cdp::Fetch::{
FulfillRequest, HeaderEntry, RequestPattern, RequestStage,
};
use headless_chrome::protocol::cdp::Network::{Cookie, CookieParam};
use headless_chrome::protocol::cdp::Page::CaptureScreenshotFormatOption;
use headless_chrome::protocol::cdp::Runtime::{RemoteObjectSubtype, RemoteObjectType};
use headless_chrome::protocol::cdp::DOM::RGBA;
use headless_chrome::types::{Bounds, RemoteError};
use headless_chrome::LaunchOptionsBuilder;
use log::*;
use rand::prelude::*;
use headless_chrome::browser::tab::RequestPausedDecision;
use headless_chrome::browser::transport::{SessionId, Transport};
use headless_chrome::util::Wait;
use headless_chrome::{Browser, Tab};
use std::collections::HashMap;
pub mod logging;
pub mod server;
/// Launches a dumb server that unconditionally serves the given data as a
/// successful html response; launches a new browser and navigates to the
/// server.
///
/// Users must hold on to the server, which stops when dropped.
fn dumb_server(data: &'static str) -> (server::Server, Browser, Arc<Tab>) {
let server = server::Server::with_dumb_html(data);
let (browser, tab) = dumb_client(&server);
(server, browser, tab)
}
fn browser() -> Browser {
Browser::new(
LaunchOptionsBuilder::default()
.headless(true)
.build()
.unwrap(),
)
.unwrap()
}
fn dumb_client(server: &server::Server) -> (Browser, Arc<Tab>) {
let browser = browser();
let tab = browser.new_tab().unwrap();
tab.navigate_to(&format!("http://127.0.0.1:{}", server.port()))
.unwrap();
(browser, tab)
}
#[test]
fn simple() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
tab.wait_for_element("div#foobar")?;
Ok(())
}
// NOTE: we disable this test for Mac, because Travis doesn't support xvfb as a 'service'
#[cfg(target_os = "linux")]
#[test]
fn bounds_changed() -> Result<(), anyhow::Error> {
logging::enable_logging();
let browser = browser();
let tab = browser.new_tab().unwrap();
// New browser windows start in normal (windowed) state
let bounds = tab.get_bounds()?;
assert_eq!(bounds.state, WindowState::Normal);
// Return to normal window size, setting only the coordinates
tab.set_bounds(Bounds::Normal {
left: Some(5),
top: Some(5),
width: None,
height: None,
})?;
let new_bounds = tab.get_bounds()?;
assert_eq!(new_bounds.state, WindowState::Normal);
assert_eq!(new_bounds.left, 5);
assert_eq!(new_bounds.top, 5);
assert_eq!(new_bounds.width, bounds.width);
assert_eq!(new_bounds.height, bounds.height);
tab.set_bounds(Bounds::Normal {
left: None,
top: None,
width: Some(200.0),
height: Some(100.0),
})?;
let new_bounds = tab.get_bounds()?;
assert_eq!(new_bounds.state, WindowState::Normal);
assert_eq!(new_bounds.left, 5);
assert_eq!(new_bounds.top, 5);
assert_eq!(new_bounds.width, 200.0);
assert_eq!(new_bounds.height, 100.0);
Ok(())
}
#[test]
fn bounds_unchanged() -> Result<(), anyhow::Error> {
logging::enable_logging();
let browser = browser();
let tab = browser.new_tab().unwrap();
let bounds = tab.get_bounds()?;
// Minimizing a window does *not* change it's bounds
tab.set_bounds(Bounds::Minimized)?;
let min_bounds = tab.get_bounds()?;
assert_eq!(min_bounds.state, WindowState::Minimized);
assert_eq!(min_bounds.left, bounds.left);
assert_eq!(min_bounds.top, bounds.top);
assert_eq!(min_bounds.width, bounds.width);
assert_eq!(min_bounds.height, bounds.height);
// Maximizing a window does *not* change it's bounds
tab.set_bounds(Bounds::Maximized)?;
let max_bounds = tab.get_bounds()?;
assert_eq!(max_bounds.state, WindowState::Maximized);
assert_eq!(max_bounds.left, bounds.left);
assert_eq!(max_bounds.top, bounds.top);
assert!(max_bounds.width >= bounds.width);
assert!(max_bounds.height >= bounds.height);
// Setting a window to fullscreen does *not* change it's bounds
tab.set_bounds(Bounds::Fullscreen)?;
let fs_bounds = tab.get_bounds()?;
assert_eq!(fs_bounds.state, WindowState::Fullscreen);
assert_eq!(fs_bounds.left, bounds.left);
assert_eq!(fs_bounds.top, bounds.top);
assert!(fs_bounds.width >= bounds.width);
assert!(fs_bounds.height >= bounds.height);
Ok(())
}
#[test]
fn actions_on_tab_wont_hang_after_browser_drops() -> Result<()> {
logging::enable_logging();
for _ in 0..20 {
let (_, browser, tab) = dumb_server(include_str!("simple.html"));
std::thread::spawn(move || {
let mut rng = rand::thread_rng();
let millis: u64 = rng.gen_range(0..5000);
std::thread::sleep(std::time::Duration::from_millis(millis));
trace!("dropping browser");
drop(browser);
});
let _element = tab.find_element("div#foobar");
}
Ok(())
}
#[test]
fn form_interaction() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("form.html"));
tab.wait_for_element("input#target")?
.type_into("mothership")?;
tab.wait_for_element("button")?.click()?;
let d = tab.wait_for_element("div#protocol")?.get_description()?;
assert!(d
.find(|n| n.node_value == "Missiles launched against mothership")
.is_some());
tab.wait_for_element("input#sneakattack")?.click()?;
tab.wait_for_element("button")?.click()?;
let d = tab.wait_for_element("div#protocol")?.get_description()?;
assert!(d
.find(|n| n.node_value == "Comrades, have a nice day!")
.is_some());
Ok(())
}
#[test]
fn send_character() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("form.html"));
tab.find_element("input#target")?.click()?;
tab.send_character("mothership")?;
tab.find_element("button")?.click()?;
let d = tab.wait_for_element("div#protocol")?.get_description()?;
assert!(d
.find(|n| n.node_value == "Missiles launched against mothership")
.is_some());
Ok(())
}
#[test]
fn tab_get_content() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("simple.html"));
let html = tab.get_content()?;
// The html returned depends on how the browser formatted it. The HTML is always correct, but
// some of the newlines or tabs might be missing.
assert!(html.replace('\n', "") == include_str!("simple.html").replace('\n', ""));
Ok(())
}
#[test]
fn element_get_content() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("simple.html"));
let elem = tab.find_element("div#within")?;
let html = elem.get_content()?;
assert!(html == r#"<div id="within"></div>"#);
Ok(())
}
fn decode_png(i: &[u8]) -> Result<Vec<u8>> {
let decoder = png::Decoder::new(i);
let mut reader = decoder.read_info()?;
let mut buf = vec![0; reader.output_buffer_size()];
reader.next_frame(&mut buf)?;
Ok(buf)
}
fn sum_of_errors(inp: &[u8], fixture: &[u8]) -> u32 {
inp.chunks_exact(fixture.len())
.map(|c| {
c.iter()
.zip(fixture)
.map(|(b, e)| (i32::from(*b) - i32::from(*e)).pow(2) as u32)
.sum::<u32>()
})
.sum()
}
#[test]
fn set_background_color() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("transparent.html"));
tab.wait_for_element("body")?;
// Check that the top-left pixel on the page has the background color set in transparent.html
tab.set_background_color(RGBA {
r: 255,
g: 0,
b: 0,
a: Some(1.),
})?;
let png_data = tab.capture_screenshot(CaptureScreenshotFormatOption::Png, None, None, true)?;
let buf = decode_png(&png_data[..])?;
assert!(sum_of_errors(&buf[0..4], &[0xff, 0x00, 0x00, 0xff]) < 5);
Ok(())
}
#[test]
fn set_transparent_background_color() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("transparent.html"));
tab.wait_for_element("body")?;
// Check that the top-left pixel on the page has the background color set in transparent.html
tab.set_transparent_background_color()?;
let png_data = tab.capture_screenshot(CaptureScreenshotFormatOption::Png, None, None, true)?;
let buf = decode_png(&png_data[..])?;
assert!(sum_of_errors(&buf[0..4], &[0x00, 0x00, 0x00, 0x00]) < 5);
Ok(())
}
#[test]
fn capture_screenshot_png() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("simple.html"));
tab.wait_for_element("div#foobar")?;
// Check that the top-left pixel on the page has the background color set in simple.html
let png_data = tab.capture_screenshot(CaptureScreenshotFormatOption::Png, None, None, true)?;
let buf = decode_png(&png_data[..])?;
assert!(sum_of_errors(&buf[0..4], &[0x11, 0x22, 0x33, 0xff]) < 5);
Ok(())
}
#[test]
fn capture_screenshot_element() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("simple.html"));
// Check that the screenshot of the div's content-box has no other color than the one set in simple.html
let png_data = tab
.wait_for_element("div#foobar")?
.capture_screenshot(CaptureScreenshotFormatOption::Png)?;
let buf = decode_png(&png_data[..])?;
for i in 0..buf.len() / 4 {
assert!(sum_of_errors(&buf[i * 4..(i + 1) * 4], &[0x33, 0x22, 0x11, 0xff]) < 5);
}
Ok(())
}
#[test]
fn capture_screenshot_element_box() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("simple.html"));
// Check that the top-left pixel of the div's border-box has the border's color set in simple.html
let pox = tab.wait_for_element("div#foobar")?.get_box_model()?;
let png_data = tab.capture_screenshot(
CaptureScreenshotFormatOption::Png,
None,
Some(pox.border_viewport()),
true,
)?;
let buf = decode_png(&png_data[..])?;
assert!(dbg!(sum_of_errors(&buf[0..4], &[0x22, 0x11, 0x33, 0xff])) < 15);
Ok(())
}
#[test]
fn capture_screenshot_jpeg() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("simple.html"));
tab.wait_for_element("div#foobar")?;
let jpg_data =
tab.capture_screenshot(CaptureScreenshotFormatOption::Jpeg, Some(100), None, true)?;
let mut decoder = jpeg_decoder::Decoder::new(&jpg_data[..]);
let buf = decoder.decode().unwrap();
assert!(sum_of_errors(&buf[0..4], &[0x11, 0x22, 0x33]) < 5);
Ok(())
}
#[test]
fn test_print_file_to_pdf() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("./pdfassets/index.html"));
let local_pdf = tab.wait_until_navigated()?.print_to_pdf(None)?;
assert!(local_pdf.len() > 1000); // an arbitrary size
assert!(local_pdf.starts_with(b"%PDF"));
Ok(())
}
#[test]
fn get_box_model() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("simple.html"));
let pox = tab.wait_for_element("div#foobar")?.get_box_model()?;
// Check that the div has exactly the dimensions we set in simple.html
assert_eq!(pox.width as i32, 3 + 100 + 3);
assert_eq!(pox.height as i32, 3 + 20 + 3);
Ok(())
}
#[test]
fn box_model_geometry() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("simple.html"));
let center = tab.wait_for_element("div#position-test")?.get_box_model()?;
let within = tab.wait_for_element("div#within")?.get_box_model()?;
let above = tab
.wait_for_element("div#strictly-above")?
.get_box_model()?;
let below = tab
.wait_for_element("div#strictly-below")?
.get_box_model()?;
let left = tab.wait_for_element("div#strictly-left")?.get_box_model()?;
let right = tab
.wait_for_element("div#strictly-right")?
.get_box_model()?;
assert!(above.content.strictly_above(¢er.content));
assert!(above.content.above(¢er.content));
assert!(above.margin.above(¢er.content));
assert!(!above.margin.strictly_above(¢er.content));
assert!(above.content.within_horizontal_bounds_of(¢er.content));
assert!(!above.content.within_vertical_bounds_of(¢er.content));
assert!(below.content.strictly_below(¢er.content));
assert!(below.content.below(¢er.content));
assert!(below.margin.below(¢er.content));
assert!(!below.margin.strictly_below(¢er.content));
assert!(left.content.strictly_left_of(¢er.content));
assert!(left.content.left_of(¢er.content));
assert!(left.margin.left_of(¢er.content));
assert!(!left.margin.strictly_left_of(¢er.content));
assert!(!left.content.within_horizontal_bounds_of(¢er.content));
assert!(left.content.within_vertical_bounds_of(¢er.content));
assert!(right.content.strictly_right_of(¢er.content));
assert!(right.content.right_of(¢er.content));
assert!(right.margin.right_of(¢er.content));
assert!(!right.margin.strictly_right_of(¢er.content));
assert!(within.content.within_bounds_of(¢er.content));
assert!(!center.content.within_bounds_of(&within.content));
Ok(())
}
#[test]
fn reload() -> Result<()> {
logging::enable_logging();
let mut counter = 0;
let responder = move |r: tiny_http::Request| {
let response = tiny_http::Response::new(
200.into(),
vec![tiny_http::Header::from_bytes(&b"Content-Type"[..], &b"text/html"[..]).unwrap()],
std::io::Cursor::new(format!(r#"<div id="counter">{counter}</div>"#)),
None,
None,
);
trace!("{}", counter);
counter += 1;
r.respond(response)
};
let server = server::Server::new(responder);
let (browser, tab) = dumb_client(&server);
assert!(tab
.wait_for_element("div#counter")?
.get_description()?
.find(|n| n.node_value == "0")
.is_some());
assert!(tab
.reload(false, None)?
.wait_for_element("div#counter")?
.get_description()?
.find(|n| n.node_value == "1")
.is_some());
// TODO test effect of scriptEvaluateOnLoad
Ok(())
}
#[test]
fn find_elements() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let divs = tab.wait_for_elements("div")?;
let divs_xpath = tab.wait_for_elements_by_xpath("//*[@id]")?;
assert_eq!(8, divs.len());
assert_eq!(7, divs_xpath.len());
Ok(())
}
/*
#[test]
fn find_element_on_tab_and_other_elements() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let containing_element = tab.find_element("div#position-test")?;
let inner_element = containing_element.find_element("#strictly-above")?;
dbg!(&inner_element);
let attrs = inner_element.get_attributes()?.unwrap();
assert_eq!(attrs["id"], "strictly-above");
Ok(())
}
*/
#[test]
fn find_element_on_tab_by_xpath() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let containing_element_xpath = tab.wait_for_xpath("/html/body/div[2]")?;
let inner_element_xpath =
containing_element_xpath.wait_for_xpath(r#"//*[@id="strictly-above"]"#)?;
dbg!(&inner_element_xpath);
let attrs = inner_element_xpath.get_attributes()?.unwrap();
let id: Vec<&String> = attrs.iter().filter(|v| v.as_str() == "id").collect();
assert_eq!(id[0].as_str(), "strictly-above");
Ok(())
}
#[test]
fn set_user_agent() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(
r#"
<html>
<body>
<script>
document.write(navigator.userAgent + ";" + navigator.platform + ";" + navigator.language);
</script>
</body>
</html>
"#,
);
tab.set_user_agent("UnitTestClient", Some("de-DE-1996"), Some("UnitTest"))?;
// The test-tab has already navigated once, so reload to ensure that js
// environment is using the correct values.
tab.reload(true, None)?;
assert!(tab
.wait_for_element("body")?
.get_description()?
.find(|n| n
.node_value
.starts_with("UnitTestClient;UnitTest;de-DE-1996"))
.is_some());
Ok(())
}
#[test]
fn wait_for_element_returns_unexpected_errors_early() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let start = Instant::now();
let remote_error = tab
.wait_for_element("") // pass an invalid selector
.unwrap_err()
.downcast::<RemoteError>()?;
assert_eq!(remote_error.message, "DOM Error while querying");
assert!(start.elapsed() < Duration::from_secs(1));
Ok(())
}
#[test]
fn call_js_fn_sync() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let element = tab.wait_for_element("#foobar")?;
let result = element.call_js_fn("function() { return 42 }", vec![], false)?;
assert_eq!(result.Type, RemoteObjectType::Number);
assert_eq!(result.description, Some("42".to_owned()));
assert_eq!(result.value, Some((42).into()));
Ok(())
}
#[test]
fn call_js_fn_async_unresolved() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let element = tab.wait_for_element("#foobar")?;
let result = element.call_js_fn("async function() { return 42 }", vec![], false)?;
assert_eq!(result.Type, RemoteObjectType::Object);
assert_eq!(result.subtype, Some(RemoteObjectSubtype::Promise));
assert_eq!(result.description, Some("Promise".to_owned()));
assert_eq!(result.value, None);
Ok(())
}
#[test]
fn call_js_fn_async_resolved() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let element = tab.wait_for_element("#foobar")?;
let result = element.call_js_fn("async function() { return 42 }", vec![], true)?;
assert_eq!(result.Type, RemoteObjectType::Number);
assert_eq!(result.subtype, None);
assert_eq!(result.description, Some("42".to_owned()));
assert_eq!(result.value, Some((42).into()));
Ok(())
}
#[test]
fn evaluate_sync() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let result = tab.evaluate("(function () { return 42 })();", false)?;
assert_eq!(result.Type, RemoteObjectType::Number);
assert_eq!(result.description, Some("42".to_owned()));
assert_eq!(result.value, Some((42).into()));
Ok(())
}
#[test]
fn evaluate_async_unresolved() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let result = tab.evaluate("(async function () { return 42 })();", false)?;
assert_eq!(result.Type, RemoteObjectType::Object);
assert_eq!(result.description, Some("Promise".to_owned()));
assert_eq!(result.subtype, Some(RemoteObjectSubtype::Promise));
assert_eq!(result.value, None);
Ok(())
}
#[test]
fn evaluate_async_resolved() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let result = tab.evaluate("(async function () { return 42 })();", true)?;
assert_eq!(result.Type, RemoteObjectType::Number);
assert_eq!(result.subtype, None);
assert_eq!(result.description, Some("42".to_owned()));
assert_eq!(result.value, Some((42).into()));
Ok(())
}
#[test]
fn set_request_interception() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!(
"coverage_fixtures/basic_page_with_js_scripts.html"
));
let patterns = vec![
RequestPattern {
url_pattern: None,
resource_Type: None,
request_stage: Some(RequestStage::Response),
},
RequestPattern {
url_pattern: None,
resource_Type: None,
request_stage: Some(RequestStage::Request),
},
];
tab.enable_fetch(Some(&patterns), None)?;
tab.enable_request_interception(Arc::new(
move |transport: Arc<Transport>, session_id: SessionId, intercepted: RequestPausedEvent| {
if intercepted.params.request.url.ends_with(".js") {
let js_body = r#"document.body.appendChild(document.createElement("hr"));"#;
let headers = vec![HeaderEntry {
name: "Content-Type".to_string(),
value: "application/javascript".to_string(),
}];
let fulfill_request = FulfillRequest {
request_id: intercepted.params.request_id,
response_code: 200,
response_headers: Some(headers),
binary_response_headers: None,
body: Some(base64::prelude::BASE64_STANDARD.encode(js_body)),
response_phrase: None,
};
RequestPausedDecision::Fulfill(fulfill_request)
} else {
RequestPausedDecision::Continue(None)
}
},
))?;
// ignore cache:
tab.navigate_to(&format!("http://127.0.0.1:{}", server.port()))
.unwrap();
tab.wait_until_navigated()?;
// There are two JS scripts that get loaded via network, they both append an element like this:
assert_eq!(2, tab.wait_for_elements("hr")?.len());
Ok(())
}
#[test]
fn authentication() -> Result<()> {
logging::enable_logging();
let browser = Browser::default()?;
let tab = browser.new_tab()?;
tab.authenticate(Some("login".to_string()), Some("password".to_string()))?;
tab.enable_fetch(None, Some(true))?;
tab.navigate_to("http://httpbin.org/basic-auth/login/password")?;
tab.wait_until_navigated()?;
Ok(())
}
#[test]
fn response_handler() -> Result<()> {
logging::enable_logging();
let server = server::Server::with_dumb_html(include_str!(
"coverage_fixtures/basic_page_with_js_scripts.html"
));
let browser = Browser::default()?;
let tab = browser.new_tab().unwrap();
let responses = Arc::new(Mutex::new(Vec::new()));
let responses2 = responses.clone();
let responses3 = responses.clone();
assert!(tab
.register_response_handling(
"test1",
Box::new(move |response, fetch_body| {
// NOTE: you can only fetch the body after it's been downloaded, which might be some time
// after the initial 'response' (with status code, headers, etc.) has come back. hence this
// sleep:
sleep(Duration::from_millis(500));
let body = fetch_body().unwrap();
responses2.lock().unwrap().push((response, body));
})
)?
.is_none());
assert!(tab
.register_response_handling(
"test2",
Box::new(move |response, fetch_body| {
// NOTE: you can only fetch the body after it's been downloaded, which might be some time
// after the initial 'response' (with status code, headers, etc.) has come back. hence this
// sleep:
sleep(Duration::from_millis(500));
let body = fetch_body().unwrap();
responses3.lock().unwrap().push((response, body));
})
)?
.is_none());
tab.navigate_to(&format!("http://127.0.0.1:{}", server.port()))
.unwrap();
tab.wait_until_navigated()?;
let final_responses: Vec<_> = responses.lock().unwrap().clone();
assert_eq!(final_responses.len(), 3 * 2);
assert_eq!(final_responses[0].0.response.mime_type, "text/html");
assert!(final_responses[0].1.body.contains("Click me"));
Ok(())
}
#[test]
fn loading_failed_handler() -> Result<()> {
logging::enable_logging();
let server = server::Server::with_dumb_html(include_str!(
"coverage_fixtures/basic_page_with_loading_failed_element.html"
));
let browser = Browser::default()?;
let tab = browser.new_tab().unwrap();
let failed_event = Arc::new(Mutex::new(Vec::new()));
let failed_event_clone = failed_event.clone();
assert!(tab
.register_loading_failed_handling(
"test1",
Box::new(move |response, loading_failed| {
failed_event_clone
.lock()
.unwrap()
.push((response, loading_failed))
})
)?
.is_none());
tab.navigate_to(&format!("http://127.0.0.1:{}", server.port()))
.unwrap();
tab.wait_until_navigated()?;
let final_failed_event: Vec<_> = failed_event.lock().unwrap().clone();
assert_eq!(final_failed_event.len(), 1);
assert_eq!(
final_failed_event[0].0.response.url,
"http://httpbin.org/status/404"
);
assert_eq!(final_failed_event[0].0.response.status, 404);
assert_eq!(final_failed_event[0].1.error_text, "net::ERR_ABORTED");
Ok(())
}
#[test]
fn incognito_contexts() -> Result<()> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let incognito_context = browser.new_context()?;
let incognito_tab: Arc<Tab> = incognito_context.new_tab()?;
let tab_context_id = incognito_tab.get_target_info()?.browser_context_id.unwrap();
assert_eq!(incognito_context.get_id(), tab_context_id);
assert_eq!(
incognito_context.get_tabs()?[0].get_target_id(),
incognito_tab.get_target_id()
);
Ok(())
}
#[test]
fn get_script_source() -> Result<()> {
logging::enable_logging();
let server = server::file_server("tests/coverage_fixtures");
let browser = Browser::default()?;
let tab: Arc<Tab> = browser.new_tab()?;
tab.enable_profiler()?;
tab.start_js_coverage()?;
tab.navigate_to(&format!(
"{}/basic_page_with_js_scripts.html",
&server.url()
))?;
tab.wait_until_navigated()?;
sleep(Duration::from_millis(500));
let script_coverages = tab.take_precise_js_coverage()?;
tab.enable_debugger()?;
let contents = tab.get_script_source(&script_coverages[0].script_id)?;
assert_eq!(
include_str!("coverage_fixtures/coverage_fixture1.js"),
contents
);
let contents = tab.get_script_source(&script_coverages[1].script_id)?;
assert_eq!(
include_str!("coverage_fixtures/coverage_fixture2.js"),
contents
);
Ok(())
}
#[test]
fn read_write_cookies() -> Result<()> {
logging::enable_logging();
let responder = move |r: tiny_http::Request| {
let response = tiny_http::Response::new(
200.into(),
vec![
tiny_http::Header::from_bytes(&b"Content-Type"[..], &b"text/html"[..]).unwrap(),
tiny_http::Header::from_bytes(&b"Set-Cookie"[..], &b"testing=1; Max-Age=100;"[..])
.unwrap(),
],
std::io::Cursor::new("<div></div>"),
None,
None,
);
r.respond(response)
};
let server = server::Server::new(responder);
let (browser, tab) = dumb_client(&server);
// can read cookies
{
tab.navigate_to(&server.url())?;
tab.wait_until_navigated()?;
let cookies = tab.get_cookies()?;
assert_eq!(cookies.len(), 1);
let Cookie { name, value, .. } = cookies.first().unwrap();
assert_eq!(name, "testing");
assert_eq!(value, "1");
let t: Result<()> = Ok(()); // type hint for error
t
}?;
// can change (delete and set) value for current url
{
tab.set_cookies(vec![CookieParam {
name: "testing".to_string(),
value: "2".to_string(),
url: None,
domain: None,
path: None,
secure: None,
http_only: None,
same_site: None,
expires: None,
priority: None,
same_party: None,
source_scheme: None,
source_port: None,
partition_key: None,
}])?;
let cookies = tab.get_cookies()?;
assert_eq!(cookies.len(), 1);
let cf = cookies.first().unwrap();
assert_eq!(cf.name, "testing");
assert_eq!(cf.value, "2");
let t: Result<()> = Ok(()); // type hint for error
t
}?;
Ok(())
}
#[test]
fn close_tabs() -> Result<()> {
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let tabs = browser.get_tabs();
let wait = Wait::with_timeout(Duration::from_secs(30));
let check_tabs = |num: usize| {
let num_tabs = tabs.lock().unwrap().len();
assert_eq!(num, num_tabs);
};
let wait_tabs = |num: usize| {
let num_tabs = tabs.lock().unwrap().len();
if num_tabs == num {
Some(true)
} else {
None
}
};
check_tabs(1);
let new_tab1 = browser.new_tab()?;
new_tab1
.navigate_to(&format!("http://127.0.0.1:{}", server.port()))?
.wait_until_navigated()?;
check_tabs(2);
let new_tab2 = browser.new_tab()?;
new_tab2
.navigate_to(&format!("http://127.0.0.1:{}", server.port()))?
.wait_until_navigated()?;
check_tabs(3);
let closed = new_tab1.close(true)?;
assert!(closed);
wait.until(|| wait_tabs(2))?;
check_tabs(2);
new_tab2.close_with_unload()?;
wait.until(|| wait_tabs(1))?;
check_tabs(1);
Ok(())
}
#[test]
fn parses_shadow_doms() -> Result<()> {
logging::enable_logging();
let (_, browser, tab) = dumb_server(include_str!("shadow-dom.html"));
tab.wait_for_element("html")?;
Ok(())
}
#[test]
fn set_extra_http_headers() -> Result<()> {
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let mut headers = HashMap::new();
headers.insert("test", "header");
tab.set_extra_http_headers(headers)?;
tab.enable_fetch(None, None)?;
tab.enable_request_interception(Arc::new(
|transport: Arc<Transport>, session_id: SessionId, intercepted: RequestPausedEvent| {
println!("{:?}", intercepted.params.request.headers);
// assert_eq!(
// intercepted.params.request.headers.get("test"),
// Some(&"header".to_string())
// );
RequestPausedDecision::Continue(None)
},
))?;
tab.navigate_to(&format!("http://127.0.0.1:{}", server.port()))?
.wait_until_navigated()?;
Ok(())
}
#[test]
fn get_css_styles() -> Result<()> {
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
tab.navigate_to(&format!("http://127.0.0.1:{}", server.port()))?
.wait_until_navigated()?;
let element = tab.wait_for_element("#within")?;
let styles = element.get_computed_styles()?;
let v = styles
.iter()
.filter_map(|p| {
if p.name == "top" || p.name == "background-color" || p.name == "position" {
Some(p.value.as_str())
} else {
None
}
})
.collect::<Vec<&str>>();
assert!(!v.is_empty());
assert_eq!(["rgb(255, 255, 0)", "absolute", "5px"], v.as_slice());
Ok(())
}