-
Notifications
You must be signed in to change notification settings - Fork 1
/
global_actions.R
378 lines (327 loc) · 9.27 KB
/
global_actions.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
#' Open a URL
#'
#' Navigate the browser to specified URL, waiting until the page is considered
#' open before finishing.
#'
#' @param url The URL to navigate to: a string.
#' @inheritParams back
#'
#' @returns
#' The session object, invisibly.
#'
#' @family global actions
#'
#' @examplesIf selenider::selenider_available()
#' session <- selenider_session()
#'
#' open_url("https://r-project.org")
#'
#' # Or:
#' open_url(session = session, "https://r-project.org")
#'
#' @export
open_url <- function(url, timeout = NULL, session = NULL) {
check_string(url)
check_number_decimal(timeout, allow_null = TRUE)
check_class(session, "selenider_session", allow_null = TRUE)
if (is.null(timeout)) {
timeout <- if (on_ci()) 60 * 5 else 60
}
if (is.null(session)) {
session <- get_session(.env = caller_env())
}
check_session_active(session)
driver <- session$driver
if (session$session == "chromote") {
promise <- driver$Page$loadEventFired(wait_ = FALSE, timeout_ = timeout)
driver$Page$navigate(url, wait_ = FALSE, timeout_ = timeout)
driver$wait_for(promise)
} else if (session$session == "selenium") {
driver$set_timeouts(page_load = timeout * 1000)
driver$navigate(url, timeout = timeout)
} else {
driver$setTimeout(milliseconds = timeout * 1000)
driver$navigate(url)
}
invisible(session)
}
#' Move back or forward in browsing history
#'
#' @description
#' `back()` navigates to the previously opened URL, or the previously opened
#' page in your browsing history.
#'
#' `forward()` reverses the action of `back()`, going to the next page in your
#' browsing history.
#'
#' @param timeout The maximum time to wait for the page to load, in seconds.
#' This defaults to 60, unless in a Github Action, in which case it defaults
#' to 5 minutes.
#' @param session A `selenider_session` object. If not specified, the global
#' session object (the result of [get_session()]) is used.
#'
#' @returns
#' The session object, invisibly.
#'
#' @family global actions
#'
#' @examplesIf selenider::selenider_available()
#' session <- selenider_session()
#'
#' open_url("https://r-project.org")
#'
#' open_url("https://www.tidyverse.org/")
#'
#' back()
#'
#' forward()
#'
#' @export
back <- function(timeout = NULL, session = NULL) {
check_number_decimal(timeout, allow_null = TRUE)
check_class(session, "selenider_session", allow_null = TRUE)
if (is.null(timeout)) {
timeout <- if (on_ci()) 60 * 5 else 60
}
if (is.null(session)) {
session <- get_session(.env = caller_env())
}
check_session_active(session)
driver <- session$driver
if (session$session == "chromote") {
navigation_history <- driver$Page$getNavigationHistory()
index <- navigation_history$currentIndex + 1
history <- navigation_history$entries
if (index > 1) {
new_id <- history[[index - 1]]$id
promise <- driver$Page$loadEventFired(wait_ = FALSE, timeout_ = timeout)
driver$Page$navigateToHistoryEntry(new_id, wait_ = FALSE, timeout_ = timeout)
driver$wait_for(promise)
} else {
warn_history_page_not_found(next_page = FALSE)
}
} else if (session$session == "selenium") {
driver$set_timeouts(page_load = timeout * 1000)
driver$back(timeout = timeout)
} else {
driver$setTimeout(milliseconds = timeout * 1000)
driver$goBack()
}
invisible(session)
}
#' @rdname back
#'
#' @export
forward <- function(timeout = NULL, session = NULL) {
check_number_decimal(timeout, allow_null = TRUE)
check_class(session, "selenider_session", allow_null = TRUE)
if (is.null(timeout)) {
timeout <- if (on_ci()) 60 * 5 else 60
}
if (is.null(session)) {
session <- get_session(.env = caller_env())
}
check_session_active(session)
driver <- session$driver
if (session$session == "chromote") {
navigation_history <- driver$Page$getNavigationHistory()
index <- navigation_history$currentIndex + 1
history <- navigation_history$entries
if (index < length(history)) {
new_id <- history[[index + 1]]$id
promise <- driver$Page$loadEventFired(wait_ = FALSE, timeout_ = timeout)
driver$Page$navigateToHistoryEntry(new_id, wait_ = FALSE, timeout_ = timeout)
driver$wait_for(promise)
} else {
warn_history_page_not_found(next_page = TRUE)
}
} else if (session$session == "selenium") {
driver$set_timeouts(page_load = timeout * 1000)
driver$forward(timeout = timeout)
} else {
driver$setTimeout(milliseconds = timeout * 1000)
driver$goForward()
}
invisible(session)
}
#' Reload the current page
#'
#' `reload()` and `refresh()` both reload the current page.
#'
#' @inheritParams back
#'
#' @returns
#' The session object, invisibly.
#'
#' @family global actions
#'
#' @examplesIf selenider::selenider_available()
#' session <- selenider_session()
#'
#' open_url("https://r-project.org")
#'
#' reload()
#'
#' @export
reload <- function(timeout = NULL, session = NULL) {
check_number_decimal(timeout, allow_null = TRUE)
check_class(session, "selenider_session", allow_null = TRUE)
if (is.null(timeout)) {
timeout <- if (on_ci()) 60 * 5 else 60
}
if (is.null(session)) {
session <- get_session(.env = caller_env())
}
check_session_active(session)
driver <- session$driver
if (session$session == "chromote") {
promise <- driver$Page$loadEventFired(wait_ = FALSE, timeout_ = timeout)
session$driver$Page$reload(wait_ = FALSE, timeout_ = timeout)
driver$wait_for(promise)
} else if (session$session == "selenium") {
driver$set_timeouts(page_load = timeout * 1000)
driver$refresh(timeout = timeout)
} else {
driver$setTimeout(milliseconds = timeout * 1000)
driver$refresh()
}
invisible(session)
}
#' @rdname reload
#'
#' @export
refresh <- reload
#' Take a screenshot of the current page
#'
#' Take a screenshot of the current session state, saving this image to a file.
#'
#' @param file The file path to save the screenshot to.
#' @param view Whether to open the interactively view the screenshot. If this is
#' `TRUE` and `file` is `NULL`, the screenshot will be deleted after viewing.
#' @inheritParams back
#'
#' @returns
#' `file`, if it is not `NULL`. Otherwise, the session object is returned, invisibly.
#'
#' @family global actions
#'
#' @examplesIf selenider::selenider_available()
#' session <- selenider_session()
#'
#' open_url("https://www.google.com")
#'
#' file_path <- withr::local_tempfile(fileext = ".png")
#'
#' take_screenshot(file_path)
#'
#' @export
take_screenshot <- function(file = NULL, view = FALSE, session = NULL) {
check_string(file, allow_null = TRUE)
check_bool(view)
check_class(session, "selenider_session", allow_null = TRUE)
if (view) {
rlang::check_installed("showimage", reason = "to view the screenshot")
}
if (is.null(file) && !view) {
stop_null_screenshot_file()
}
if (is.null(session)) {
session <- get_session(.env = caller_env())
}
check_session_active(session)
if (is.null(file)) {
file <- withr::local_tempfile(fileext = ".png")
}
if (session$session == "chromote") {
session$driver$screenshot(file, show = view)
} else if (session$session == "selenium") {
result <- session$driver$screenshot()
writeBin(jsonlite::base64_dec(result), file)
if (view) {
showimage::show_image(file)
}
} else {
session$driver$screenshot(file = file)
if (view) {
showimage::show_image(file)
}
}
if (!is.null(file)) {
file
} else {
invisible(session)
}
}
#' Read the HTML of a session
#'
#' Uses [xml2::read_html()] to read the page source of the session
#'
#' @param session Optionally, a `selenider_session` object.
#' @param ... Passed into [xml2::read_html()]
#'
#' @returns An XML document.
#'
#' @family global actions
#'
#' @examplesIf selenider::selenider_available(online = FALSE)
#' html <- "
#' <p>Example text</p>
#' "
#'
#' session <- minimal_selenider_session(html)
#'
#' get_page_source()
#'
#' @export
get_page_source <- function(session = NULL, ...) {
rlang::check_installed("xml2")
if (is.null(session)) {
session <- get_session(.env = caller_env())
}
check_session_active(session)
driver <- session$driver
if (session$session == "chromote") {
document <- driver$DOM$getDocument()
root <- document$root$nodeId
page_source <- driver$DOM$getOuterHTML(root)$outerHTML
} else if (session$session == "selenium") {
page_source <- driver$get_page_source()
} else {
page_source <- unpack_list(driver$getPageSource())
}
xml2::read_html(page_source, ...)
}
#' Get the URL of the current page
#'
#' Get the full URL of the current page.
#'
#' @param session Optionally, a `selenider_session` object.
#'
#' @returns A string: the current URL.
#'
#' @family global actions
#'
#' @examplesIf selenider::selenider_available()
#' session <- selenider_session()
#'
#' open_url("https://r-project.org")
#'
#' current_url()
#'
#' @export
current_url <- function(session = NULL) {
if (is.null(session)) {
session <- get_session(.env = caller_env())
}
check_session_active(session)
driver <- session$driver
if (session$session == "chromote") {
history <- driver$Page$getNavigationHistory()
current_page <- history$entries[[history$currentIndex + 1]]
current_page$url
} else if (session$session == "selenium") {
driver$current_url()
} else {
unpack_list(driver$getCurrentUrl())
}
}