RSelenium - Introduction

Orange County R User Group

John Harrison (johndharrison0@gmail.com)

John Harrison

What is Selenium?

  1. Selenium automates browsers. (http://docs.seleniumhq.org/)
  2. Selenium is a set of tools for cross-platform automated testing of web applications.
  3. Selenium supports
    • Chrome, Firefox, Safari, Internet Explorer, PhantomJS and others
    • Windows, OS X, Linux, Android, iOS and other OS's
    • Bindings in a number of languages (http://docs.seleniumhq.org/download/)

Why a set of tools?

Selenium is actually composed of a number of projects (http://docs.seleniumhq.org/projects/).

  • alt text Selenium IDE
  • alt text Selenium Remote Control
  • alt text Selenium WebDriver
  • alt text Selenium Grid

Selenium WebDriver and Selenium Grid compose Selenium 2.0.

Selenium 2.0

RSelenium interacts with the WebDriver
The WebDriver "drives" the browsers.
The browsers have various bindings
Each browser may have bespoke methods
  • Firefox - The driver comes in the form of an xpi (firefox extension)
  • Chrome - executable downloaded from the Chromium project
    which acts as a bridge between "chrome" and the "driver".
  • IE - similar to chrome
  • The SafariDriver is implemented as a Safari browser extension.

RSelenium

Basic operation

  • Browser initiation
  • Navigation
  • DOM interaction
  • Frames and Windows

Browser initiation

# RSelenium::startServer()
library(RSelenium)
remDr <- remoteDriver(browserName = "someBrowser", remoteServerAddr = ip, port = port)
remDr$open(silent = TRUE)
remDr$navigate("http://somecoolsite")
BrowserSessions% Sessions
1.
Chrome
135
45.76%
2.
Firefox
111
37.63%
3.
Safari
29
9.83%
4.
Internet Explorer
12
4.07%
5.
Opera
3
1.02%
6.
Safari (in-app)
3
1.02%
7.
Android Browser
2
0.68%

Navigation

remDr$Navigate("http://somewhere.com")
remDr$goBack()
remDr$goForward()
remDr$refresh()
remDr$getTitle()
remDr$getCurrentUrl()
remDr$getStatus()
remDr$getAllCookies()
remDr$deleteCookieNamed("PREF")

DOM interaction

RSelenium has a number of methods of finding elements in the document object model with two methods to search anchor elements (An anchor is a piece of text which marks the beginning and/or the end of a hypertext link.)

  • By id
  • By class
  • By css selector
  • By name
  • By tag name
  • By link text (anchor elements)
  • By partial link text (anchor elements)

Frames and Windows

remDr$switchToFrame("string|number|null|WebElement")
remDr$getWindowHandles()
remDr$getCurrentWindowHandle()
remDr$switchToWindow("windowId")

Advanced operation

  • Javascript
  • Interacting with shiny apps
  • Remote driving
  • External providers

Javascript

Interacting with shiny apps

Remote driving

External providers

  • Sauce Labs - http://saucelabs.com/

    user <- "rselenium0"
    pass <- "*******************************"
    port <- 80
    ip <- paste0(user, ':', pass, "@ondemand.saucelabs.com")
    browser <- "firefox"
    version <- "25"
    platform <- "OS X 10.9"
    extraCapabilities <- list(name = "Test RSelenium", username = user, accessKey = pass)
    
    remDr <- remoteDriver$new(remoteServerAddr = ip, port = port, browserName = browser
                          , version = version, platform = platform
                          , extraCapabilities = extraCapabilities)
    

External providers

  • Browser Stack - http://www.browserstack.com/

    require(RSelenium)
    user <- "johnharrison" 
    pass <- "******************"
    port <- 80
    ip <- paste0(user, ':', pass, "@hub.browserstack.com")
    extraCapabilities <- list("browser" = "IE",
                          "browser_version" = "7.0",
                          "os" = "Windows",
                          "os_version" = "XP",
                          "browserstack.debug" = "true")
    remDr <- remoteDriver$new(remoteServerAddr = ip, port = port
                          , extraCapabilities = extraCapabilities)
    

External example

testAppScript <- function(remDr){
  remDr$open(); remDr$setImplicitWaitTimeout(2000)
  remDr$navigate("http://spark.rstudio.com/johnharrison/shinytestapp/")
  Sys.sleep(2)
  webElems <- remDr$findElements("css selector", "#ctrlSelect span")
  lapply(webElems, function(x){x$highlightElement()})
  Sys.sleep(2)
  appIds <- c("summary", "distPlot", "ggPlot", "dttable")
  lapply(seq_along(webElems), function(x){
    if(!webElems[[x]]$isElementSelected()[[1]]){
      webElems[[x]]$clickElement()
      # test for its output
      out <- remDr$findElement("id", appIds[x])
      out$highlightElement()
    }})
  remDr$close()
}
testAppScript(remDr)