Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Starting AppDriver in a custom URL for testing bookmarking #219

Closed
zsigmas opened this issue Jun 3, 2022 · 5 comments · Fixed by #262
Closed

Request: Starting AppDriver in a custom URL for testing bookmarking #219

zsigmas opened this issue Jun 3, 2022 · 5 comments · Fixed by #262
Labels
enhancement New feature or request

Comments

@zsigmas
Copy link

zsigmas commented Jun 3, 2022

Hi all, I have not yet found a direct way of testing bookmarking. This is relevant when creating modules that you want to guarantee will save and restore bookmarks correctly.

One possibility would be to include a parameter in the AppDriver$new method:

app <- appDriver$new(appObject, query_args = "/?inputs_&n=10"
@schloerke
Copy link
Collaborator

For now, you can start your app in a background process and provide the url directly.

Something like..

# Untested code!

# Start app in background process given the port value
port <- httpuv::randomPort()
app_bg <- callr::r_bg(
  function(port) {
    appObject <- make_app_object()
    shiny::runApp(
      appObject,
      port = port,
      test.mode = TRUE
    )
  },
  list(port = port),
  stderr = "|"
)
withr::defer({ app_bg$kill() })

# Wait until the app is ready
for (i in 1:100) {
  if (pingr::is_up("127.0.0.1", 12345)) {
    break
  }
  Sys.sleep(0.2)
}

# Create bookmark url
app_url <- paste0("http://127.0.0.1:", port, "/?inputs_n=10")

# Use url value
app <- appDriver$new(app_url)

# Test like normal

While not as easy as a parameter, it can be wrapped up into a method. Will probably need to adjust the {withr} call to withr::defer({ app_bg$kill() }, envir = rlang::caller_env())

@schloerke schloerke added the enhancement New feature or request label Jun 7, 2022
@zsigmas
Copy link
Author

zsigmas commented Jun 7, 2022

Thanks a lot!

@zsigmas
Copy link
Author

zsigmas commented Jun 7, 2022

Maybe a simpler solution for this problem could be:

(notice I have corrected some typos in the URL from the previous response)

bg_app <- shinytest2::AppDriver$new(app_dir = "my/shiny/app")
bg_app_url <- bg_app$get_url()
app<- shinytest2::AppDriver$new(paste0(bg_app_url, "?_inputs_&n=10"))

This delegates all the app launching process to shinytest2::AppDriver$new

Unfortunately, this (as well as the approach proposed in #219 (comment)) has the following problem:

bg_app <- shinytest2::AppDriver$new(app_dir = "my/shiny/app")
bg_app_url <- bg_app$get_url() # Assume that bg_app_url is http://127.0.0.1:1234
app<- shinytest2::AppDriver$new(paste0(bg_app_url, "/?inputs_n=10"))
app$get_url(shinytest2::AppDriver$new(paste0(bg_app_url, "?_inputs_&n=10"))
# [1] http://127.0.0.1:1234/?_inputs_=&n=10
# Notice the equal following '?_inputs_'

This breaks any attempt to include bookmarking parameters in the url.

I was able to pinpoint the problem to:

private$url <- httr::build_url(res)

I will report this in a different issue that links here to separate it from this enhancement request.

@schloerke
Copy link
Collaborator

What a wonderful solution!

bg_app <- shinytest2::AppDriver$new(app_dir = "my/shiny/app")
bg_app_url <- bg_app$get_url()
app<- shinytest2::AppDriver$new(paste0(bg_app_url, "?_inputs_&n=10"))

With this not being too terrible to have the user implement, (for now) I'm going to just make it a FAQ entry with your solution.

@schloerke
Copy link
Collaborator

The issues with _inputs_ != _inputs= has been addressed in #222

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants