Mobile Automation

Introducing Robotium Recorder

Introductory Words from Renas at linkedin
 
We are thrilled to announce the official release of our new product, Robotium Recorder. Robotium Recorder is significant in that anyone in a matter of minutes can create professional test cases, utilizing and taking full advantage of the Robotium framework. It is the result of 4 years of continuous feedback from the testing community and includes cool features like our unique ClickToAssert™ feature that we are certain you will love.

Robotium Tech, our new company, will drive both the development of the Robotium framework and Robotium Recorder going forward. We have a number of exciting ideas and products that we can’t wait to develop and share with you!

To try Robotium Recorder for free go to Robotium.com and follow the installation instructions.

http://www.robotium.com/

We look forward to and welcome any feedback!

Best,
Renas and the Robotium Tech team

Getting Running Services on Android

Put following code snippet into TestProject and get list of all running services over android Phone,

ActivityManager vActivityManager = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);       

        Log.i(“TestingService”, “Total are”+manager.getRunningServices(Integer.MAX_VALUE).size());

        for (RunningServiceInfo vRunningServices : manager.getRunningServices(Integer.MAX_VALUE))
        {                       
            Log.i(“TestingService”, “PackageName: “+vRunningServices.service.getPackageName());
            Log.i(“TestingService”, “ClassName: “+vRunningServices.service.getClassName());
            Log.i(“TestingService”, “ShortClassName: “+vRunningServices.service.getShortClassName());
            Log.i(“TestingService”, “——————————————-“);
        }

Running Robotium Test Project using Ant

Pre-Reqs:

– JDK should be installed with JAVA_HOME evnrionment variable set
– Ant – can be found on Apache
–  Working Robotium Test Project, one can find already desinged sample TestProject here
–  Make sure that all required jars are in libs directory and added from build path setting of Eclipse
– Device/emulator should be connected to pc

Lets Start:

1. In Eclipse workspace having testProject, run the following command to set up Ant for the app project:

android update project -p [TestProject Path]

android tool is used to create/update projects, we need to update our exisiting project. it will generate any required files and folders that are either missing or need to be updated.

2. Next step is to link test project with AUT(Application Under Test). Note that the path given to -m must be the relative path to the project under test, seen from the test project, not the workspace:

android update test-project -m [../ProjectPath] -p [Testproject path]

3. To setup library projects

android update lib-project –target “Google Inc.:Google APIs:17” –path [path]

4. Refresh the projects in Eclipse

5. Test if you can build the app project with

ant clean debug

It should come up with “BUILD Successfull” status

6. Run the last command

ant clean debug install test

It will make all necessary installations and run testProject, see the command line for results. 



How to analyse Memory Availability of Android Testing Project

Both approaches worked for me, please using any of your choice,

Getting Runtime Memory using Runtime

static void checkMemory() {

double vTotalMem = Double.valueOf(Runtime.getRuntime().totalMemory());
double vMaxMem = Double.valueOf(Runtime.getRuntime().maxMemory());
double vAvailableMem = Double
.valueOf(Runtime.getRuntime().freeMemory());
print(“Runtime Memory info: TotalMem=”
+ Math.round(vTotalMem / 1048576) + “mb. MaxMemory”
+ Math.round(vMaxMem / 1048576) + ” mb. AvailableMemory=”
+ Math.round(vAvailableMem / 1048576) + ” mb.”);

}
Getting Memory using ActivityManager 

static void checkMemory() {
              ((ActivityManager)solo.getCurrentActivity()
                        .getSystemService(“activity”)).getMemoryInfo(mi);
              print(“Kernal Memory info: AvailableMemory=”+mi.
                         availMem/1048576+“mb IsMemLow=”+mi.lowMemory);
        if(mi.lowMemory) print(“memory is increasing”);
              else print(“Memory condition is fine”);
 
}

Get number of views on Android screen using Robotium

Please call the respective function to get its number on screen,

ImageViews:

       static int getNoOfImageViews() {
return solo.getCurrentViews(ImageView.class).size();
}
ImageButtons:
static int getNoOfImageButtons() {
return  solo.getCurrentViews(ImageButton.class).size();
}
RadioButtons:
static int getNoOfRadioButtons() {
return  solo.getCurrentViews(RadioButton.class).size();
}
RadioGroup:
static int getNoOfRadioGroup() {
return  solo.getCurrentViews(RadioGroup.class).size();
}
CheckBox:
static int getNoOfCheckbox() {
return  solo.getCurrentViews(CheckBox.class).size();
}
ListView:
static int getNoOfListViews() {
return  solo.getCurrentViews(ListView.class).size();
}
TextViews in a ListView:
static int getNoOfTextViewsInListView() {
return  solo.getCurrentViews(TextView.class,
solo.getCurrentViews(ListView.class).get(0)).size();
}
ImageViews in a ListView:
static int getNoOfImageViewsInListView() {
return  solo.getCurrentViews(ImageView.class,
solo.getCurrentViews(ListView.class).get(0)).size();
}
GridView:
static int getNoOfGridViews() {
return  solo.getCurrentViews(GridView.class).size();
}
ImageViews in a GridView:
static int getNoOfImageViewsInGridView() {
return  solo.getCurrentViews(GridView.class,
solo.getCurrentViews(GridView.class).get(0)).size();
}
ToggleButton:
static int getNoOfToggleButtons() {
return  solo.getCurrentViews(ToggleButton.class).size();
}
Button:
static int getNoOfButtons() {
return  solo.getCurrentViews(Button.class).size();
}
TextViews:
static int getNoOfTextViews() {
return  solo.getCurrentViews(TextView.class).size();

}

How to get all ImageButtons on screen using Robotium

static void getAllImageButtons() {
ArrayList allImageButton = solo
.getCurrentViews(ImageButton.class);
print(“Total ImageButtons:” + allImageButton.size());
for (ImageView vImageButton : allImageButton) {
if (vImageButton.getVisibility() == View.VISIBLE) {
print(“Image Button ID: “ + vImageButton.getId() + “Tag: “
+ vImageButton.getTag().toString() + ” Visibility:”
+ vImageButton.getVisibility() + “View String: “
+ vImageButton.toString());
}
}

}

How to get all ImageViews on screen using Robotium

static void getAllImageViews() {
ArrayList allImageViews = solo
.getCurrentViews(ImageView.class);
print(“Total ImageViews:” + allImageViews.size());
for (ImageView vImageView : allImageViews) {
if (vImageView.getVisibility() == View.VISIBLE) {
print(“Image ID: “
+ vImageView.getId()
+ “Tag: “
+ (vImageView.getTag() != null ? vImageView.getTag()
.toString() : “null”) + ” Visibility:”
+ vImageView.getVisibility() + ” View String :”
+ vImageView.toString());
}
}

}

How to get all ToggleButtons on screen using Robotium

static void printAllToggleButtons() {
ArrayList allToggleButtons = solo.getCurrentViews(ToggleButton.class);
print(“Total ToggleButtons:” + allToggleButtons.size());
for (ToggleButton vToggleButton : allToggleButtons) {
if (vToggleButton.getVisibility() == View.VISIBLE) {
print(“Button ID: “ + vToggleButton.getId() + ” Tag :”
+ vToggleButton.getTag().toString() + ” Value:”
+ vToggleButton.getText().toString() + ” Visibility:”
+ vToggleButton.getVisibility() + ” View String :”
+ vToggleButton.toString());
}
}

}

Running Android TestCase from device

To test Android application using Robotium we generally make a TestProject containing test cases, during building TestProject, it will be compiled & installed on Device/Emulator as an independent application. We have to connect device with computer to compile and install TestProject.
TestProject application can also be used independently after installing, means install once in device, and then we can run our test iterations without connecting device to computer by simply tapping on installed TestProject app same as we use other applications on device.

We can do this by following simple steps.

Introducing Dev Tools
Dev Tools app is one of Android SDK apps by default installed on emulator, it contains many features which are useful to know more about emulator/device like Connectivity shows details about the device connectivity with Internet, Configuration gives details of device configuration.
For the scope of this tutorial we need to work know about Instrumentation.









Android instrumentation is a set of control methods which control an Android component independently of its normal life cycle. They also control how Android loads applications.To know more about instrumentation please click


On tapping Instrumentation feature, it will load below interface showing android.test.InstrumentationTestRunner. It can run any of the test case classes provided by Android and supports all possible types of testing.
On tapping android.test.InstrumentationTestRunner will run any test case available/installed in device. To know more about android.test.InstrumentationTestRunner please click

We need to install Dev Tools on our device to make our TestProject workable same as other applications.

Get Dev Tools App
We will get Dev Tools installable app from emulator.  From eclipse run emulator and open DDMS interface, it will show the running emulator details in Devices section. Select running emulator and from right side section load File Explorer interface (we can add it by clicking (+) icon at bottom left of eclipse if its not already visible).
File explorer will show emulator file system like below.


Expand the system directory, then app it will show the list of all apps available in file system. Find the Developement.apk file in app directory. Development.apk is installable application name of Dev Tools.  
Highlight Development.apk like below

Now we will get out Development.apk file from emulator & save it on our computer.

Click on pull a file from device from top right of File Explorer interface. It will load interface to save file on computer save it at desired directory.


Install Dev Tools on Device
Connect device with computer and from terminal/command prompt run following command to check device
adb devices

If devices is properly connected to computer it should return device information. If not we may need to install device driver or enable USB Debugging mode from Settings > Applications > Development of device

Run following command to install ‘Development.apk’ file on device
adb -d install /Development.apk

It will show success message after successfully installing Dev Tools.
Now we can see the Dev Tools app installed in our device applications list. Load it and get in to the Instrumentation feature as discussed above.

Install & Run TestProject app
Design your test cases in TestProject right click on TestProject > Run as > Android JUnit Test
It will build TestProject and install it in Device(make sure that device is connected with computer 🙂 ).
After successfully done it, we can find our TestProject installed in
Settings > Applications > Manage Applications
To run TestProject load Instrumentation screen from Dev Tools there should be another android.test.InstrumentationTestRunner visible, Tap on it and stay back it will run our TestProject.

Note: It is also possible to get TestProject app visible in applications list but this is not in scope of this tutorial.

How to get all Buttons on screen using Robotium

static void printAllButtons() {
ArrayList
print(“Total Buttons:” + allButtons.size());
for (Button vButton : allButtons) {
if (vButton.getVisibility() == View.VISIBLE) {
print(“Button ID: “
+ vButton.getId()
+ ” Tag:”
+ (vButton.getTag() != null ? vButton.getTag()
.toString() : “null”) + ” Value:”
+ vButton.getText().toString() + ” Visibility:”
+ vButton.getVisibility() + ” View String:”
+ vButton.toString());
}
}

}