Driving OS/Browsers Local and Remote

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.

## $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:

## $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:

## $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:

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

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

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

## $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:

## $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:

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