Friday, September 23, 2011

Automating a Native android app using Selenium and NativeDriver

As there are more and more development happening on the android front, automation of the developed applications has also become a necessity. Android within its API supports a testing framework which can be used for testing applications, but writing and developing cases using that API is not easy. We may need a framework that makes it easy to write and develop test cases for our applications.
One of such framework is "NativeDriver". It is built over the Selenium webdriver automation framework.
As you must be knowing that selenium is vastly used open-source functional automation tool. 
In this write-up I will tell you on how to automate an android application using NativeDriver
Following things are required before starting:
  • Android SDK 2.2 or later - download
  • Eclipse version 3.5 or later (Eclipse IDE for Java Developers recommended) - download
  • Android Development Toolkit (ADT) plug-in - Installing ADT
  • Ant - download
  • JDK 1.6 or later - download
Building the NativeDriver libraries:
  • Checkout the NativeDriver code:
  • svn checkout https://nativedriver.googlecode.com/svn/trunk nativedriver --username {Google account e-mail address}
  • Build the NativeDriver code:
  • $ cd nativedriver/android
    $ ant

The libraries are built and stored in the nativedriver/android/build directory:

  • server-standalone.jar - this should be linked to your Android application, and runs on the Android device (or emulator). This is the NativeDriver server, and listens for requests to automate the application, such as “start an activity” or “click a UI element”
  • client-standalone.jar - this should be linked to the test, which is the NativeDriver client. It implements a WebDriver API which communicates with the server.

Adding the NativeDriver jar to your application



  • Import your android application code into the eclipse by using the File -> Import functionality of eclipse.
  • Add the server-standalone.jar to the build-path of your android application by Right clicking on the Android application project -> Configure build path -> Add jars or Add external jars (browse to the server-standalone.jar file, select and add it).
  • To the application AndroidMainfest.xml file, add the following to the element.

 <instrumentation android:targetPackage="{app_package_name}"
   android:name="com.google.android.testing.nativedriver.server.ServerInstrumentation" />
   android:name="android.permission.INTERNET" />
   android:name="android.permission.WAKE_LOCK" />
   android:name="android.permission.DISABLE_KEYGUARD" />
           Here {app_package_name} needs to be replaced with the name of the package as specified in the mainfest element's package attribute.
  • Build the application and install it on the device.
  • Now go to the commandline and start the instrumentation using the following line:
adb shell am instrument {app_package_name}/com.google.android.testing.nativedriver.server.ServerInstrumentation
Here again the {app_package_name} needs to be replaced with the name of the package as specified in the mainfest element's package attribute.
  • Enable port forwarding by using the following command:
adb forward tcp:54129 tcp:54129

Writing your Test Cases
For writing your test cases you just have to create a new Java project and start writing code for it. I will explain this using a code example. The following code is done to automate an opensource K9 mail application on android:


Important Notes:-

  • As there is no recording feature available, we need to write the code manually for automating our scenarios. 
  • For identifying the element id to interact on the UI, you can get the ID's of the elements from the R.java file generated inside your android application code.
  • Also as we are writing our cases in Java code we can use our own test framework and generate html reports for out automation execution.


Note: Most of the material in this blog has been taken from the original site of the NativeDriver. The credit for such material goes to the original author.
Following is the link to it. http://code.google.com/p/nativedriver

80 comments :

Aakash Thadani said...

Hi,

I get this error when I try to run "ANT" to build a library.

Error: \build.xml:29: Compile failed; see the compiler error output for details.

Do you know what could be an issue?

Varun Menon said...

What is there at line 29 of your build.xml file?
As it is saying its most probably some compilation error. Is there any other message that is thrown on the terminal?

Aegis Software said...
This comment has been removed by a blog administrator.
Varun Menon said...

@Unkown:Your appreciations are welcomed but advertising is not.This blog is not to advertise your company. Please dont post any links or info abt any company in my blog. I will be deleting any such comments if posted.

vamsi voora said...

Hi it is very useful code for my project.can you help me how to do testing to switch between the tabs in android

Varun Menon said...

@Vamsi - wht kind of tabs are u talking abt? Is it a native add or a web-app?

vamsi voora said...

Its a native app

Varun Menon said...

@vamsi- In case the tabs have names u can use the name of the tab to find the element and clicking on them.

vamsi voora said...

But my application has images for tabs.So is there any method to click on image

Varun Menon said...

@Vamsi- Try using sikuli..

Surya said...

Varun, is sendKeys working for you? I am testing a simple signin activity. which has two edittext boxes one for username and other for password.
After entering data for username(via sendkeys), click() on password doesn't work as I noticed that the focus still stays on username. Due to this the password is also entered in the username box.
Below is the simple logic

WebElement uName = driver.findElement(By.id("user_name_autocompletetextview"));
uName.clear();
uName.sendKeys("user1");


WebElement passwd = driver.findElement(By.id("password_edittext"));
passwd.click();
passwd.clear();
passwd.sendKeys("val");

WebElement ele = driver.findElement(By.id("signin_btn"));
ele.sendKeys(AndroidKeys.ENTER);

Varun Menon said...

@Surya - Yes I am using it, and it works fine. You are doing it correctly selecting the element and then sending keys.
If while typing username the keyboard is still visible. Try to hide it by simulating enter or back in such cases.And then move forward with entering password.

manju said...

hi Varun using this nativedriver can i automate native android applications like (Phone app).

Varun Menon said...

nativedriver is meant for automating native android/iphone application only. Sorry I dont have an idea abt "Phone app".

Varun Menon said...

Hi all,
Please take a look at "bot-bot" automation tool. More details can be found at the following link.

http://blog.varunin.com/2012/02/native-android-application-automation.html

Varun

Surya said...

Varun, for me it never works, the password is also entered in username field.
Looks like this issue is posted in forums.
http://groups.google.com/group/nativedriver-users/browse_thread/thread/17806d716995f1ca?pli=1

But wondering how you did not get into this issue.
I noticed the first time when I installed the app, the signin flows works as desired from the second time on it fails.

Varun Menon said...

@Surya - Thanks for the link. I had faced the said problem. But before entering the value. I do an element.click and then enter the text.

Milind Warade said...

Hi @Surya, Varun: I am also getting same problem.

WebElement Fullname = driver.findElement(By.id("name"));
Fullname.click();
Fullname.clear();
Fullname.sendKeys("Milind Warade");
//Start spinner activity
startSpinnersActivity();
// Select DOB
AndroidNativeElement starsSpinner
= driver.findElement(By.id("dobBtn"));
starsSpinner.click();
WebElement month = driver.findElement(AndroidNativeBy.text("Feb"));
month.click();
month.clear();
month.sendKeys("Aug");
WebElement date = driver.findElement(AndroidNativeBy.text("21"));
date.click();
date.clear();
date.sendKeys("28");
WebElement year = driver.findElement(AndroidNativeBy.text("2012"));
year.click();
year.clear();
year.sendKeys("1980");
WebElement set = driver.findElement(AndroidNativeBy.text("Set"));
set.click();
//Set Gender
AndroidNativeElement gender
= driver.findElement(By.id("genderBtn"));
gender.click();
gender.click();
WebElement gender1 = driver.findElement(AndroidNativeBy.text("Male"));
gender1.click();
//Set Resident
AndroidNativeElement resident
= driver.findElement(By.id("residentBtn"));
resident.click();
resident.click();
WebElement resident1 = driver.findElement(AndroidNativeBy.text("Yes"));
resident1.click();
//set email address
WebElement email = driver.findElement(By.id("emailId"));
email.click();
email.clear();
email.sendKeys("xyz@yahoo.co.in");
//set cell phone
WebElement cellphone = driver.findElement(By.id("cellphone"));
cellphone.click();
cellphone.clear();
cellphone.sendKeys("9988774455");
driver.findElement(By.id("okBtn")).click();

It does not focus on email address.
I have also used
driver.findElement(By.id("emailId")).click();
//set email address
before email.
then also it does not work.

Jonny said...

hey Varun, can you help?

this senario works ok.
AndroidNativeElement resident = driver.findElement(By.id("residentBtn"));
resident.click();

but if its more than a click i need i get in to trouble.

this is the error i get
java.lang.NoClassDefFoundError: android/webkit/WebView
on this line
WebView webView = (WebView)driver.findAndroidNativeElements(AndroidNativeBy.id("myWebview"));

Varun Menon said...

I think the the class WebView is not present in any of your classpath jars. You need to check which jar contains this class and whether it has been added or not.

Varun Menon said...

Also tell me "more than one click" on the same element or a different element or the same screen.

Jonny said...
This comment has been removed by the author.
Jonny said...

Hi, WebView is present, its in import android.webkit.*; which is in the android 4.0.3.jar.
the project builds ok, its just when it runs i get the problem

what i want to do with the WebView is to test the url, as in -

WebView webView = (WebView) driver.findAndroidNativeElements( AndroidNativeBy.id("myWebview"));
string myUrl = webView.GetUrl();

i get the same problem with any control. eg if i try to get hold of an AlertDialog.

Anonymous said...

Hi Varun
I was new to selenium.I have a small doubt ,while Compose a gmail to friend i was not able to getting compose button id,i tried with firebug,IDE,Xpather ,and css ,but i was not able to click on the compose button.Can,anyone try out this and please let me know.Hope i will get response soon.

Anonymous said...

I am having issues building the native driver., i am getting compilation error. Any pointers

Surya said...

I'm facing "ClassNotFoundException?: com.google.android.testing.nativedriver.server.ServerIns? trumentation" while starting instrumentation - adb shell am instrument {app_package_name}/com.google.android.testing.nativedriver.server.ServerInstrumentation?.

Please post if anyone fixed the issue.

Varun Menon said...

@Surya-Hopefully Its on windows system. We had recently identified this issue in windows system. We are tried to find a solution for it but was unable to. This problem is wrt adb and windows.

Varun Menon said...

@Rama- what kind of errors?

QA said...

Does anyone has idea on iPhone native driver pls share

Thanks

Manish said...

Hi Varun,

I am doing R&D for Android Native Application testing but my limitation is to use C#, can we do same thing using C# as scripting language, if yes then how?

Thanks.Manish Singh

Varun Menon said...

@manish - Cant help on that. But according to me it may not be possible as android uses java as the base language for development. Any automation framework/tool you take internally uses the APIs provided by android which are written in Java.

Unknown said...

Hi Varun,

I am new for this so have couple of query ,it can be basic but please help me in that.

1) Why are we copying project from SVN ,It is just for Demo or mandatory for Native driver.

2)If i have Android project example calculator and i am able to config the same in eclipse without ant than how will get native driver jar files.

3)As per the blog,we have to create the java project to write the code so where will make the changes in android maifest file because the project that we are copying through SVN is Android project so we can make changes there.

Thanks in Advacne

Varun Menon said...

@naveen - Following are the answers to your queries:

1) SVN is required to checkout the nativedriver code to generate the jar.
2) I dont think nativedriver jars are available for download. Hence you need its source code to generate one.
3) Its clearly mentioned that the changes needs to be made to application AndroidManifest File(i.e. app under test). New Java project needs to be created for writing the test-cases.

Unknown said...

Hi Varun,

Thanks for the same ,A humble request , Can you share your mobile number on saxena.nicky@gmail.com ..i need your precious 15 mins ..Its really helpful for me.

your 15 mins will solve my all issue..

Ashrafunisa said...

hi Varun
I tried the procedure as u said but i got . connection reset errror

Varun Menon said...

GOt ur mail and error log.
Did u started the instrumentation server in your android?

Nitish said...

HI Varun
Pls Guide. I Want run Selenium webdriver Test In android emulator.
I Had Already created android 2.3.3 Emulator and also configure my eclipse for android .
But whenever I run my code Error Occurs. So If u have any reference code and different things one must do before run the test ,then let me know

Thanks
Nitish

Jayanth Vijayakumar said...
This comment has been removed by the author.
Jayanth Vijayakumar said...

Hi varun,

Help me in this.

I gave the adb command as follows for my package name com.rn.idine.SplashActivity

adb shell am instrument com.rn.idine.SplashActivity/com.rn.idine.SplashActivity.server.ServerInstrumentation

But its throwing INSTRUMENTATION_FAILED

Whether my command line is right?

Nayan Tejani said...

hi Varun,
got a few conceptual queries..
Can you please share your cell number with me on nayantejani@gmail.com..
have tried quite a few varieties to automate .. using android device, emulator..
but all in vain... i could achieve launching the browser on android ..however the test fails on subsequent steps..
somehow i m now getting confused about differences in approaches for automating android native projects, automating web application on android devices / emulators..

highly appreciate your help and guidance..

Nayan Tejani said...

hi,

Appreciate your help n guidance in this regards.
M trying to automate mobile based web application.
I tried to start up with a simple site like http://www.google.com
Have configured Selenium Webdriver pack & also Android SDK in eclipse.
Using Android v2.2 API
Installed android-server-2.21.0.apk on my android device connected to my Windows XP desktop thru a USB connection.
configured an APN in my mobile device to use the proxy which my host machine using for Internet access.

Created a simple Java Project in eclipse and below is the code

android-server-2.21.0

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.AndroidDriver;
public class Testwebdriver{
public static void main(String[] args)throws InterruptedException, InvalidFormatException, IOException {
AndroidDriver driver = new AndroidDriver();
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Android forums");
driver.findElement(By.name("q")).submit;
}
}

M facing below issues :-
1. When i execute the code, the first step is executed properly and my mobile device launches the google page in a browser.
but when it comes to next steps it throws UnreachableBrowserException:
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.

Also my mobile device flashes a message something like : the Application webdriver has stopped unexpectedly.


2. another thing i discovered is that for launching the given page, my mobile device is using my mobile service providers internet data access.
however, it should have used the Internet access through my PC (host to which it is connected)

3. when i try using an Emulator, even the page is not launched and my emulator continues to show the blank screen which is displayed when we launch the WebDriver..

have ensure proper port forwarding for both real device and my emulator

adb -s S56608c8bf06d forward tcp:8080 tcp:8080 -> S56608c8bf06d is serial # of my real device
adb -s emulator-5554 forward tcp:8080 tcp:8080 -> emulator-5554 is emulator

if anybody has come across & resolved this kinda issue, kindly share the resolution steps.
Appreciate your help..

Regards,
Nayan

Varun Menon said...

It should be the following:

adb shell am instrument com.rn.idine.SplashActivity/com.google.android.testing.nativedriver.server.ServerInstrumentation

Varun Menon said...

Sorry but I cant share my contact details.
Regarding your query:
The automation approach for each kind of android app Native / Web / Hybrid have to be different.
You can automate Native / Hybrid app using "bot-bot" or some other frameworks available in the market.
For Web based application automation you can use Selenium webdriver.

Varun Menon said...

No idea on this as I havent worked much on Selenium webdriver + Android.

Jayanth Vijayakumar said...

Thanks varun

vinod said...
This comment has been removed by the author.
vinod said...

Hi,
I got an error when i am trying to run using ant to build.

Error: android\sample-aut\simplelayouts\android\build.xml:58: Compile failed; see the compiler error output for details.

Varun Menon said...

What is the compiler error? It may have printed in the console.
Also I dont suggest Native driver to be used now, as its development is stopped.

vinod said...

ERROR :: android\sample-aut\simplelayouts\android\src\com\google\android\testing\nativedriver\server\AndroidNativeDriver.java:46: cannot find symbol
[javac] symbol : class ActionChainsGenerator
[javac] location: package org.openqa.selenium.interactions
[javac] import org.openqa.selenium.interactions.ActionChainsGenerator;

okeyy if native driver is not used then how can we automate android apk files??

Varun Menon said...

You can use bot-bot developed by imaginea.
Or if you are interested writing code you can use Robotium.

vinod said...

automating android apk is possible using selenium ?? other than Native Driver ??

Sivaprasad Kandula said...

for this native driver source code is required ?

Varun Menon said...

To generate the jar.. Yes..

rgoyal said...

Thank you for sharing the steps. I followed all the steps above, however now I am stuck at following error in logcat. please help ...
Java.lang.RuntimeEcxeption: Unable to instantiate instrumentation ComponentInfo {com.example.test_snd/com.google.android.testing.native.driver.server.ServerInstrumentation}: java.lang.ClaaNotFoundException

my code are:
public class test{
private AndroidNativeDriver driver; //Defining the driver object of AndroidNativeDriver

@Before
public void beforeTest() { // Before method that will get the driver object from getDriver()
driver = getDriver();
}

@After
public void afterTest() { //Runs after the test is run and shutdown the driver.
driver.quit();
}

// Build/start the Android driver and returns it.
protected AndroidNativeDriver getDriver() {
return new AndroidNativeDriverBuilder()
.withDefaultServer()
.build();
}

private void startActivity() {
driver.startActivity("com.example.test_snd.MainActivity");
}

@Test //test function
public void mytest(){
startActivity();
driver.findElement(AndroidNativeBy.text("Hello World"));
System.out.println("The text is: " + driver.findElement(AndroidNativeBy.text("Hello World")).getText());
}
}

Varun Menon said...

You may need to include the jar as part of the build by importing it in the window where build path is configured.

Arvind Singh said...

Hi Guys ,
Could u please let me know the process to automate google playstore using this tool ,is it possible to automate native apps ?if yes then please mention basic steps I mean In eclipse->new->project->....?

Arvind Singh said...

Hi Varun,
I read your all answers,those are quite interesting .I need your guidance please,I want to automate google android play store native application .How it is possible using nativedriver or you can suggest another tool to automate android native app .
and for Nativedriver please let me know the steps from starting in eclipse bcz m new for this actually .I am waiting for your reply .
--
Thanks
Arvind

Unknown said...

Varun,
As you mentioned earlier (April 2, 2012) instrumentation problem is present between wrt adb and windows. Any recent updates on the same, because we are also facing "Unable to instantiate instrumentation ComponentInfo..." issue while doing some POC with native driver. Appreciate your inputs in advance.

Thanks,
Mahmudul

Unknown said...

Hi Varun,
I am working on manual mobile testing now.
I want to learn 'Automating a Native android app using Selenium and NativeDriver '.Can you pls suggest me how to learn and is it possible for you to provide any online class and course and how much i have to pay and all details.

Thanks,
Vijay

Varun Menon said...

Sorry for the late reply.
Can you check Robotium out. Also most of the automating tools need source code of the respective app to be available. Please make sure you have the source code of the app available as it makes a lot of things easy. If you want to learn android automation, get the source code of any small opensource app and try automating it.

Varun Menon said...

Hi,

I dont conduct any training or online sessions. Also this Im doing out of interest and dont charge anything for the suggestions.
Basics on how to start automation u can get from google, but if face any issues for which you are unable to find a solution you can ask me.

Varun

Dilip12c said...

Hi Varun,

I'm planing to automate iPhone web apps with WebDriver, here I have some doubts.
1. Can we develop scripts on windows and run it on iPhone?
2. If we develop scripts only on Mac (iOS), can we write the script in JAVA or only on Objective C?

I appreciate your help in this matter. Thanks in advance.

Mark Walsh said...

Nice blog mate. However there are some things worth mentioning.

1) The step where we use svn to download the native driver is no longer working
vn checkout https://nativedriver.googlecode.com/svn/trunk nativedriver --username {Google account e-mail address}

Access is refused even though I am entering the right password.

2) Development of Android Native Driver has been ceased as of June 2013.
However mature the driver is now will be as good as it gets.

Where to now with automating native apps (aside from Robotium)?

Unknown said...
This comment has been removed by a blog administrator.
Varun Menon said...

@Lee Wright - Appreciate your feedback, but please do not post links and advertise in my blog.

Thanks
Varun

Unknown said...

Hi I am new to this Native Apps

Can u tel me exactly from where i can get the AndriodNativeDriver jar
I am not able to find the jars

I am using Windows8 Eclipse(Java EE Deve)

Thanks in Advance

Varun Menon said...

Nativedriver is not in active development any more and I will not suggest it to be used for automation. You can try with Robotium instead

Unknown said...

I followed the steps as mentioned by you.

After typing the commads on cmd:
adb shell am instrument com.google.android.testing.nativedriver.simplelayouts/com.google.android.testing.nativedriver.server.ServerInstrumentation

and adb forward tcp:54129 tcp:54129

if i check logcat and search for "JETTY is started on port 54129".
But i am not able to found anything.
and even if i click on a test case and run as JUNIT test then i get an error "software caused connection abort".

Could you please let me know whats wrong and why i am not getting to see any logs after i do adb forward.

Varun Menon said...

Nativedriver is not under development please try to use some other api like Robotium for automation

Unknown said...

i resolved the issue. because the internet was not on my mobile. so after i switched internet on, everything is working like a charm.

i have already automated web application with webdriver and now we were launching android app so i thought in the same automation project i will add one more suit for android app testing.
have u personally used android native driver for functionality test like login, sign up etc? and when u say that try using robotium, is the api somewhat similar to webdriver and can it be used with Java.

Varun Menon said...

I havent personally used native driver on a project. But I still wont suggest you to use it. There are few newly released WebDriver based apis like Appium and Selendroid available in the opensource market that can used to automate Native android applications.

Unknown said...

Hi Varun,

Thanks for the suggestions. I was going through Appium and Selendroid and also Robotium. One drawback for Selendroid and Robotium is that they can only automate android apps and appium can be used to automate android and ios both.

Do you have any suggestions on the above mentioned tools, have u used any of them or could you please redirect me to somewhere, where i can find more comprehensive list of Pros and Cons of all these.

Thanks in advance!!

Varun Menon said...

Selecting a tool depends upon your automation requirement and your understanding of the api chosen. I have worked with Nativedriver and Robotium but not with Apium or Slendroid as of now.

Unknown said...

I am planning for small PoC of robotium and Appium. Will update the forum once i am done with both.

Unknown said...

Hi Varun,
As Android Webdriver application starts a Jetty server automatically when launched, can we also do this for using Android Native Driver with any native app ?

Anonymous said...

Hi Varun,

Please can you tell me how can i Hit Done button from my andoid keyboard ?

Unknown said...

driver.findElement(By.name("Done")).click()

Anonymous said...

Thanks for reply but Done is not web page button i am asking for android done keyboard button .

Appreciate for your reply.

Unknown said...
This comment has been removed by the author.
Unknown said...

Hi ,

I had a scenario where i have a text & an option of "Copy To Clipboard" so i have to click on "Copy to clipboard" button and paste the text into another textbox in selendroid(Java).

Can you please suggest me how to paste the text into another textbox as early,

Post a Comment