Skip to content

Testing Mobile Apps Locally

MatthewWalker42 edited this page Mar 28, 2014 · 16 revisions

Mobile applications can be difficult things to test. A lot of applications must be tested on a variety of phones and tablets with greatly varying specs and operating systems. To get the most accurate test results, one would have to have a vast collection of mobile devices that most people can't afford and most companies deem unnecessary. Thankfully, emulators exist that allow for virtually any major mobile device to be virtually recreated. There is also plenty of software that allows for automated application testing on both real devices and emulators. This page will discuss how to use an emulator to test your mobile application through SharpSauce.

Installing Android SDK

In order to first get started testing on an emulator, you must have some way to compile your application code and run an emulator. I would suggest Using Android Studio, just because that is what I am most familiar with, and it is fairly painless to work with. If you prefer eclipse, or another compiler, and already have it installed, then you can skip to the next section. For everyone else, follow this [link](https://github.com/simnova/webdevdocs/wiki/Installing-PhoneGap-and-Android-Studio-on-Windows) to get instructions for installing Android Studio.
If you successfully completed the instructions, you should now have android studio installed on your machine. You can use this to compile your code through the Build -> Rebuild Project menu options. Once you have built an application it should appear somewhere in an out -> production -> android folder structure. It will be a .apk file.

Installing Appium

Appium is the way I choose to test my mobile applications locally. SharpSauce can work in conjunction with Appium to automate tests on a local emulator. To do this, we must first install Appium on our machines. For those of you that followed the instructions in the link above, installing Appium is easy. Simply open your command prompt, (make sure you have admin rights) and enter:
`npm install -g appium`
That is it. Appium is now installed on your machine. If you did not follow the link above, please look at it for directions on how to install Node to your computer. Once Node is installed, you can enter the command above.

Installing the Application on an Emulator

First, make sure that your emulator is running. Then, using your command prompt, enter the command:
`adb install Path\To\Application\myApplication.apk`
The application should automatically appear on the emulator. You should now be able to open it up and use it through mouse clicks and typing on a keyboard. However, in order to test this app with Appium and SharpSauce, you will need to uninstall the application. This is because Appium installs the application itself when you run the test, and if it is already installed, Appium won't be able to open the app because of mismatched IDs. As long as your app was installed using Appium, you won't have to uninstall it. In order to uninstall the app, you should enter the command:
`adb uninstall Application.Package`
"Application.Package" is the package name of your application. It is usually in the format of "com.Something" or "com.Something.else".

Testing the App Through Appium and SharpSauce

With the application not installed on the emulator, you can open up your test in SharpSauce and enter the command (in the command prompt):
`appium &`
Make sure that the port 4732 is open on your machine, as that is what Appium will use. Now if you run your test in SharpSauce, Appium will install your application, and run the test automatically. Your test script should not look too different from a standard script. The main difference will be that you will need to use a `driver.SwitchTo().Window("WEBVIEW")` command before you can run any other webdriver command. This line only applies if you are using a webview application.

TroubleShooting Problems

  • The test can't find elements in a hybrid app
    If you are trying to test a hybrid app, then you will likely need to have this line before you try to find any elements on your app:
`driver.SwitchTo().Window("WEBVIEW");`
What this line does, is it sets the webdriver to expect webelements. If your app is being built through cordova or phonegap, this will allow you to test it. Note that "driver" is the name of my RemoteWebDriver variable, you may have a different name.
  • How do Capabilities Differ Between Remote and Local?
    The simple answer is that the device capabilities don't differ too much from remote to local. You may notice that local test have more capabilities than remote tests. The only capability where the difference truly matters is the device capability. For example, if you are using Sauce Labs to test an Android hybrid app, you will want to set the device capability to "Selendroid". However, to do the same test locally, you would need to set the device capability to "Android".
  • Which Web Driver Should be Used for Local Testing?
    In order to test an app locally, you will need to use the RemoteWebDriver object. If you wish to test remotely using Sauce Labs, you will want to use the SauceLabsDriver object. The difference between the two is that the SauceLabsDriver can do everything the RemoteWebDriver can, but the RemoteWebDriver can't do everything the SauceLabsDriver can. I should clarify. Technically the RemoteWebDriver can do everything the SauceLabsDriver can, but it requires the user to have more code. For instance, using the SauceLabsDriver, you can select dropdown values in a single line of code. Whereas the RemoteWebDriver takes three lines of code to accomplish the same task. However, the SauceLabsDriver can only be used if you are connecting to Sauce Labs for testing.

Clone this wiki locally