/
app_tester.R
169 lines (151 loc) · 5.11 KB
/
app_tester.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
#' App tester
#'
#' This is an R6 class for running automated evaluations of psychTestR tests
#' using the \code{shinytest} framework.
#' When you create an \code{AppTester} object,
#' it spins up a Shiny app and a headless web browser,
#' which you can then interact with by calling the methods of the
#' \code{AppTester} object.
#'
#' - \code{app <- AppTester$new(dir)}:
#' creates a new \code{AppTester} object. The argument
#' \code{dir} should be a directory containing a file \code{app.R},
#' with \code{app.R} containing your psychTestR test.
#'
#' - \code{app$stop()} - stops the daemon and deletes the output directory.
#' Call this once you're finished with the \code{AppTester} object.
#'
#' - \code{app$click(buttons)} - clicks all the buttons with IDs
#' listed in the character vector \code{buttons}.
#'
#' - \code{app$click_next()} - clicks the "Next" button.
#'
#' - \code{app$get_ui()} - returns the HTML of the current UI of the psychTestR test.
#'
#' - \code{app$get_ui_text()} - extracts displayed text from \code{app$get_ui()}
#' using some heuristics. If \code{squish = TRUE}, extra whitespace
#' is removed.
#'
#' - \code{app$expect_ui_text(text, squish = TRUE)} - throws an error
#' if the output of \code{app$get_ui_text(squish = squish)}
#' is not equal to the argument \code{text}.
#'
#' - \code{app$get_locals()} - returns the current local variables.
#'
#' - \code{app$get_globals()} - returns the current global variables.
#'
#' - \code{app$get_title()} - returns the current title.
#'
#' - \code{app$expect_title(text)} - throws an error
#' if the output of \code{app$get_title()}
#' is not equal to the argument \code{text}.
#'
#' - \code{app$get_problems_info()} - returns the "problems info"
#' text displayed at the bottom of the page.
#'
#' - \code{app$expect_problems_info(text)} - throws an error
#' if the output of \code{app$get_problems_info()}
#' is not equal to the argument \code{text}.
#'
#' - \code{app$get_results()} - returns the current results for the current participant.
#'
#' - \code{app$expect_results(val)} - throws an error
#' if the output of \code{as.list(app$get_results())}
#' is not equal to \code{val}.
#'
#' - \code{app$clean_output()} - deletes the output directory.
#'
#' - \code{app$set_inputs(formA = "Text A", formB = "Text B")} -
#' passes (for example) the string \code{"Text A"} to the UI
#' input with ID \code{"formA"} and the string \code{"Text B"}
#' to the UI input with ID \code{"formB"}.
#'
#' - \code{...} - \code{AppTester} objects inherit from \code{\link[shinytest]{ShinyDriver}}
#' objects, and have access to all of their methods.
#'
#' @md
#'
#' @note
#' \code{AppTester} depends on the prior installation of a headless web browser, PhantomJS,
#' which you can install with \code{\link[shinytest]{installDependencies}}.
#'
#' @export
#'
AppTester <- R6::R6Class(
"AppTester",
inherit = shinytest::ShinyDriver,
public = list(
get_ui = function() {
self$getAllValues(input = FALSE, output = FALSE, export = TRUE)$export$ui
},
get_ui_text = function(squish = TRUE) {
val <- self$get_ui() %>% as.character() %>% htm2txt::htm2txt()
if (squish) val <- self$str_squish(val)
val
},
str_squish = function(x) {
x <- gsub("^\\s", "", x) # trim whitespace from start of string
x <- gsub("\\s$", "", x) # trim whitespace from end of string
x <- gsub("\\s+", " ", x) # replace any duplicated whitespace with a single space
x
},
expect_ui_text = function(text, squish = TRUE) {
testthat::expect_equal(
self$get_ui_text(squish = squish),
text
)
},
get_locals = function() {
self$getAllValues(input = FALSE, output = FALSE, export = TRUE)$export$locals
},
get_globals = function() {
self$getAllValues(input = FALSE, output = FALSE, export = TRUE)$export$globals
},
get_title = function() {
self$getAllValues(input = FALSE, output = FALSE, export = TRUE)$export$title
},
expect_title = function(text) {
testthat::expect_equal(
self$get_title(),
text
)
},
get_problems_info = function() {
self$getAllValues(input = FALSE, output = FALSE, export = TRUE)$export$problems_info
},
expect_problems_info = function(text) {
testthat::expect_equal(
self$get_problems_info(),
text
)
},
get_results = function() {
self$getAllValues(input = FALSE, output = FALSE, export = TRUE)$export$results
},
expect_results = function(val) {
testthat::expect_equal(
self$get_results() %>% as.list(),
val
)
},
stop = function(clean = TRUE) {
super$stop()
if (clean) self$clean_output()
},
clean_output = function() {
unlink(file.path(self$getAppDir(), "output"),
recursive = TRUE)
},
set_inputs = function(...) {
self$setInputs(...)
},
click = function(buttons) {
call <- rep("click", times = length(buttons)) %>% setNames(buttons) %>% as.list()
do.call(self$set_inputs, args = call)
},
click_next = function() {
# self$set_inputs("next" = "click")
self$click("next")
}
)
)