rstudio / shinytest

Automated testing for shiny apps
https://rstudio.github.io/shinytest/
Other
225 stars 55 forks source link

Shinytest for a dynamically created UI issue (with reprex) #385

Closed GreyMerchant closed 11 months ago

GreyMerchant commented 3 years ago

Hello,

I am trying to incorporate shinytest into my shinyapp and I am able to run the recording of the test with recordTest() but I end up with the error below . Below I have attached the reprex for you (it is a simple survey where depending on what you answer in the question it will create a certain set of questions on the next screen for you. Just note the package which you need to install for it to work.

#devtools::install_github('mtrybulec/interviewer')
library(interviewer)
#> Loading required package: shiny
#> Loading required package: shinyjs
#> Warning: package 'shinyjs' was built under R version 4.0.3
#> 
#> Attaching package: 'shinyjs'
#> The following object is masked from 'package:shiny':
#> 
#>     runExample
#> The following objects are masked from 'package:methods':
#> 
#>     removeClass, show
#> 
#> Attaching package: 'interviewer'
#> The following object is masked from 'package:shinyjs':
#> 
#>     runExample
#> The following object is masked from 'package:shiny':
#> 
#>     runExample
library(shiny)

ui <- shiny::fluidPage(
  interviewer::useInterviewer(),
  shiny::uiOutput(outputId = "questionnaireOutput")
)

server <- 
function(input, output, session) {

  responses <- interviewer::buildResponses(
    id = c("a", "b", "c"),
    label = c("response A", "response B", "response C")
  )

  output$questionnaireOutput <-
    interviewer::questionnaire(
      label = "Loop DEMO",
      welcome = list(
        shiny::p("Welcome!"),
        shiny::HTML("<p>This demo shows how <strong>loops</strong> can be defined in <strong>interviewer</strong>.</p>")
      ),

      interviewer::question.multiple(
        id = "LoopSource",
        label = "Loop source (two questions will be asked for each response mentioned here, in random order)",
        responses = responses
      ),

      interviewer::pageBreak(),

      function() {
        result <- list()
        responseIds <- getResponseIds("LoopSource")
        responseIds <- responseIds[sample(length(responseIds))] # randomize subsequent blocks

        for (responseId in responseIds) {
          responseLabel <- responses[which(responses$id == responseId), "label"]

          result <- append(
            result,
            list(
              interviewer::question.single(
                id = paste0("LoopQuestion1", responseId),
                label = sprintf("Loop question 1 for '%s'", responseLabel),
                responses = responses
              ),

              interviewer::question.single(
                id = paste0("LoopQuestion2", responseId),
                label = sprintf("Loop question 2 for '%s'", responseLabel),
                responses = responses
              ),

              interviewer::pageBreak()
            )
          )
        }

        result
      },

      interviewer::question.single(
        id = "Final",
        label = "Final",
        responses = responses
      ),

      goodbye = "Done!",
      exit = function(data) {
        cat("Done:\n")
        print(data)
      }
    )

}

shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:5136

After you have the shiny app working you can just run recordTest in the right folder with the app.R file from above and you should see the error after you made your full selection of actions that need to be captured. I can share my exact test scenario but other instances trigger it too.

library(shinytest)

shinytest::recordTest("loop_simple_test")

Running mytest.R Error in session_makeRequest(self, private, endpoint, data, params, headers) : 
  Can't find variable: $escape

Any help on this?

maxheld83 commented 3 years ago

this may be related to / smaller reprex may be in #402

StatisMike commented 2 years ago

$escape is the function used in input bindings JS code. Error may be based on the used package itself.

Try to clone the repo and change $escape in this line https://github.com/mtrybulec/interviewer/blob/master/inst/www/main.js#L122 to Shiny.$escape. Install from that local repo and try it again

flachboard commented 11 months ago

Hello, is there any update regarding the issue involving inputs and a dynamic UI while testing?

gadenbuie commented 11 months ago

I agree with @StatisMike that this particular issue is most likely specific to the interviewer package.

@flachboard Have you tried shinytest2, the newer replacement for shinytest? I'd recommend investigating if that package fits your use case.

flachboard commented 11 months ago

My apologies, I am using shinytest2 and having this issue. Thanks for the response!

gadenbuie commented 11 months ago

@flachboard no worries! Please take a look at the issues page in the shinytest2 repo to see if anything fits your issue. If not, feel free to open an issue there with a reproducible example.