Android Automated Testing

Setting up Protractor on mobile devices

To run protractor on mobile devices Appium is used as server,

Pre-Requs:
Appium server should be up and running 
Protractor server should be running
Device(Android/iOS) should be connected to PC and accessible through adb

Its above all is Ok, only few changes in conf.js of protractor are required to point its requests appium,

  seleniumAddress: ‘http://localhost:4723/wd/hub’,
    capabilities: {
    platformName: ‘android’,
    deviceName: ‘galaxy s4’,
    browserName: ‘chrome’,
    newCommandTimeout: 60
      },
    baseUrl: ‘http://10.0.2.2:’ + (process.env.HTTP_PORT || ‘8000’),

Some points to understand, 
1. Selenium use port 4444 but to use Appium it should be port 4723 instead of 4444
2. platformName: should be name of env where script will execute, like Andorid/iOS
3. deviceName: should be name of device like s4/iphone 5
4. browserName: should be chrome/safari
5. baseUrl is 10.0.2.2 instead of localhost because it is used to access the localhost of the host machine in the android emulator/device

Sources:
https://github.com/angular/protractor/issues/361
https://github.com/angular/protractor/issues/697
https://github.com/angular/protractor/blob/master/docs/browser-setup.md

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

Selenium WebDriver – Using AdvancedUserInteractions API

The Advanced User Interactions API is more comprehensive API for describing actions a user can perform on a web page such as drag and drop or clicking multiple elements while holding down the Control key.

Actions vMyActions = new Actions(vDriver);
      vMyActions keyDown(Keys.CONTROL)
        .click(someElement)
        .click(someOtherElement)
        .keyUp(Keys.CONTROL);

Then get the action:

   Action vBindAction = vMyActions.build();

And execute it:

   vBindAction.perform();

  • It supports single and composite actions for Mouse and Keyboards
  • It supports most of common browsers including mobile browsers

source

Following are my reusable custom actions, will add more as needed 🙂

 1. MouseMove
Move mouse over an element, usually to get hover

Locatable vElement = (Locatable) vDriver.findElement(by);
Mouse vMouse = ((HasInputDevices) vDriver).getMouse();
vMouse.mouseMove(vElement.getCoordinates());

2. Drag and drop
I need to drag and element on some other element,

vFirtElement = vDriver.findElement(By.id(“id));
vContaingerElement = vDriver.findElement(By.id(“containerid));
new Actions(vDriver). dragAndDrop(vFirtElement, vContaingerElement).build().perform();

3. Drag and Drop By
I need to drag and element from one place to other,

vElementToDrag = vDriver.findElement(By.id(“id”));
new Actions(vDriver).dragAndDropBy(vElementToDrag, 120, 1100).build().perform();

4. Select multiple elements
If I need to check/select multiple list items,

List vList = vDriver.findElements(By .cssSelector(“.class));
Actions vClickMultiple = new Actions(vDriver);
 vClickMultiple.clickAndHold(vList.get(1))
        .clickAndHold(vList.get(2))
        .click();
Action vBindActions = vClickMultiple.build();
 vBindActions.perform();

5. Slide
we can use dragAndDropBy to emulate sliding event

vElementToSlide = vDriver.findElement(By .className(“class”));
new Actions(vDriver).dragAndDropBy(vElementToSlide, 120, 0).build().perform();

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());
}
}

}