7

I configured selenium server in docker. It works ok - I can connect to it, but when I want to interact with running local shiny app Rselenium does not see it. Details below:

I did step-by-step:

  • I run selenium server:

    docker run -d -p 4445:4444 selenium/standalone-chrome

  • successfully connected to selenium server:

remDr <- remoteDriver(remoteServerAddr = "localhost" , port = 4445L , browserName = "chrome" , platform = "MAC")

> remDr$open() [1] "Connecting to remote server"

  • run shiny app from terminal (in separate r session):

> shiny::runApp(file.path(find.package("RSelenium"), "apps", "shinytestapp"), port = 6012) Listening on http://127.0.0.1:6012

  • and then tried to do some tests:

remDr$navigate("localhost:6012") appTitle <- remDr$getTitle()[[1]] expect_equal(appTitle, "Shiny Test App")

and got error:

Error: 'appTitle' not equal to "Shiny Test App". 1/1 mismatches x[1]: "localhost" y[1]: "Shiny Test App"

  • after all I made a screenshot:

remDr$screenshot(display = TRUE)

and it looks like this:

enter image description here

Do you have idea why RSelenium does not see shiny app running locally?

Taz
  • 5,755
  • 6
  • 26
  • 63
  • @ jdharrison I mounted docker using command your command and it looks like this: `PORTS: 0.0.0.0:6012->6012/tcp, 0.0.0.0:4445->4444/tcp` but RSelenium still doesn't see shiny app. Any other ideas? – Taz Aug 23 '17 at 23:58

2 Answers2

3

I figured it out with a LOT OF HELP from @jdharrison.

First make docker compose file (be careful with indentations - one indention must be 2 spaces) and save as docker-compose.yml:

version: '2'
services:
  ropensci:
    image: rocker/ropensci
    ports:
      - "8788:8787"
    links:
      - selenium:selenium
      - shiny:shiny
  selenium:
    image: selenium/standalone-chrome
    ports:
      - "4445:4444"
    links:
      - shiny:shiny
  shiny:
    image: rocker/shiny
    container_name: shiny
    volumes:
      - ~/Users/username/services/volumes/shiny/apps:/srv/shiny-server/
      - ~/Users/username/services/volumes/shiny/logs:/var/log/
      - ~/Users/username/services/volumes/shiny/packages:/home/shiny/

or download: https://codeshare.io/2j4yLB

then run docker-compose up from folder where docker-compose.yml file is.

  • Add your apps to /home/username/services/volumes/shiny/apps
  • To navigate to your app from selenium use http://shiny:3838/myapp

To check if it works you can save below code as app.R in: ~/Users/username/services/volumes/shiny/apps/example/:

library(shiny)
server <- function(input, output) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })
}
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100)
    ),
    mainPanel(plotOutput("distPlot"))
  )
)
shinyApp(ui = ui, server = server)

and run:

library(RSelenium) 
remDr <- remoteDriver(remoteServerAddr = "selenium", port = 4444L, browser = "chrome") 
remDr$open()
remDr$navigate(url = "http://shiny:3838/example")
remDr$screenshot(display = TRUE)

If everything is ok you should see screenshot: enter image description here

Taz
  • 5,755
  • 6
  • 26
  • 63
1

There are a number of ways to achive this. Easiest way is to run docker in --net=host mode. This will mean mean selenium server runs on the default port 4444

docker run -d --net=host selenium/standalone-chrome&

Your docker container will now have access to the HOST localhost.

To run on a non default PORT you can pass docker a selenium env variable:

docker run -d --net=host -e SE_OPTS="-port 4445" selenium/standalone-chrome
jdharrison
  • 30,085
  • 4
  • 77
  • 89
  • When I run docker like this I can't connect to docker on port 4444: `Failed to connect to localhost port 4444: Connection refuse`. I work on mac osx - does it matter? – Taz Aug 24 '17 at 07:57
  • Yes port 4444 sometimes seems an issue on MAC. You can set the docker container to run on an alternative as outlined above. – jdharrison Aug 24 '17 at 10:44
  • Still same error :`Undefined error in httr call. httr output: Failed to connect to localhost port 4445: Connection refused`. Maybe I should reconfigure something? Maybe I should do something with proxy settings? Or something else? – Taz Aug 24 '17 at 12:09
  • Yes it appears there are issues with MAC and --net=host https://github.com/docker/for-mac/issues/1031 . The preferred option would be to run shiny server as a container also then networking is straight forward. – jdharrison Aug 24 '17 at 12:22
  • But I have running docker container with rstudio and if I try : `remDr$navigate(url = "localhost:8787"); remDr$getTitle()[[1]]` I get: `localhost` too. It looks like `-p` mapping doesn't work. Why? – Taz Aug 24 '17 at 12:36
  • Your shiny app is running on the HOST machine. This needs to be running as a container. This container would then be able to communicate with the selenium server container. – jdharrison Aug 24 '17 at 12:45
  • See https://github.com/ropensci/RSelenium/issues/102 for details on using docker compose and communication between containers running rstudio etc and container running selenium – jdharrison Aug 24 '17 at 12:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152770/discussion-between-taz-and-jdharrison). – Taz Aug 24 '17 at 14:31