## Introduction

The goal of this vignette is to give a basic overview of how one might approach using RSelenium with combinations of operating systems (OS) and browsers both locally and remotely.

## RSelenium with Local Fully-fledged Browsers

### Firefox

The default browser for RSelenium is firefox. When a remoteDriver class is instantiated using default options for example remdr <- remoteDriver() then the browser listed is firefox.

remdr <- remoteDriver()
remDr
## $remoteServerAddr ## [1] "localhost" ## ##$port
## [1] 4444
##
## $browserName ## [1] "firefox" ## ##$version
## [1] ""
##
## $platform ## [1] "ANY" ## ##$javascript
## [1] TRUE
##
## $autoClose ## [1] FALSE ## ##$nativeEvents
## [1] TRUE
##
## $extraCapabilities ## list() Other browsers can be driven using RSelenium. We shall split these browsers into three groups. Full-fledged browsers, headless browsers and mobile browsers. The standalone selenium jar has the ability to drive other full-fleged browsers such as chrome, internet explorer, safari and opera. First we shall look at how to drive chrome using RSelenium ### Chrome Firstly we note that chrome in this instance can be considered as having three parts. There is the browser itself (“chrome”), the language bindings provided by the Selenium project (“the driver”) and an executable downloaded from the Chromium project which acts as a bridge between “chrome” and the “driver”. This executable is called “chromedriver”. We need to have a “chromedriver” running. First we need to locate one. The download directory for chromedriver is currently located at http://chromedriver.storage.googleapis.com/index.html. In this example we shall look at running chrome on a windows platform so we will download the windows chromedriver. The most uptodate version of chromedriver at the time of writing was 2.9. In the notes this supports chrome v31-34. We are running chrome 33 so this is fine. ----------ChromeDriver v2.9 (2014-01-31)---------- Supports Chrome v31-34 We download the appropriate file for windows and extract the .exe to our Documents folder. The .exe can be placed where the user pleases but it must be in the system path. In this case we placed in the Documents folder namely C:. This directory was added to the system path. We assume that a selenium server is also running if not one can be started using RSelenium::startServer(). Now we are done. A chrome browser can be controlled as follows: require(RSelenium) # RSelenium::startServer() # if needed remDr <- remoteDriver(browserName = "chrome") remDr$open()

head(remDr$sessionInfo) ##$platform
## [1] "WIN8"
##
## $acceptSslCerts ## [1] TRUE ## ##$javascriptEnabled
## [1] TRUE
##
## $browserName ## [1] "chrome" ## ##$chrome
## $chrome$userDataDir
## [1] "C:\\Users\\john\\AppData\\Local\\Temp\\scoped_dir24584_12002"
##
##
## $rotatable ## [1] FALSE ### Internet Explorer Similarly to the chrome browser you do not need to run an installer before using the InternetExplorerDriver, though some configuration is required. The standalone server executable must be downloaded from the Downloads page and placed in your PATH. Again we need to download this executable and place it in our path. At the time of writing the internet explorer .exe is included with the main standalone server here. The current release is 2.40. The system I am running is 64 bit so we download the 64bit version. For simplicity we again place this in our Documents directory namely C:. This directory is already in the system path from running the chrome example. If you want to place the internet explorer .exe in another folder add this folder to your system path. To control internet explorer as a browser is now as simple as: require(RSelenium) # RSelenium::startServer() # if needed remDr <- remoteDriver(browserName = "internet explorer") remDr$open()

head(remDr$sessionInfo, 7) ##$platform
## [1] "WINDOWS"
##
## $javascriptEnabled ## [1] TRUE ## ##$elementScrollBehavior
## [1] 0
##
## $ignoreZoomSetting ## [1] FALSE ## ##$enablePersistentHover
## [1] TRUE
##
## $ie.ensureCleanSession ## [1] FALSE ## ##$browserName
## [1] "internet explorer"

### Safari

Currently Apple have discontinued developement of safari for windows. The latest version for windows was 5.1.7 available here. Starting with Selenium 2.30.0, the SafariDriver comes bundled with the Selenium server so nothing other then having safari installed should be required. For the purposes of this vignette I downloaded and installed safari 5.1.7 on a windows 8.1 system. Once installed controlling safari was as easy as:

require(RSelenium)
# RSelenium::startServer() # if needed
remDr <- remoteDriver(browserName = "safari")
remDr$open() head(remDr$sessionInfo)
## $platform ## [1] "WINDOWS" ## ##$cssSelectorsEnabled
## [1] TRUE
##
## $javascriptEnabled ## [1] TRUE ## ##$secureSsl
## [1] TRUE
##
## $browserName ## [1] "safari" ## ##$webdriver.remote.sessionid
## [1] "a18da818-5160-47c4-8e88-7e95605c5cab"

### Opera

Opera is currently not supported for versions newer then 12. http://code.google.com/p/selenium/wiki/OperaDriver gives details on the current status. For the purposes of this vignette I downloaded and installed the 64 bit version of 12.16 located here. I had some issues getting this to run YMMV however. The first issue was getting the following error message

require(RSelenium)
# RSelenium::startServer() # if needed
remDr <- remoteDriver(browserName = "opera")
remDr$open() cat(remDr$value$message) ## Could not find a platform that supports bundled launchers, please set it manually ## Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:54:28' ## System info: host: 'johnlt', ip: '192.168.58.1', os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.7.0_45' ## Driver info: driver.version: OperaDriver So not surprising win 8.1 was first unveiled and released as a public beta in June 2013 and on July 4, 2013, Opera 12.16 was released being the last current version of opera supported by selenium. OperaLauncherRunner.java located here does not currently cater for the WIN8 enum returned by Platform.getCurrent(). The solution is to start the selenium server with additional parameters passed to java (RSelenium::startServer doesnt pass arguments to java atm) java -Dos.name=windows -jar selenium-server-standalone.jar This is actually using java -D which is used to set a system property. The system property we set is os.name. This is nothing to do with selenium-server and the appearance of Dos is a coincidence not related to DOS. Now we get a new issue. require(RSelenium) # RSelenium::startServer() # if needed remDr <- remoteDriver(browserName = "opera") remDr$open()

remDr$value$message
## [1] "Unable to find executable for product Opera Desktop"

So in this case we refer to the operadriver wiki. It states that the OperaDriver server expects you to have Opera or Opera Next installed in the default location for each system which for windows is %PROGRAMFILES%.exe or more precisely C:Files.exe. As I have installed the 64bit version I need to tell opera driver where to look

require(RSelenium)
# RSelenium::startServer() # if needed
remDr <- remoteDriver(
browserName = "opera",
extraCapabilities = list("opera.binary" = "C:\\Program Files\\Opera x64\\opera.exe")
)
remDr$open() head(remDr$sessionInfo)
## $platform ## [1] "ANY" ## ##$opera.binary
## [1] "C:\\Program Files\\Opera x64\\opera.exe"
##
## $javascriptEnabled ## [1] TRUE ## ##$opera.host
## [1] "127.0.0.1"
##
## $browserName ## [1] "opera" ## ##$opera.idle
## [1] FALSE

A few small issues. I suspect if you were running win 7 or lower and the 32 bit version of opera 12.16 it would probably run out of the box.

## RSelenium with Local Headless Browsers

Next we shall look at running what is known as headless browsers. Usually a browser can do three things

1. For given url, download the html page (or any other content apart from html)
2. Render the content into dom, eg executing javascript inside the script tag. and the executed result will be reflected on the browsers dom.
3. Render the dom into visualised content.

A headless browser handles items 1 and 2 but doesnt carryout 3. This means it doesnt display anything. All pages etc. are in memory rather then displayed to the user. The result of this is that headless browsers should perform faster then their full-fledged competitors which could be welcome news to speed up testing.

### phantomjs

The first headless browser we shall look at is phantomjs. Firstly download the relevant zip file for your OS from here. We are using windows so we downloaded phantomjs-2.1.1-windows.zip. It is sufficient to place the location of the directory containing phantomjs.exe in your path. In this case we probably could have just extracted phantomjs.exe to the Documents folder where chromedriver etc current reside.

However I extracted it to the desktop keeping the contents of the zip. The reasoning behind this was that phantomjs is driven by selenium using ghostdriver. At some point the version of ghostdriver phantomjs uses will be upgraded and will accept calls from an unexposed method phantomExecute of the RSelenium remoteDriver class. There are interesting scripts contained in the phantomjs /example directory like netsniff.js which captures network traffic in HAR format. When the phantomExecute method is exposed these scripts will be useful. So I added the location of the .exe to my path namely the directory C:-1.9.7-windows. Once your operating system can find phantomjs.exe or the equivalent driving a phantomjs browser is as easy as:

require(RSelenium)
# RSelenium::startServer() # if needed
remDr <- remoteDriver(browserName = "phantomjs")
remDr$open() head(remDr$sessionInfo)
## $platform ## [1] "XP" ## ##$acceptSslCerts
## [1] FALSE
##
## $javascriptEnabled ## [1] TRUE ## ##$browserName
## [1] "phantomjs"
##
## $rotatable ## [1] FALSE ## ##$driverVersion
## [1] "1.1.0"

We can take a screenshot even thou the browser is headless:

remDr$navigate("http://www.google.com/ncr") remDr$screenshot(display = TRUE)
###### RSelenium on win 8.1 driving phantomjs

PhantomJS is excellent. It has only recently as of version 1.8 had Ghost Driver integration and hopefully its importance will increase further.

### HtmlUnit

The original headless browser for selenium was htmlunit.

## RSelenium with Local Mobile Browsers

Configuring your local machine to use mobile browsers can be slightly tricky. If you are having difficulty setting up on your particular OS you may want to skip this section.

### Android

The first mobile browser we will look at driving is Android. The selenium project had android drivers in the selenium project. The current state of these drivers is listed here.

As can be noted driving android using the selenium server has been deprecated in favour of the selendroid project. Once selendroid has been setup this means that rather than running the selenium standalone jar as a server we will be running an equivalent selendroid jar to drive our browser on our real or emulated android phone. More on this later for now we will look at setting selendroid up.

#### Setting up Selendroid

The selendroid project has a page on getting started.

##### JDK

There are a couple of things to note Java SDK (minimum 1.6) must be installed. Most likely JRE is installed on your system. To check look for the directory C:Files1.7.0_51 or something similar. You can also check the version of java and it should indicate Java(TM) SE Runtime Environment.

java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

If you need the JDK you can install from here. Once the JDK is installed the environment variable JAVA_HOME should be set.

echo %JAVA_HOME%
C:\Program Files\Java\jdk1.7.0_51
##### SDK

Another Developement kit needs to be installed SDK in this case. From the link provided you can download the ADT Bundle for Windows. I downloaded the bundle and extracted the zip to the Desktop this resulted in a directory C:\Users\john\Desktop\adt-bundle-windows-x86_64-20131030. There is a guide to setup the SDK here. The environment variable ANDROID_HOME needs to be set. This should be set to the /sdk/ directory in the extracted bundle.

echo %ANDROID_HOME%
C:\Users\john\Desktop\adt-bundle-windows-x86_64-20131030\sdk

After setting ANDROID_HOME %ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools needs to be added to the system path also. Typing android in a command prompt should bring up the following:

###### Android SDK on win 8.1

From the tools make sure all are installed. From the latest android release Android 4.4.2 (API 19) in this case make sure all are installed. From the Extras folder, select the checkbox for the Android Support Library and make sure it is installed. You will also want to install the Intel Hardware Accelerated Execution Manager. Instructions on how to do so are here. Basically checking the box Intel x86 Emulator Accelerator (HAXM) and “installing” will download it to %ANDROID_HOME%/extras/intel. In this folder is an exe IntelHaxm.exe which should be ran to finish the install.

##### Creating an Android Virtual Device

Next we need to emulate a phone. The alternative is to use a hardware phone running the Android OS. Refer to here if you are running a hardware phone but it should be as simple as connecting it to the machine running selendroid via usb. We shall instead create an Android Virtual Device (Avd). The easiest way to do this is by typing android avd into a command console.

###### Android AVD on win 8.1

You can see I have created an avd already named “my_avd”. You will need to create one by clicking new. The details for the “my_avd” are shown here

###### Android AVD my_avd on win 8.1

Click ok and an avd should have been created. You can start it using the panel. Click the newly created avd then click start. It will take a few moments but a panel containing a virtual phone will hopefully boot up and eventually get to the phone screen.

###### Android AVD my_avd on win 8.1

If you have got to this point it is most likely you will now be able to drive this phone with selendroid. Take a few moments to play with your virtual android phone ;).

##### Selendroid

Finally, we need to download the selendroid driver. The selendroid homepage has a link at the bootom to the most recent jar. This jar should be saved on the local computer. I saved the jar in the Documents folder namely C:\Users\john\Documents. To begin running tests you need to run the jar with java in a command prompt.

java -jar selendroid-standalone-0.8.0-with-dependencies.jar
##### RSelenium and Selendroid

Finally we are ready to use RSelenium to control an android browser.

require(RSelenium)
remDr <- remoteDriver(browserName = "android")
remDr$open() remDr$navigate("http://www.google.com/ncr")

### iOS

Testing iOS requires a Mac. The setup is similar to selendroid. Appium and ios-driver can be used. An SDK is required namely iPhone simulator SDK and a virtual phone is ran in a similar fashion to Selendroid.

## RSelenium with Remote Browsers and External Sites

Setting up multiple OS/browsers combinations locally is not always the best use of ones time. It is an interesting exercise to implement for example an Android platform locally but the overhead of having multiple systems and browsers quickly overcomes the utility. There are professional service providers who maintain a suite of OS/browsers that can be utilised for testing. Companies such as https://saucelabs.com/ and http://www.browserstack.com offer free automated testing to open source projects. In this vignette we will demonstrate remote testing using Sauce Labs.

### Setting up Sauce Labs

We assume in this vignette that you are setting up Sauce Labs for an open source project. Firstly you should register your project here. On the account page you will find the access key for your account that is in a similar format to 49953c74-5c46-4ff9-b584-cf31a4c71809. Using Sauce Labs is pretty straightforward. You need to tell it what OS/Browser combination you would like. A list of possible setups can be viewed here. As an example lets suppose we wished to run google chrome version 33 on OSX version mavericks.

require(RSelenium)
pass <- "49953c74-5c46-4ff9-b584-cf31a4c71809" # Your Sauce Labs access key
port <- 80
ip <- paste0(user, ':', pass, "@ondemand.saucelabs.com")
rdBrowser <- "chrome"
version <- "33"
platform <- "OS X 10.9"
extraCapabilities <- list(
name = "RSelenium OS/Browsers vignette first example",
accessKey = pass,
tags = list("RSelenium-vignette", "OS/Browsers-vignette")
)
remDr <- remoteDriver$new( remoteServerAddr = ip, port = port, browserName = rdBrowser, version = version, platform = platform, extraCapabilities = extraCapabilities ) We state the browser and OS we require (chrome 33/ Mac OSX 10.9). The user and password are used to form an appropriate ip address for our remote server (http://rselenium0:49953c74-5c46-4ff9-b584-cf31a4c71809@ondemand.saucelabs.com in this case). They are also passed as extraCapabilities to the remote Selenium server. We give our test a name and any additional tags we wish that are passed to the remote Selenium server. Details of the name and tags are given here. They are used to annotate our tests. #### Basic Example As a basic first example we will run a script using the mavericks/ chrome 33 combination. We run the following commands: testScript <- function(remDr){ remDr$open()
remDr$navigate("http://www.google.com/ncr") Sys.sleep(2) # highlight the query box remDr$findElement("name", "q")$highlightElement() Sys.sleep(2) # goto rproject remDr$navigate("http://www.r-project.org")
# go Back
remDr$goBack() # go Forward remDr$goForward()
Sys.sleep(2)
webElems <- remDr$findElements("css selector", "frame") # highlight the frames lapply(webElems, function(x){x$highlightElement()})

remDr$close() } testScript(remDr) And thats it. We have ran our first remote test using Sauce Labs. The results of the test can be viewed https://saucelabs.com/tests/ae22f859de8746f9bfedad2f49c1c329. I think you will agree its a nice setup. We have access to screenshots of all the commands we issued and a video (screencast) of the test run. We can view the selenium server logs and the medadata associated with our test. ### Testing Multiple OS/Browsers We can easily extend the simple test we ran for multiple OS/Browser combinations. The browser and platform variables need to be assigned the combinations we require. osBrowser <- list( "OS X 10.9" = list( browser = list("safari", "firefox"), version = list('7', '28') ), "Windows 8" = list( browser = list("chrome", "firefox", "internet explorer"), version = list('33', '28', '10') ), "Linux" = list( browser = list("chrome", "firefox", "opera"), version = list('33', '28', '12') ) ) lapply(seq_along(osBrowser), function(x) { platform <- names(osBrowser)[x] lapply(seq_along(osBrowser[[x]]$browser), function(y){
rdBrowser <- osBrowser[[x]]$browser[[y]] version <- osBrowser[[x]]$version[[y]]
remDr <- remoteDriver$new( remoteServerAddr = ip, port = port, browserName = rdBrowser, version = version, platform = platform, extraCapabilities = extraCapabilities ) testScript(remDr) }) }) To view the results you can goto the RSelenium project page on Sauce Labs https://saucelabs.com/u/rselenium. Listed here are all the tests ran on the RSelenium package. A partial search by name Browsers vignette first example will give the results of this test. There are a few repeats of the first Mavericks/ chrome 33 test where I tuned the script. ###### Sauce Labs results for simple test script ## RSelenium with Remote Browsers and Local Sites Testing external webpages and websites across a range of operating systems and browsers can be achieved using Sauce Labs as we observed in the last section. Often however especially in a developement phase of a project we either do not have or do not want an external version of our webpage/website/webapp. A good example would be our shinytestapp. Lets open a new R session and run our testapp. require(shiny) runApp(file.path(find.package("RSelenium"), "apps", "shinytestapp"), port = 3000) To access our app we would require the ip address http://localhost:3000/. How do we access this from a remote webdriver? ### Sauce Connect Thankfully Sauce Labs have a solution for this known as Sauce Connect. Sauce Connect is a secure tunneling app which allows you to execute tests securely when testing behind firewalls via a secure connection between Sauce Labs client cloud and your environment. This allows you to drive an external Browser and have it interact with a local webpage/website/webapp. #### Setting up Sauce Connect Firstly you need to download the Sauce Connect zip for the operating system you are using to run your tests on. This machine I will be testing from is running windows 8.1 so I download the windows zip. I unzipped Sauce Connect to the Documents folder so it is now accessible at C:\Users\john\Documents\sc-4.1-win32. From a windows command prompt we navigate to the Sauce Connect bin directory and run the .exe file supplying our Sauce Labs user name and access key. sc.exe -u rselenium0 -k 49953c74-5c46-4 ff9-b584-cf31a4c71809 ### Basic Example We opened our shinytestapp on port 3000 because Sauce Connect only supports a set number of ports. All ports can be used but for this you need a locally-defined domain name (which can be set in your hosts file) rather than localhost. This is simple to do but for the purposes of this vignette we shall connect to http://localhost:3000. Again to start with we shall use Mavericks with Chrome 33. require(RSelenium) user <- "rselenium0" # Your Sauce Labs username pass <- "49953c74-5c46-4ff9-b584-cf31a4c71809" # Your Sauce Labs access key  port <- 80 ip <- paste0(user, ‘:’, pass, “@ondemand.saucelabs.com”) port <- 4445 ip <- paste0(user, ':', pass, "@localhost") rdBrowser <- "firefox" version <- "26" platform <- "Linux" extraCapabilities <- list( name = "RSelenium OS/Browsers vignette second example", username = user, accessKey = pass, tags = list( "RSelenium-vignette", "OS/Browsers-vignette", "Example 2" ) ) remDr <- remoteDriver$new(
port = port,
browserName = rdBrowser,
version = version,
platform = platform,
extraCapabilities = extraCapabilities
)

Everything is as before the exeception is that when we ask to browse to a localhost address Sauce Connect will intervene. Also we connect to Sauce Labs through Sauce Connect on localhost:4445 by default instead of ondemand.saucelabs.com:80.

localScript <- function(remDr){
remDr$open() remDr$setImplicitWaitTimeout(2000) # wait for elements for 2 seconds
remDr$navigate("http://localhost:3000") Sys.sleep(2) # highlight the labels webElems <- remDr$findElements("css selector", "#ctrlSelect span")
lapply(webElems, function(x) {x$highlightElement()}) Sys.sleep(2) appIds <- c("summary", "distPlot", "ggPlot", "dttable") # Click each checkbox and check for its output 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()
}

localScript(remDr)