R: Class to manage a shiny app and a phantom.js headless browser

ShinyDriver {shinytest} | R Documentation

## Class to manage a shiny app and a phantom.js headless browser

### Description

Class to manage a shiny app and a phantom.js headless browser

### Usage

    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$get_windows_size()
    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$listWidgets()

    app$checkUniqueWidgetNames()

    app$findWidget(name, iotype = c("auto", "input", "output"))

    app$expectUpdate(output, ..., timeout = 3000,
        iotype = c("auto", "input", "output"))

### Arguments

app

  A `ShinyDriver` instance.

path

  Path to a directory containing a Shiny app, i.e. a single `app.R` file or a `server.R` and `ui.R` pair.

loadTimeout

  How long to wait for the app to load, in ms. This includes the time to start R.

phantomTimeout

  How long to wait when connecting to phantomJS process, in ms.

checkNames

  Whether to check if widget names are unique in the app.

debug

  Whether to start the app in debugging mode. In debugging mode debug messages are printed to the console.

seed

  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.

cleanLogs

  Whether to remove the stdout and stderr logs when the Shiny process object is garbage collected.

shinyOptions

  A list of options to pass to `runApp()`.

name

  Name of a shiny widget. For `$sendKeys` it can be `NULL`, in which case the keys are sent to the active HTML element.

iotype

  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

  Keys to send to the widget or the app. See the `sendKeys` method of the `webdriver` package.

width

  Scalar integer, the desired width of the browser window.

height

  Scalar integer, the desired height of the browser window.

file

  File name to save the screenshot to. If `NULL`, then it will be shown on the R graphics device.

css

  CSS selector to find an HTML element.

linkText

  Find `<a>` HTML elements based on their `innerText`.

partialLinkText

  Find `<a>` HTML elements based on their `innerText`. It uses partial matching.

xpath

  Find HTML elements using XPath expressions.

expr

  A string scalar containing JavaScript code that evaluates to the condition to wait for.

checkInterval

  How often to check for the condition, in milliseconds.

timeout

  Timeout for the condition, in milliseconds.

output

  Character vector, the name(s) of the Shiny output widgets that should be updated.

...

  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.

### Details

`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 error 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$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.

### Examples

    ## 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")

    ## End(Not run)

[Package shinytest version 1.3.0 Index]