Use your business data to your advantage with the help of Syncfusion’s new data science offerings. Discover how a custom big data solution can provide your company with valuable predictions about key market trends.

Mobile Devices

Packt Publishing

Testing web applications using Selenium is made simple with this tutorial. Written for those with no prior experience, it helps you learn through practical exercises and code samples. The perfect entry point to Selenium 2.

Important preliminary points

While you can use the Android emulator for the Android parts of the article, it is highly recommended that you have a real device that you can use. The reason is that the emulator tries to emulate the hardware that phones run on. This means that it needs to translate it to a low-level command that ARM-based devices would understand. A real iOS device is not needed as that simulates a device and therefore is significantly faster. The device will also need to have Android 4.0+ or better known as Ice Cream Sandwich. You will need to download the Android App from http://code.google.com/p/selenium/downloads/list. It will be named android-server-<version>.apk where <version> is the latest version.

You will however need to have a machine with OS X on to start the simulator since it is part of XCode. If you do not have XCode installed you can download it via the AppStore. You will also need to install all of the command-line tools that come with XCode. You will also need to check out the Selenium code from its source repository. You need to build the WebDriver code for iOS since it can't be added to the Apple App Store to be downloaded on to devices.

Working with Android

Android devices are becoming commonplace with owners of smartphones and tablets. This is because there are a number of handset providers in the market. This has meant that in some parts of the world, it is the only way that some people can access the Internet. With this in mind, we need to make sure that we can test the functionality.

Emulator

While it is not recommended to use the emulator due to the speed of it, it can be really useful. Since it will act like a real device in that it will run all the bits of code that we want on the virtual device, we can see how a web application will react.

Time for action — creating an emulator

If you do not have an Android device that you can use for testing, then you can set up an Android emulator. The emulator will then get the Selenium WebDriver APK installed and then that will control the browser on the device. Before we start, you will need to download the Android SDK from http://developer.android.com/sdk/index.html.

Open up a command prompt or a terminal.

Enter cd <path>/android-sdk/tools where <path> is the path to the android-sdk directory.

Now enter ./android create avd -n my_android -t 14 where:

–n my_android gives the emulator the name my_android.

–t 14 tells it which version of android to use. 14 and higher is Android 4 and higher support.

When prompted Do you wish to create a custom hardware profile [no], enter no.

Run the emulator with:

./emulator -avd my_android &

It will take some time to come up but once it has been started, you will not have to restart unless it crashes or you purposefully close it. Once loaded you should see something like the following:

What just happened?

We have just seen what is involved in setting up the Android emulator that we can use for testing of mobile versions of our applications. As was mentioned, we need to make sure that we set up the emulator to work with Android 4.0 or later. For the emulator we need to have a target platform of 14 or later. Now that we have this done, we can have a look at installing the WebDriver Server on the device.

Installing the Selenium WebDriver Android Server

We have seen that we can access different machines and control the browsers on those machines with Selenium WebDriver RemoteDriver. We need to do the same with Android. The APK file that you downloaded earlier is the Selenium Server that is specifically designed for Android devices. It has a smaller memory footprint since mobile devices do not have the same amount of memory as your desktop machine.

We need to install this on the emulator or the physical device that you have.

Time for action — installing the Android Server

In this section, we will learn the steps required to install the Android server on the device or emulator that you are going to be using. To do this, you will need to have downloaded the APK file from http://code.google.com/p/selenium/downloads/list. If you are installing this onto a real device make sure that you allow installs from Unknown Sources.

Open a command prompt or a terminal.

Start the emulator or device if you haven't already.

We need to run the available devices:

<path to>/android_sdk/platform-tools/adb devices

It will look like this:

Take the serial number of the device.

Now we need to install. We do that with the following command:

adb -s <serialId> -e install -r android-server.apk

Once that is done you will see this in the command prompt or terminal:

And on the device you will see:

What just happened?

We have just seen how we can install the Android Server on the server. This process is useful for installing any Android app from the command line. Now that this is done we are ready to start looking at running some Selenium WebDriver code against the device.

Creating a test for Android

Now that we have looked at getting the device or emulator ready, we are ready to start creating a test that will work against a site. The good thing about the Selenium WebDriver, like Selenium RC, is that we can easily move from browser to browser with only a small change. In this section, we are going to be introduced to the AndroidDriver.

Time for action — using the Android driver

In this section we are going to be looking at running some tests against an Android device or emulator. This should be a fairly simple change to our test, but there are a couple of things that we need to do right before the test runs.

Open a command prompt or terminal.

We need to start the server. We can do this by touching the app or we can do this from the command line with the following command:

Run the test. You will see that it runs the same test against the Android device.

What just happened?

We have just run our first test against an Android device. We saw that we had to forward the HTTP traffic to port 8080 to the device. This means that the normal calls, which use the JSON Wire Protocol, will then be run on the device.

Currently Opera Software is working on getting OperaDriver to work on Mobile devices. There are a few technical details that are being worked on and hopefully in the future we will be able to use it.

Mozilla is also working on their solution for Mobile with Selenium. Currently a project called Marionette is being worked on that allows Selenium to work on Firefox OS, Firefox Mobile for Android as well as Firefox for Desktop. You can read up on it at https://wiki.mozilla. org/Auto-tools/Projects/Marionette.

Have a go hero — updating tests for Android

Have a look at updating all of the tests that you would have written so far in the book to run on Android. It should not take you long to update them.

Running with OperaDriver on a mobile device

In this section we are going to have a look at using the OperaDriver, the Selenium WebDriver object to control Opera, in order to drive Opera Mobile. Opera has a large market share on mobile devices especially on lower end Android devices.

Before we start we are going to need to download a special emulator for Opera Mobile.

As of writing this, it has just come out of Opera's Labs so the download links may have been updated.

Time for action — using OperaDriver on Opera Mobile

To make sure that we have the right amount of coverage over the browsers that users may be using, there is a good chance that you will need to add Opera Mobile. Before starting, make sure that you have downloaded the version of the emulator for your Operating System with one of the links mentioned previously.

What just happened?

We have just seen what is required to run a test against Opera Mobile using OperaDriver. This uses the same communication layer that is used in communicating with the Opera desktop browser called Scope.

We will see the mobile versions of web applications, if they are available, and be able to interact with them.

If you would like the OperaDriver to load up tablet size UI, then you can add the following to use the tablet UI with a display of 1280x800 pixels. This is a common size for tablets that are currently on the market.

If you want to see the current orientation of the device and to access the touch screen elements, you can swap OperaDriver object for OperaDriverMobile. For the most part, you should be able to do nearly all of your work against the normal driver.

Working with iOS

iPhones and iPod Touches are such commonplace these days. A lot of companies are working hard to offer a really good experience for these users. This means that users are starting to become accustomed to using web applications with their phones.

We can run our tests against the simulator or against the real device. Compared to Android, the simulator is really quick. This is because it is not trying to emulate the hardware of actual Apple devices.

Time for action — setting up the simulator

In this section, we are going to be making sure that we have the simulator or device ready. To do this we will need to do the following:

If you want to build it for the simulator, set your build configuration to Simulator/ iPad OS 5.0 / iWebDriver. This is done in a drop-down box in the top-left of the project window.

Click Build& Go! iWebDriver will be built and the simulator will start. You can see what it will look like in the following screenshot:

What just happened?

We have just got all the requirements ready to start writing our first test. After making sure that we have XCode, which has the iOS SDK, we were able to start the simulator and have iWebDriver installed.

We will now have a look at how to set up running your tests against a real device. Before we do that, we are going to have to make sure that we have set up a provisioning profile.

To do this we need to do the following. One thing to note is that you will have to pay US$99 to join the iOS program. To do this:

Go through the Provisioning Assistant and complete all the steps that you are asked to do:

Time for action — setting up on a real device

Setting up tests to run on a simulator is quite useful but having the tests running on a mobile device can be really useful. Let us have a look at setting this up on a real mobile device:

You will also need a provisioning profile from Apple to be installed and configured for your device.

Open Info.plist and edit the Bundle Identifier to com.NAME.$ {PRODUCT_NAME:identifier} where NAME is the name you registered your provisioning profile to be an authority on.

Make sure your device is connected to your computer. Your device must also be routable from your computer. The easiest way to do this is to configure a wifi network and connect your device to it.

Click Build & Go. iWebDriver will be installed on the device.

What just happened?

We have just installed iWebDriver on a real device. We can now run our tests against iPhones or iPads. The hard part in running tests against these devices is now done. Let's have a look at updating our tests.

Creating a test for iOS devices

Now that we have looked at getting the device or simulator ready, we are ready to start creating a test that will work against a site. The good thing about the Selenium WebDriver, like Selenium RC, is that we can easily move from browser to browser with only a small change. In this section, we are going to be introduced to the iPhoneDriver.

Time for action — using the iPhone driver

In this section, we are going to be looking at running some tests against an iOS device or simulator. This should be a fairly simple change to our test but there are a couple of things that we need to do right before the test runs.

Now we are ready to update our test. I will show an example from the previous test:

Run the test. You will see that it runs the same test against an iOS device.

What just happened?

We have just seen how we can run our tests against iOS devices. Depending on which simulator we start XCode from, we can either have it run against iPhone or iPad.

Have a go hero — updating tests for iOS Devices

Have a look at updating all of the tests that you would have written so far in the book to run on iOS. It should not take you long to update them.

Summary

We learnt a lot in this article about using Selenium WebDriver with mobile devices. We saw that after a little setup of the device and the machine running the test, it was fairly easy to get up and running.

Specifically, we covered:

Working with Android: In this section we had a look at what is needed to set up Android for testing with Selenium WebDriver. We set up an emulator in case we didn't have a real device. We also saw how we can install the Android Server on the device or emulator.

We then moved on to creating our test that ran against the emulator or the device.

Working with iOS: In this section we looked at setting up the simulator or getting iWebDriver installed on a real device.

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.