Class to manage a shiny app and a phantom.js headless browser
app <- ShinyDriver$new(path = ".", loadTimeout = 5000, checkNames = TRUE, debug = c("none", "all", ShinyDriver$debugLogTypes), phantomTimeout = 5000, seed = NULL, cleanLogs = TRUE, shinyOptions = list())) app$stop() app$getDebugLog(type = c("all", ShinyDriver$debugLogTypes))
app$getValue(name, iotype = c("auto", "input", "output")) app$setValue(name, value, iotype = c("auto", "input", "output")) app$sendKeys(name = NULL, keys)
app$getWindowSize() app$setWindowSize(width, height)
app$getUrl() app$goBack() app$refresh() app$getTitle() app$getSource() app$takeScreenshot(file = NULL)
app$findElement(css = NULL, linkText = NULL, partialLinkText = NULL, xpath = NULL)
app$findElements(css = NULL, linkText = NULL, partialLinkText = NULL, xpath = NULL)
app$waitFor(expr, checkInterval = 100, timeout = 3000)
app$waitForValue(name, ignore = list(NULL, ""), iotype = "input", timeout = 10000, checkInterval = 400)
app$listWidgets()
app$checkUniqueWidgetNames()
app$findWidget(name, iotype = c("auto", "input", "output"))
app$expectUpdate(output, ..., timeout = 3000, iotype = c("auto", "input", "output"))
A ShinyDriver
instance.
Path to a directory containing a Shiny app, i.e. a
single app.R
file or a server.R
and ui.R
pair.
How long to wait for the app to load, in ms. This includes the time to start R.
How long to wait when connecting to phantomJS process, in ms.
Whether to check if widget names are unique in the app.
Whether to start the app in debugging mode. In debugging mode debug messages are printed to the console.
An optional random seed to use before starting the application. For apps that use R's random number generator, this can make their behavior repeatable.
Whether to remove the stdout and stderr logs when the Shiny process object is garbage collected.
A list of options to pass to runApp()
.
Name of a shiny widget. For $sendKeys
it can
be NULL
, in which case the keys are sent to the active
HTML element.
Type of the Shiny widget. Usually shinytest
finds the widgets by their name, so this need not be specified,
but Shiny allows input and output widgets with identical names.
Keys to send to the widget or the app. See the
sendKeys
method of the webdriver
package.
Scalar integer, the desired width of the browser window.
Scalar integer, the desired height of the browser window.
File name to save the screenshot to. If NULL
, then
it will be shown on the R graphics device.
CSS selector to find an HTML element.
Find <a>
HTML elements based on their
innerText
.
Find <a>
HTML elements based on their
innerText
. It uses partial matching.
Find HTML elements using XPath expressions.
A string scalar containing JavaScript code that evaluates to the condition to wait for.
How often to check for the condition, in milliseconds.
List of possible values that are to not be
considered valid. app$waitForValue
will continue to poll until
it finds a value not contained in ignore
.
Timeout for the condition, in milliseconds.
Character vector, the name(s) of the Shiny output widgets that should be updated.
When setting the value of an input, allow it to set the value of an input even if that input does not have an input binding.
For expectUpdate
these can be named arguments.
The argument names correspond to Shiny input widgets: each input
widget will be set to the specified value.
ShinyDriver$new()
function creates a ShinyDriver
object. It starts
the Shiny app in a new R session, and it also starts a phantomjs
headless browser that connects to the app. It waits until the app is
ready to use. It waits at most loadTimeout
milliseconds, and if
the app is not ready, then it throws an error. You can increase
loadTimeout
for slow loading apps. Currently it supports apps
that are defined in a single app.R
file, or in a server.R
and ui.R
pair.
app$stop()
stops the app, i.e. the external R process that runs
the app, and also the phantomjs instance.
app$getDebugLog()
queries one or more of the debug logs:
shiny_console
, browser
or shinytest
.
app$getValue()
finds a widget and queries its value. See
the getValue
method of the Widget
class.
app$setInputs()
sets the value of inputs. The arguments must all
be named; an input with each name will be assigned the given value.
app$uploadFile()
uploads a file to a file input. The argument must
be named and the value must be the path to a local file; that file will be
uploaded to a file input with that name.
app$getAllValues()
returns a named list of all inputs, outputs,
and export values.
app$setValue()
finds a widget and sets its value. See the
setValue
method of the Widget
class.
app$sendKeys
sends the specified keys to the HTML element of the
widget.
app$getWindowSize()
returns the current size of the browser
window, in a list of two integer scalars named ‘width’ and
‘height’.
app$setWindowSize()
sets the size of the browser window to the
specified width and height.
app$getUrl()
returns the current URL.
app$goBack()
“presses” the browser's ‘back’
button.
app$refresh()
“presses” the browser's ‘refresh’
button.
app$getTitle()
returns the title of the page. (More precisely
the document title.)
app$getSource()
returns the complete HTML source of the current
page, in a character scalar.
app$takeScreenshot()
takes a screenshot of the current page
and writes it to a file, or (if file
is NULL
) shows it
on the R graphics device. The output file has PNG format.
app$findElement()
find an HTML element on the page, using a
CSS selector or an XPath expression. The return value is an
Element
object from the webdriver
package.
app$findElements()
finds potentially multiple HTML elements,
and returns them in a list of Element
objects
from the webdriver
package.
app$waitFor()
waits until a JavaScript expression evaluates
to true
, or a timeout happens. It returns TRUE
is the
expression evaluated to true
, possible after some waiting.
app$waitForValue()
waits until the current application's
input
(or output
) value is not one of the supplied invalid
values. The function returns the value found if the time limit has not
been reached (default is 10 seconds). This function can be useful in
helping determine if an application has initialized or finished
processing a complex reactive situation.
app$listWidgets()
lists the names of all input and output
widgets. It returns a list of two character vectors, named input
and output
.
app$checkUniqueWidgetNames()
checks if Shiny widget names
are unique.
app$findWidget()
finds the corresponding HTML element of a Shiny
widget. It returns a Widget
object.
expectUpdate()
is one of the main functions to test Shiny apps.
It performs one or more update operations via the browser, and then
waits for the specified output widgets to update. The test succeeds if
all specified output widgets are updated before the timeout. For
updates that involve a lot of computation, you increase the timeout.
# NOT RUN {
## https://github.com/rstudio/shiny-examples/tree/master/050-kmeans-example
app <- ShinyDriver$new("050-kmeans-example")
expectUpdate(app, xcol = "Sepal.Width", output = "plot1")
expectUpdate(app, ycol = "Petal.Width", output = "plot1")
expectUpdate(app, clusters = 4, output = "plot1")
# }
Run the code above in your browser using DataCamp Workspace