-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.R
1513 lines (1436 loc) · 50.8 KB
/
session.R
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' Start a Selenium Client session
#'
#' @description
#' This class represents the client to a Selenium session. It will only work
#' if a Selenium server instance is running. If you get an error, use
#' [selenium_server_available()] to check if a server is running. See the
#' package README for more information, or use [selenium_server()] to try and
#' start a server automatically.
#'
#' @export
SeleniumSession <- R6::R6Class(
"SeleniumSession",
public = list(
#' @field id The id of the session, generated when the session is started.
id = NULL,
#' @field browser The browser that the session is using.
browser = NULL,
#' @field port The port that the session is using.
port = NULL,
#' @field host The host that the session is running on.
host = NULL,
#' @description
#' Create a Selenium session: opening a browser which can be controlled by
#' the Selenium client.
#'
#' @param browser The name of the browser to use (e.g. "chrome", "firefox",
#' "edge").
#' @param port The port that the Selenium server is using, so we can
#' connect to it.
#' @param host The host that the Selenium server is running on. This is
#' usually 'localhost' (i.e. your own machine).
#' @param verbose Whether to print the web requests that are being sent and
#' any responses.
#' @param capabilities A list of capabilities to pass to the Selenium
#' server, to combine with the defaults generated using `browser`.
#' See [chrome_options()], [firefox_options()], and [edge_options()].
#' @param request_body A list of request body parameters to pass to the
#' Selenium server. Overrides `capabilities`.
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return A `SeleniumSession` object.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new(verbose = TRUE)
#'
#' session$close()
#' }
initialize = function(browser = "firefox",
port = 4444L,
host = "localhost",
verbose = FALSE,
capabilities = NULL,
request_body = NULL,
timeout = 20) {
check_string(browser)
check_number_whole(port)
check_string(host)
check_bool(verbose)
check_list(capabilities, allow_null = TRUE)
check_list(request_body, allow_null = TRUE)
check_number_decimal(timeout, allow_null = TRUE)
opts <- switch(browser,
firefox = list(
browserName = "firefox",
acceptInsecureCerts = TRUE
),
edge = list(
browserName = "microsoftEdge",
),
list(browserName = browser)
)
if (!is.null(capabilities)) {
opts <- merge_lists(opts, capabilities)
}
body <- list(
capabilities = list(
firstMatch = list(named_list()),
alwaysMatch = opts
)
)
url <- sprintf("http://%s:%s", host, port)
private$req <- httr2::request(url)
private$verbose <- verbose
req <- req_command(private$req, "New Session")
req <- req_body_selenium(req, body, request_body = request_body)
result <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
result_r <- httr2::resp_body_json(result)
self$id <- result_r$value$sessionId
self$browser <- browser
self$port <- port
self$host <- host
},
#' @description
#' Create a [WebElement] object using the parameters of the current
#' session.
#'
#' @param id The element id.
#'
#' @return A [WebElement] object.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' element <- session$find_element(using = "css selector", value = "*")
#'
#' element2 <- session$create_webelement(id = element$id)
#'
#' session$close()
#' }
create_webelement = function(id) {
check_string(id)
result <- WebElement$new(self$id, private$req, private$verbose, id)
result
},
#' @description
#' Create a [ShadowRoot] object using the parameters of the current
#' session.
#'
#' @param id The shadow root id.
#'
#' @return A [ShadowRoot] object.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' shadow_root <- session$create_shadowroot(id = "foo")
#'
#' session$close()
#' }
create_shadowroot = function(id) {
check_string(id)
ShadowRoot$new(self$id, private$req, private$verbose, id)
},
#' @description
#' Close the current session. Once a session is closed, its methods will
#' no longer work. However, the Selenium server will still be running.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$close()
#' }
close = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Delete Session", session_id = self$id)
req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
invisible(self)
},
#' @description
#' Get the status of the Selenium server. Unlike all other methods, this
#' method is independent of the session itself (meaning it can be used
#' even after [SeleniumSession$close()][SeleniumSession] is called). It is
#' identical to [get_server_status()], but uses the host, port and verbose
#' options passed to the session, for convenience.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return A list that can (but may not always) contain the following
#' fields:
#'
#' * `ready`: Whether the server is ready to be connected to. This should
#' always be returned by the server.
#' * `message`: A message about the status of the server.
#' * `uptime`: How long the server has been running.
#' * `nodes`: Information about the slots that the server can take.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$status()
#'
#' session$close()
#'
#' session$status()
#' }
status = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
get_status(private$req, private$verbose)
},
#' @description
#' Get the timeouts of the current session. There are three types of
#' timeouts:
#'
#' * *session script timeout*: The amount of time that the server will wait
#' for scripts to run. Defaults to 3 seconds.
#' * *page load timeout*: The amount of time that the server will wait for
#' the page to load. Defaults to 30 seconds.
#' * *implicit wait*: The amount of time that the server will wait for
#' elements to be located, or for elements to become interactable when
#' required. Defaults to 0 seconds.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return A list with three items: `script`, `page_load`, and `implicit`.
#'
#' @examples
#' \dontrun{
#'
#' session <- SeleniumSession$new()
#'
#' session$get_timeouts()
#'
#' session$close()
#' }
get_timeouts = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Get Timeouts", session_id = self$id)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(response)$value
},
#' @description
#' Set the timeouts of the current session. The types of timeouts are
#' defined in `SeleniumSession$get_timeouts()`.
#'
#' @param script The amount of time to wait for scripts. By default, this
#' is not set.
#' @param page_load The amount of time to wait for the page to load.
#' @param implicit_wait The amount of time to wait for elements on the
#' page.
#' @param request_body A list of request body parameters to pass to the
#' Selenium server, overriding the default body of the web request
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$set_timeouts(script = 100)
#'
#' session$get_timeouts()
#'
#' session$close()
#' }
set_timeouts = function(script = NULL, page_load = NULL, implicit_wait = NULL, request_body = NULL, timeout = 20) {
check_number_decimal(script, allow_null = TRUE)
check_number_decimal(page_load, allow_null = TRUE)
check_number_decimal(implicit_wait, allow_null = TRUE)
check_list(request_body, allow_null = TRUE)
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Set Timeouts", session_id = self$id)
body <- compact(list(
script = script,
pageLoad = page_load,
implicit = implicit_wait
))
if (length(body) == 0) {
stop()
}
req <- req_body_selenium(req, body, request_body = request_body)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(response)$value
invisible(self)
},
#' @description
#' Navigate to a URL.
#'
#' @param url The URL to navigate to. Must begin with a protocol (e.g.
#' 'https://').
#' @param request_body A list of request body parameters to pass to the
#' Selenium server, overriding the default body of the web request.
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$close()
#' }
navigate = function(url, request_body = NULL, timeout = 20) {
check_string(url)
check_list(request_body, allow_null = TRUE)
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Navigate To", session_id = self$id)
req <- req_body_selenium(req, list(url = url), request_body = request_body)
req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
invisible(self)
},
#' @description
#' Get the current URL.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The URL of the current page.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$current_url()
#'
#' session$close()
#' }
current_url = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Get Current URL", session_id = self$id)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(response)$value
},
#' @description
#' Go back in the navigation history.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$navigate("https://www.tidyverse.org")
#'
#' session$back()
#'
#' session$current_url()
#'
#' session$close()
#' }
back = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Back", session_id = self$id)
req <- req_body_selenium(req, NULL)
req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
invisible(self)
},
#' @description
#' Go forward in the navigation history.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$navigate("https://www.tidyverse.org")
#'
#' session$back()
#'
#' session$forward()
#'
#' session$current_url()
#'
#' session$close()
#' }
forward = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Forward", session_id = self$id)
req <- req_body_selenium(req, NULL)
req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
invisible(self)
},
#' @description
#' Reload the current page.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$refresh()
#'
#' session$close()
#' }
refresh = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Refresh", session_id = self$id)
req <- req_body_selenium(req, NULL)
req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
invisible(self)
},
#' @description
#' Get the title of the current page.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The title of the current page.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$title()
#'
#' session$close()
#' }
title = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Get Title", session_id = self$id)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(response)$value
},
#' @description
#' Get the current window handle.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The handle of the current window (a string).
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$window_handle()
#'
#' session$close()
#' }
window_handle = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Get Window Handle", session_id = self$id)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(response)$value
},
#' @description
#' Close the current window.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$new_window()
#'
#' session$close_window()
#'
#' session$close()
#' }
close_window = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Close Window", session_id = self$id)
resp <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(resp)$value
},
#' @description
#' Switch to a specific window.
#'
#' @param handle The handle of the window to switch to.
#' @param request_body A list of request body parameters to pass to the
#' Selenium server, overriding the default body of the web request
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' handle <- session$window_handle()
#'
#' handle2 <- session$new_window()$handle
#'
#' session$switch_to_window(handle)
#'
#' session$switch_to_window(handle2)
#'
#' session$close()
#' }
switch_to_window = function(handle, request_body = NULL, timeout = 20) {
check_string(handle)
check_list(request_body, allow_null = TRUE)
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Switch To Window", session_id = self$id)
req <- req_body_selenium(req, list(handle = handle), request_body = request_body)
req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
invisible(self)
},
#' @description
#' Get the handles of all open windows.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The handles of all open windows (a list of strings).
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' handles <- session$window_handles()
#'
#' session$close()
#' }
window_handles = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Get Window Handles", session_id = self$id)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(response)$value
},
#' @description
#' Create a new window. Note that this window is not automatically
#' switched to.
#'
#' @param type Whether to create a tab or a window.
#' @param request_body A list of request body parameters to pass to the
#' Selenium server, overriding the default body of the web request
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return A list containing two elements:
#'
#' * `handle`: The handle of the new window.
#' * `type`: The type of window. ("tab" or "window").
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' handle <- session$new_window()$handle
#'
#' session$switch_to_window(handle)
#'
#' session$close()
#' }
new_window = function(type = c("tab", "window"), request_body = NULL, timeout = 20) {
type <- rlang::arg_match(type)
check_list(request_body, allow_null = TRUE)
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "New Window", session_id = self$id)
req <- req_body_selenium(req, list(type = type), request_body = request_body)
resp <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(resp)$value
},
#' @description
#' Frames allow you to split a window into multiple sections, where each
#' section can load a separate HTML document. This function allows you to
#' switch to a specific frame, given its ID, meaning that frame will become
#' the current browsing context.
#'
#' @param id The ID of the frame to switch to. By default, the top-level
#' browsing context is switched to (i.e. not a frame). This can also be
#' a [WebElement] object, in which case the frame that contains said
#' element will be switched to.
#' @param request_body A list of request body parameters to pass to the
#' Selenium server, overriding the default body of the web request
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$switch_to_frame()
#'
#' session$close()
#' }
switch_to_frame = function(id = NA, request_body = NULL, timeout = 20) {
check_list(request_body, allow_null = TRUE)
check_number_decimal(timeout, allow_null = TRUE)
if (inherits(id, "WebElement")) {
id <- id$toJSON()
} else {
check_number_whole(id, allow_na = TRUE)
}
req <- req_command(private$req, "Switch To Frame", session_id = self$id)
req <- req_body_selenium(req, list(id = id), request_body = request_body)
req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
invisible(self)
},
#' @description
#' Switch to the parent frame of the current frame.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$switch_to_frame()
#'
#' session$switch_to_parent_frame()
#'
#' session$close()
#' }
switch_to_parent_frame = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Switch To Parent Frame", session_id = self$id)
req <- req_body_selenium(req, NULL)
req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
invisible(self)
},
#' @description
#' Get the size and position of the current window.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return A list containing four elements:
#'
#' * `x`: The x position of the window relative to the left of the screen.
#' * `y`: The y position of the window relative to the top of the screen.
#' * `width`: The width of the window.
#' * `height`: The height of the window.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$get_window_rect()
#'
#' session$close()
#' }
get_window_rect = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Get Window Rect", session_id = self$id)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(response)$value
},
#' @description
#' Set the size and position of the current window.
#'
#' @param width The width of the window.
#' @param height The height of the window.
#' @param x The x position of the window relative to the left of the screen.
#' @param y The y position of the window relative to the top of the screen.
#' @param request_body A list of request body parameters to pass to the
#' Selenium server, overriding the default body of the web request
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$set_window_rect(width = 800, height = 600, x = 2, y = 3)
#'
#' session$close()
#' }
set_window_rect = function(width = NULL, height = NULL, x = NULL, y = NULL, request_body = NULL, timeout = 20) {
check_number_decimal(width, min = 0)
check_number_decimal(height, min = 0)
check_number_decimal(x)
check_number_decimal(y)
check_list(request_body, allow_null = TRUE)
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Set Window Rect", session_id = self$id)
body <- compact(list(
width = width,
height = height,
x = x,
y = y
))
if (length(body) == 0) {
stop()
}
req <- req_body_selenium(req, body, request_body = request_body)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(response)$value
},
#' @description
#' Maximize the current window. This makes the window the maximum size it
#' can be, without being full screen.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$maximize_window()
#'
#' session$close()
#' }
maximize_window = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Maximize Window", session_id = self$id)
req <- req_body_selenium(req, NULL)
resp <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(resp)$value
},
#' @description
#' Minimize the current window. This hides the window.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$minimize_window()
#'
#' session$close()
#' }
minimize_window = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Minimize Window", session_id = self$id)
req <- req_body_selenium(req, NULL)
resp <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(resp)$value
},
#' @description
#' Make the window full screen.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The session object, invisibly.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$fullscreen_window()
#'
#' session$close()
#' }
fullscreen_window = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Fullscreen Window", session_id = self$id)
req <- req_body_selenium(req, NULL)
resp <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(resp)$value
},
#' @description
#' Get the currently active element.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return A [WebElement] object.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$get_active_element()
#'
#' session$close()
#' }
get_active_element = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Get Active Element", session_id = self$id)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
id <- httr2::resp_body_json(response)$value
self$create_webelement(id[[1]])
},
#' @description
#' Find the first element matching a selector.
#'
#' @param using The type of selector to use.
#' @param value The value of the selector: a string.
#' @param request_body A list of request body parameters to pass to the
#' Selenium server, overriding the default body of the web request
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return A [WebElement] object.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$find_element(using = "css selector", value = "#download")
#'
#' session$find_element(using = "xpath", value = "//div[contains(@class, 'col-xs')]/h1")
#'
#' session$close()
#' }
find_element = function(using = c("css selector", "xpath", "tag name", "link text", "partial link text"),
value, request_body = NULL, timeout = 20) {
using <- rlang::arg_match(using)
check_string(value)
check_list(request_body, allow_null = TRUE)
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Find Element", session_id = self$id)
req <- req_body_selenium(req, list(using = using, value = value), request_body = request_body)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
id <- httr2::resp_body_json(response)$value
self$create_webelement(id[[1]])
},
#' @description
#' Find all elements matching a selector.
#'
#' @param using The type of selector to use.
#' @param value The value of the selector: a string.
#' @param request_body A list of request body parameters to pass to the
#' Selenium server, overriding the default body of the web request
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return A list of [WebElement] objects.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$find_elements(using = "css selector", value = "h1")
#'
#' session$find_elements(using = "xpath", value = "//h1")
#'
#' session$close()
#' }
find_elements = function(using = c("css selector", "xpath", "tag name", "link text", "partial link text"),
value, request_body = NULL, timeout = 20) {
using <- rlang::arg_match(using)
check_string(value)
check_list(request_body, allow_null = TRUE)
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Find Elements", session_id = self$id)
req <- req_body_selenium(req, list(using = using, value = value), request_body = request_body)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
ids <- httr2::resp_body_json(response)$value
lapply(ids, function(x) self$create_webelement(x[[1]]))
},
#' @description
#' Get the HTML source of the current page, serialized as a string.
#'
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return A string.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$navigate("https://www.r-project.org")
#'
#' session$get_page_source()
#'
#' session$close()
#' }
get_page_source = function(timeout = 20) {
check_number_decimal(timeout, allow_null = TRUE)
req <- req_command(private$req, "Get Page Source", session_id = self$id)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
httr2::resp_body_json(response)$value
},
#' @description
#' Execute a JavaScript script.
#'
#' @param x The script to execute. To return a value, do so explicitly,
#' e.g. `return 1`.
#' @param ... Additional arguments to pass to the script. These can be
#' accessed in the script using the `arguments` array. Can be [WebElement]
#' objects or lists of such objects, which will be converted to nodes.
#' @param request_body A list of request body parameters to pass to the
#' Selenium server, overriding the default body of the web request
#' @param timeout How long to wait for a request to recieve a response
#' before throwing an error.
#'
#' @return The return value of the script. Nodes or lists of nodes will
#' be converted to [WebElement] objects.
#'
#' @examples
#' \dontrun{
#' session <- SeleniumSession$new()
#'
#' session$execute_script("return 1")
#'
#' session$execute_script("return arguments[0] + arguments[1]", 1, 2)
#'
#' element <- session$find_element(value = "*")
#'
#' session$execute_script("return arguments[0]", element)
#'
#' session$close()
#' }
execute_script = function(x, ..., request_body = NULL, timeout = 20) {
check_string(x)
check_dots_unnamed()
args <- rlang::list2(...)
check_list(request_body, allow_null = TRUE)
check_number_decimal(timeout, allow_null = TRUE)
args <- prepare_for_json(args)
req <- req_command(private$req, "Execute Script", session_id = self$id)
req <- req_body_selenium(req, list(script = x, args = args), request_body = request_body)
response <- req_perform_selenium(req, verbose = private$verbose, timeout = timeout)
parse_json_result(httr2::resp_body_json(response)$value, self)
},
#' @description
#' Execute an asynchronous JavaScript script, waiting for a value to be
#' returned.
#'
#' @param x The script to execute. Unlike `execute_script()`. You return
#' an value using the callback function, which can be accessed using
#' `arguments[arguments.length - 1]`. For example, to return 1, you