Categories: ProgrammingTutorials

Mobile Devices

10 min read

(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 1280×800 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.

Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago