Mobile Devices

Exclusive offer: get 50% off this eBook here
Selenium 2 Testing Tools: Beginner’s Guide

Selenium 2 Testing Tools: Beginner’s Guide — Save 50%

Learn to use Selenium testing tools from scratch with this book and ebook.

$26.99    $13.50
by David Burns | January 2013 | Beginner's Guides Open Source

We are currently seeing an explosion of mobile devices to the market. A lot of them are more powerful than your average computer was just over a decade ago. This means that in addition to having nice clean, responsive, and functional desktop applications, we are starting to have to make sure the same basic functionality is available to mobile devices. In this article, we are going to be looking at how we can set up mobile devices to be used with Selenium WebDriver.

In this article by David Burns, author of Selenium 2 Testing Tools Beginner's Guide, we shall learn:

  • How to use the stock browser on Android

  • How to test with Opera Mobile

  • How to test on iOS

(For more resources related to this topic, see here.)

So let's get on with it...

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.

  1. Open up a command prompt or a terminal.

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

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

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

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

  1. Open a command prompt or a terminal.

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

  3. We need to run the available devices:

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

  4. It will look like this:

  5. Take the serial number of the device.

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

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

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

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

  1. Open a command prompt or terminal.

  2. 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:

    adb -s <serialId> shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity

  3. We now need to forward all the HTTP traffic to the device or emulator. This means that all the JSON Wire Protocol calls, that we learnt earlier, go to the device. We do it with:

    adb -s <serialId> forward tcp:8080 tcp:8080

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

    import junit.framework.TestCase; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.android.AndroidDriver; public class TestChapter7 { WebDriver driver; @Before public void setUp(){ driver = new AndroidDriver(); driver.get("http://book.theautomatedtester.co.uk/chapter4"); } @After public void tearDown(){ driver.quit(); } @Test public void testExamples(){ WebElement element = driver.findElement(By.id("nextBid")); element.sendKeys("100"); } }

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

Windows: http://www.opera.com/download/get.pl?id=34969⊂=true&nothanks=yes&location=360.

Mac: http://www.opera.com/download/get.pl?id=34970⊂=true&nothanks=yes&location=360.

Linux 64 Bit: Deb: http://www.opera.com/download/get.pl?id=34967⊂=true&nothanks=yes&location=360.

Tarball: http://www.opera.com/download/get.pl?id=34968⊂=true&nothanks=yes&location=360.

Linux 32 Bit: Deb: http://www.opera.com/download/get.pl?id=34965⊂=true&nothanks=yes&location=360.

TarBall: http://www.opera.com/download/get.pl?id=34966⊂=true&nothanks=yes&location=360.

Let's now see this in action.

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.

  1. Create a new test file. Add the following code to it:

    import junit.framework.TestCase; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; public class TestChapter7OperaMobile{ WebDriver driver; }

  2. What we now need to do is add a setup method. We will have to add a couple of items to our DesiredCapabilities object. This will tell OperaDriver that we want to work against a mobile version.

    @Before public void setUp(){ DesiredCapabilities c = DesiredCapabilities.opera(); c.setCapability("opera.product", OperaProduct.MOBILE); c.setCapability("opera.binary", "/path/to/my/custom/opera-mobile-build"); driver = new OperaDriver(c); }

  3. Now we can add a test to make sure that we have a working test again:

    @Test public void testShouldLoadGoogle() { driver.get("http://www.google.com"); //Let's find an element to see if it works driver.findElement(By.name("q")); }

  4. Let's now add a teardown:

    @After public void teardown(){ driver.quit(); }

  5. Your class altogether should look like the following:

    import junit.framework.TestCase; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; public class TestChapter7OperaMobile{ WebDriver driver; @Before public void setUp(){ DesiredCapabilities c = DesiredCapabilities.opera(); c.setCapability("opera.product", OperaProduct.MOBILE); c.setCapability("opera.binary", "/path/to/my/custom/opera-mobile-build"); driver = new OperaDriver(c); } @After public void teardown(){ driver.quit(); } @Test public void testShouldLoadGoogle() { driver.get("http://book.theautomatedtester.co.uk"); } }

  6. And the following should appear in your emulator:

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.

c.setCapability("opera.arguments", "-tabletui -displaysize 1280x800");

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.

Selenium 2 Testing Tools: Beginner’s Guide Learn to use Selenium testing tools from scratch with this book and ebook.
Published: October 2012
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

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:

  1. If you haven't checked the Selenium Code out, follow the steps at http://code.google.com/p/selenium/source/checkout.

  2. Open selenium/iphone/iWebDriver.xcodeproj in XCode.

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

  4. 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:

  1. Get a developer account from Apple. This is done at https://developer.apple.com.

  2. Create a certificate signing request.

  3. Open Keychain Access:

  4. Request a Certificate from a certificate authority by doing Keychain Access |Certificate Assistant | Request a Certificate From a Certificate Authority:

  5. Complete the form as show in the following screenshot:

  6. Click Continue and then save the file to somewhere that you will be able to access it:

  7. Click Continue and then it should say that it was successful as shown in the following screenshot:

  8. Go to the iPhone Developer Program Portal on https://developer.apple.com.

  9. Launch the Assistant as in the following screenshot:

  10. 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:

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

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

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

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

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

    import junit.framework.TestCase; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.iphone.IphoneDriver; public class TestChapter7 { WebDriver driver; @Before public void setUp(){ driver = new IPhoneDriver(); driver.get("http://book.theautomatedtester.co.uk/chapter4"); } @After public void tearDown(){ driver.quit(); } @Test public void testExamples(){ WebElement element = driver.findElement(By.id("nextBid")); element.sendKeys("100"); } }

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

Resources for Article :


Further resources on this subject:


Selenium 2 Testing Tools: Beginner’s Guide Learn to use Selenium testing tools from scratch with this book and ebook.
Published: October 2012
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

About the Author :


David Burns

David Burns is a Senior Developer in Test having worked with Selenium for quite a few years. He is a Selenium Core Committer and so he knows and understands what users and developers want from the framework.

Books From Packt


 Processing 2: Creative Programming Cookbook
Processing 2: Creative Programming Cookbook

 Python Testing Cookbook
Python Testing Cookbook

Agile Web Application Development with Yii1.1 and PHP5
Agile Web Application Development with Yii1.1 and PHP5

 Yii 1.1 Application Development Cookbook
Yii 1.1 Application Development Cookbook

 Selenium 1.0 Testing Tools: Beginner’s Guide
Selenium 1.0 Testing Tools: Beginner’s Guide

 Selenium Testing Tools Cookbook
Selenium Testing Tools Cookbook

 LWUIT 1.1 for Java ME Developers
LWUIT 1.1 for Java ME Developers

 Responsive Web Design with HTML5 and CSS3
Responsive Web Design with HTML5 and CSS3


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2
1
j
S
2
Z
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software