---
title: "Driving OS/Browsers Local and Remote"
output:
  html_vignette:
    toc: yes
vignette: >
  %\VignetteIndexEntry{Driving locally and remotely with RSelenium}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

## 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.

```R
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-fledged 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](http://chromedriver.storage.googleapis.com/2.9/chromedriver_win32.zip) 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:\Users\john\Documents. 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:

```R
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](http://selenium-release.storage.googleapis.com/index.html). The current release is 2.40. The system I am running is 64 bit so we download the [64bit version](http://selenium-release.storage.googleapis.com/2.40/IEDriverServer_x64_2.40.0.zip). For simplicity we again place this in our Documents directory namely C:\Users\john\Documents. 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:

```R
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](http://filehippo.com/download_safari/). 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:

```R
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](http://www.opera.com/download/guide/?os=windows&ver=12.16&local=y). I had some issues getting this to run YMMV however. The first issue was getting the following error message

```R
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](https://github.com/operasoftware/operadriver/blob/master/src/com/opera/core/systems/runner/launcher/OperaLauncherRunner.java) 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 doesn't pass arguments to java atm)

```batch
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. 

```R
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](http://code.google.com/p/selenium/wiki/OperaDriver). 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%\Opera\opera.exe or more precisely C:\Program Files\opera\opera.exe. As I have installed the 64bit version I need to tell opera driver where to look

```R
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)
1. Render the content into dom, eg executing javascript inside the script tag. and the executed result will be reflected on the browsers dom.
1. Render the dom into visualised content.

A headless browser handles items 1 and 2 but doesn't carryout 3. This means it doesn't 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](http://phantomjs.org/download.html). We are using windows so we downloaded [ phantomjs-2.1.1-windows.zip](https://bitbucket.org/ariya/phantomjs/downloads/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](https://github.com/detro/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:\Users\john\Desktop\phantomjs-1.9.7-windows. Once your operating system can find `phantomjs.exe` or the equivalent driving a phantomjs browser is as easy as:

```R
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:

```R
remDr$navigate("http://www.google.com/ncr")
remDr$screenshot(display = TRUE)
```

<h6 align = center>RSelenium on win 8.1 driving phantomjs</h6>

<img src="https://res.cloudinary.com/rselenium/image/upload/v1537203294/tmpScreenShot.png"  title = "RSelenium on win 8.1 driving phantomjs" style = "margin-left: auto;margin-right: auto; display: block;"  width = '100%'/>

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](http://code.google.com/p/selenium/wiki/AndroidDriver#REMOVED_FROM_THE_PROJECT).

As can be noted driving android using the selenium server has been deprecated in favour of the [selendroid project](http://selendroid.io/mobileWeb.html). 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](http://appium.io/docs/en/about-appium/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:\Program Files\Java\jdk1.7.0_51 or something similar. You can also check the version of java and it should indicate `Java(TM) SE Runtime Environment`. 

```batch
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](http://www.oracle.com/technetwork/java/javase/downloads/java-se-jre-7-download-432155.html). Once the JDK is installed the environment variable JAVA_HOME should be set.

```batch
echo %JAVA_HOME%
```

```
C:\Program Files\Java\jdk1.7.0_51
```

##### SDK

Another Development kit needs to be installed [SDK](http://developer.android.com/sdk/index.html) 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](http://projects.spring.io/spring-android/#quick-start). The environment variable `ANDROID_HOME` needs to be set. This should be set to the /sdk/ directory in the extracted bundle.

```batch
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:

<h6 align = center>Android SDK on win 8.1</h6>

<img src="https://res.cloudinary.com/rselenium/image/upload/v1537203294/androidsdk.png"  title = "Android SDK on win 8.1" style = "margin-left: auto;margin-right: auto; display: block;"  width = '100%'/>

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](http://software.intel.com/en-us/android/articles/installation-instructions-for-intel-hardware-accelerated-execution-manager-windows). 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](http://selendroid.io/setup.html#androidDevices) 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.

<h6 align = center>Android AVD on win 8.1</h6>

<img src="https://res.cloudinary.com/rselenium/image/upload/v1537203294/androidavd.png"  title = "Android AVD on win 8.1" style = "margin-left: auto;margin-right: auto; display: block;"  width = '100%'/>

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

<h6 align = center>Android AVD my_avd on win 8.1</h6>

<img src="https://res.cloudinary.com/rselenium/image/upload/v1537203294/androidmy_avd.png"  title = "Android AVD my_avd on win 8.1" style = "margin-left: auto;margin-right: auto; display: block;"  width = '100%'/>

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.

<h6 align = center>Android AVD my_avd on win 8.1</h6>

<img src="https://res.cloudinary.com/rselenium/image/upload/v1537203294/androidrunavd.png"  title = "Android AVD my_avd on win 8.1" style = "margin-left: auto;margin-right: auto; display: block;"  width = '100%'/>

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](http://selendroid.io/) has a link at the bottom to the most recent [jar](https://github.com/selendroid/selendroid/releases/download/0.8.0/selendroid-standalone-0.8.0-with-dependencies.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.

```batch
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.

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

<h6 align = center>Android with RSelenium on win 8.1</h6>

<img src="https://res.cloudinary.com/rselenium/image/upload/v1537203294/androidRSelenium.png"  title = "Android with RSelenium on win 8.1" style = "margin-left: auto;margin-right: auto; display: block;"  width = '100%'/>

### iOS

Testing iOS requires a Mac. The setup is similar to selendroid. [Appium](http://appium.io/docs/en/about-appium/getting-started/?lang=en) and [ios-driver](http://ios-driver.github.io/ios-driver/?page=setup) 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 [Sauce Labs](https://saucelabs.com/) and [Browser Stack](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](https://saucelabs.com/opensauce). On the [account page](https://saucelabs.com/account) 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](https://saucelabs.com/platforms). As an example lets suppose we wished to run google chrome version 33 on OSX version mavericks. 

```R
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")
rdBrowser <- "chrome"
version <- "33"
platform <- "OS X 10.9"
extraCapabilities <- list(
  name = "RSelenium OS/Browsers vignette first example",
  username = user,
  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](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](https://wiki.saucelabs.com/display/DOCS/Test+Configuration+Options). 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:

```R
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 that's it. We have ran our first remote test using Sauce Labs. The results of the test can be viewed [https://saucelabs.com/tests/ae22f859de8746f9bfedad2f49c1c329](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.

```R
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](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. 

<h6 align = center>Sauce Labs results for simple test script</h6>

<img src="https://res.cloudinary.com/rselenium/image/upload/v1537203294/saucelabsOSBrowsers.png"  title = "Sauce Labs results for simple test script" style = "margin-left: auto;margin-right: auto; display: block;"  width = '100%'/>

## 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 development 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. 

```R
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](https://saucelabs.com/docs/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](https://saucelabs.com/downloads/sc-4.0-latest-win32.zip) 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. 

```batch
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.

```R
require(RSelenium)
user <- "rselenium0" # Your Sauce Labs username
pass <- "49953c74-5c46-4ff9-b584-cf31a4c71809" # Your Sauce Labs access key 
```
~~port <- 80~~<br>
~~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(
  remoteServerAddr = ip,
  port = port,
  browserName = rdBrowser,
  version = version,
  platform = platform,
  extraCapabilities = extraCapabilities
)
```

Everything is as before the exception 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`.

```R
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)
```
