-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathREADME.Rmd
169 lines (123 loc) · 4.56 KB
/
README.Rmd
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
---
output: github_document
---
```{r, setup, echo = FALSE, message = FALSE}
knitr::opts_chunk$set(
tidy = FALSE,
fig.width = 4,
fig.height = 12,
fig.path = "man/figures")
```
```{r screenshot-manual, eval = FALSE, include = FALSE}
# Run this code to update the images (since results are random)
pjs <- webdriver::run_phantomjs()
ses <- Session$new(port = pjs$port)
ses$go("https://r-pkg.org/pkg/callr")
ses$takeScreenshot(file="man/figures/screenshot-1.png")
search <- ses$findElement("#cran-input")
search$sendKeys("html", key$enter)
ses$takeScreenshot(file="man/figures/screenshot-2.png")
```
# webdriver
> 'WebDriver' Client for 'PhantomJS'
<!-- badges: start -->
[](https://github.com/rstudio/webdriver/actions)
[](https://www.r-pkg.org/pkg/webdriver)
[](https://www.r-pkg.org/pkg/webdriver)
[](https://codecov.io/github/rstudio/webdriver?branch=main)
<!-- badges: end -->
A client for the 'WebDriver' 'API'. It allows driving a (probably headless)
web browser, and can be used to test web applications, including 'Shiny'
apps. In theory it works with any 'WebDriver' implementation, but it was only
tested with 'PhantomJS'.
## Installation
```r
install.packages("webdriver")
```
## Usage
```{r}
library(webdriver)
```
### PhantomJS
webdriver uses PhantomJS as a headless web browser. (In theory in works
with other WebDriver clients as well.) You can use the `install_phantomjs()`
function to download and install PhantomJS on your system. Alternatively
an installation that is in the PATH is sufficient.
The `run_phantomjs()` function starts PhantomJS, and waits until it is ready
to serve queries. It returns a process object that you can terminate
manually, and the port on which PhantomJS is listening.
```{r, results='hide'}
pjs <- run_phantomjs()
pjs
```
## $process
## PROCESS 'phantomjs', running, pid 17405.
##
## $port
## [1] 6795
### Sessions
Use the `Session` class to connection to a running PhantomJS process.
One process can save multiple sessions, and the sessions are independent
of each other.
```{r}
ses <- Session$new(port = pjs$port)
```
Once a session is established, you can manipulate the headless web browser
through it:
```{r}
ses$go("https://r-pkg.org/pkg/callr")
ses$getUrl()
ses$getTitle()
```
You can also take a screenshot of the whole web page, and show it on R's
graphics device, or save it to a PNG file:
```{r screenshot-1, eval = FALSE}
ses$takeScreenshot()
```

### HTML elements
The `Session` object has two methods to find HTML elements on the current
web page, which can then be further manipulated: `findElement()` and
`findElements()`. They work with CSS or XPATH selectors, and also with
(possibly partial) HTML text.
```{r}
install <- ses$findElement(".install-package")
install$getName()
install$getText()
```
If you have an HTML element that can receive keyboard keys, you can use
the `sendKeys()` method to send them. The `key` list helps with sending
special, characters, e.g. `key$enter` corresponds to pressing ENTER. For
example we can type into a search box:
```{r}
search <- ses$findElement("#cran-input")
search$sendKeys("html", key$enter)
ses$getUrl()
ses$getTitle()
```
```{r screenshot-2, eval = FALSE}
ses$takeScreenshot()
```

### JavaScript
The `executeScript()` method of a `Session` object runs arbitrary JavaScript
in the headless browser. It puts the supplied code into the body of a
JavaScript function, and the function will receive the additional arguments,
in its `arguments` array. `Element` objects as arguments are automatically
converted to the corresponding DOM elements in the browser.
The JavaScript function can return values to R. Returned HTML elements are
automatically converted to `Element` objects.
```{r}
ses$executeScript("return 42 + 'foobar';")
```
```{r}
search2 <- ses$executeScript("return document.getElementById('cran-input');")
search2$getName()
```
`Element` objects also have an `executeScript()` method, which works the
same way as the `Session` method, but it automatically supplies the HTML
element as the first argument of the JavaScript function.
`executeScript()` works synchronously. If you need asynchronous execution,
you can use the `executeScriptAsync()` function.
## License
MIT © Mango Solutions, RStudio