Showing posts with label Robotium. Show all posts
Showing posts with label Robotium. Show all posts

2013-06-25

Tips-Tricks in Robbotium(Android Automation Framework)

In this article we are going to see some tecniques that can be applied in robotium to use it smartly. This is not a complete post, I will add more tips & tricks as the time goes. For basic ideas on robotium, please see my Mobile Testing section. Details , step by steps posts are there.

Sample test template :

I have created two projects for same test case where both are two ways to test same app(one with code, and another is without code). The concept is, there will be a base class maintaining all communication with android test runner. And all test classes will extend that class . All test classes will only contain test methods where as the base class will contain the main android unit test extension , setup, tear down. Here is a screenshot for more details.
Project with source code
Project without source code

How to run Robotium Test Case?
-Run Emulator or device
-Check that the device is connected using "LogCat" /"Device from eclipse or using adb commands in command prompt.  In "LogCat" or"Device", we can see device responses.
-Install debug version of app if you are using apk file to test(no source codes)
-Right click the the package you want to run in side eclipse(or the whole project)
-Select run as Android JUnit Test. If it is not in here, select Run Configuration , and use android junit test.
-Now , we see Emulator activity from log cat and test will be executed. In eclipse, we will see junit runner describing the test progress & results.

Debugging from Emulator and Eclipse : 
- Use "LogCat" and "Device" in eclipse to see app's activity. Spatially what it is doing or interacting. screenshot.
To add "LogCat" or "Device, from eclipse, go to window > show view . You will find device and logcat. If not , click other and you will see full list. Screenshot.
- Try to avoid filter in log cat and use verbose type logcat view so that you can see detail activity of emulator . Spatially test application's activity and responses from android system . If you need to monitor specific activity , then use filter.
- From device, we can do a lot of advance and interesting stuffs like
a. Method profiling : We can see a method's resource using , run time and all other information.
b. Dump View Hierarchy : We can dump view Hierarchy for automated UI tests.
c. Take screenshot : We can take screen shot any time we want. It will help for identifying bugs.
d. Capture system wide trace :


Thanks...to be continued...:) 

2013-06-19

Android APK(No Source Code) Test in Robotium

In this article we are going to see how can we write unit tests with Robotium Framework when we do not have the source code. That means we are going to test an application using its installer apk file.

Please read my previous post for robotium setup basic


In this post we will only see the test Class used for testing stand alone APK (not with code). So, please see my this post for code start basic. I will mainly focus on test class here.

To test a stand alone application, we need to have debug version of APK file. To convert an apk to debug version, see my this post.

Like as following this post, we will not add dependency project. In stead of this step, we have to install the application in the emulator.
And add android test project in eclipse with robotium library. Now add a class(APKTestCase).

So, after adding a test class( my example class name is APKTestCase ), we will extend from ActivityInstrumentationTestCase2 without parameter(as we don't have codes, so we do not know)
So, my test class will be.

public class APKTestCase extends ActivityInstrumentationTestCase2{
}And the constructor 
public APKTestCase() throws ClassNotFoundException{
        super();
}



This constructor needs class name as parameter. So, how to get the class name ? We have to be very careful here. While testing with robotium, only activity name is not enough , we have to specify the class name. The screen shot is showing how to define the class name with pacakage name. This is called full class name.
So, if we do not have source code, so how can we get the class name. This is a bit tricky.
-Run eclipse and open an emulator.
-Run your installed application(if not installed, then install it first using ADB commands, use my this post)
-From eclipse, see log cat messages in no filtered mode.(usually in verbose)
After starting activity, system process shows in text section.
you can see the message like this <activity neme>/.<Class Name> : +<time> . See the screen shot
Here is the screen shot

So our full class name will be. com.example.android.notepad.NotesList
Now, we have to convert this class name into class by an identifier. To do this we will use a spatial class named as Class to identify. We will use it as static as it is accessing static class name. So for this part , our code will be
private static final String Launcher_Activity_ClassName  = "com.example.android.notepad.NotesList";   
private static Class launcherActivityClass;
static{
        try{
            launcherActivityClass = Class.forName(Launcher_Activity_ClassName);
        }catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }        

}
And the constructor will become
public APKTestCase() throws ClassNotFoundException
    {
        super(launcherActivityClass);

}

Like as standard test case, we need to override setup & tear down. So, finally class will be

@SuppressWarnings("unchecked")
public class APKTestCase extends ActivityInstrumentationTestCase2{  
    private Solo mySolo;
    private static final String Launcher_Activity_ClassName  = "com.example.android.notepad.NotesList";  
    private static Class launcherActivityClass;
    static{
        try{
            launcherActivityClass = Class.forName(Launcher_Activity_ClassName);
        }catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }      
    }

    public APKTestCase() throws ClassNotFoundException
    {
        super(launcherActivityClass);
    }   
    @Override
    public void setUp() throws Exception
    {
        mySolo = new Solo(getInstrumentation(), getActivity());
    }
    @Override
    public void tearDown() throws Exception
    {
        mySolo.finishOpenedActivities();

}

Now we are ready for writing test case. We can add any sample test case. You may see a sample project link in here.

Thanks...:)

2013-06-17

How to write test case in Robotium with source code

In this article we are going to see how can we write unit test case for a android application. We will discuses about a small demo example with detail explanation. The objective of the post is to have clear idea on how to get started with robotium uni test case writing for a invoice.

First we need a project which we will test.We can download a notepad project which is used for learning purpose with robotium from here. We will get two projects there, one project containing application code base another one is robotium test case. Here is the link of the same project from my drive. We will use the code base.We will add the code base to eclipse. To do that, just import the downloaded code base. In eclipse, From, File >Import > Select Archive file under General > show the downloaded ZIP file and finish the process.

To setup robotium environment, see my this post. So, we will start from adding a android test project.[ As all previous steps are shown in my post]

After adding a test project, we get a package. We can edit the package names from android manifest in case we want to change the package name(for test project). Here is the screen shot.

Now, we have to add target package in current application manifest. To do that,
-Click AndroidManifest.xml
-Click Instruments tab
-Click the android.test.InstrumentationTestRunner
-From Target Package, provide the target package name.
(from this example : com.example.android.notepad)
-Screenshot
Now save this. We can do the same thing by adding this
 <instrumentation android:targetPackage="com.example.android.notepad" />
directly to AndroidManifest.xml
Screenshot

So,after renaming I have made my project as com.notepad.test this package and target package is com.example.android.notepad. Screen shot

Now, we need to include the main project(which we will test) as dependent of our test project. So that, when we will run the test case, the application apk will be installed in the test environment.  To do that.
-Right click on test project and select properties
-Click Java build path
-Select project tab and click add
-Select NotePad project and press ok
Screenshot

So Now, Add a class under a package of src folder .
After a class creation , extend the class from base class of " ActivityInstrumentationTestCase2 ".
So what is this "ActivityInstrumentationTestCase2" ?
This is a unit test case template(base class) that provides all controlling over a app activity for unit testing. For more detail you may see the declaration. We need to override setup and tear down functions. For those who are new at unit testing, please see junit or nunit unit test case basic. I have an article on nunit.

When extending , we will use our target activity class name(assuming that we have source code) as parameter. In here, we have to be careful as wrong class name will cause error while execution. That means, we have to choose the functions to test. From this example, we are going to test adding/deleting/editing notes. So, from code we have check which class is active(actively responsible) for those functions. in here Notelist class is responsible for showing all notes and it creates or handle all types of note operating events. So, we need this class as parameter while extending "ActivityInstrumentationTestCase2". My testing class is TestNoteAdministration So the code will be
public class TestNoteAdministration extends ActivityInstrumentationTestCase2<NotesList>{
}

-And add the constructor.
public TestNoteAdministration() {
   super(NotesList.class);        

}
This is spatial constructor for test case. This actually calls super class's constructor with test class activity as parameter. When the test run, this activity get initiated.

-Add a Solo Class's object. This is our main robotium functional test operator object. We have to initialize in setup.
private Solo mySolo;

-Now we need to override Setup and Teardown Methods.

@Override
 public void setUp() throws Exception {
        mySolo= new Solo(getInstrumentation(), getActivity());
}

In here , getInstrumentation used for initializing settings from manifest and getActivity initiates the test activity.

 @Override
 public void tearDown() throws Exception {
        mySolo.finishOpenedActivities();

}
This will close all the operating activity.

Now we are ready to write a test case. Like as a Junit test method, we will write the methods. Example -
public void AddNoteTest() throws Exception {
//implement 
}

A sample test case with the test project will be found in this link. Like as importing note pad project, you may import and see the test code.

Note:
- We will see how not to use parameter in separate post. And, I will try to provide some sample code in separate post that I have worked with different projects.
- Before running, you may fix android properties. To do that , right click the project > android tools > Fix Project Properties.

...Thanks..:)

2013-06-16

Setting up Robotium in Eclipse

In this article, we are going to see how to install Robotium In Eclipse.We will use robotium for android application testing using code base or stand alone apk. I will provide separate post for how to code. Let's make environment ready.

Step 1. Install android SDK. See my this post.

Step 2. Download Robotium : Download latest robotium and java doc from this link . As we will perform detail analysis on robotium, you may download the source from this github link. I use zipped source.

Step 3 : Adding Robotium to the project : In this step, we will see how to create robotium project. Robotium is an extended Android JUnit package. So,
-Run Eclipse
-From file > New >Project
-Select Android Test Project [Under Android Folder, with Junit logo]
 Screenshot Link 
-Add a project name(ex-ShantonuTestRobo)
-Select This Project(not under any existing project)
-Select an android version( I choose 4.1.2) and click Finish. So we will find like this.

Now , we have to add downloaded library. For this, right click on project and select properties.
 -From property window, select Java Build Path.
-Click Libraries
-Click Add external JARs and show the downloaded path of robotium solo jar.
ScreenShot
-You may add Java Doc and downloaded source files. It will help advance unit testers.
Screenshot for JavaDoc
Screenshot for Source Adding.

Step 4 : Add a class under package of the source file . We will see two ways to write robotium unit test cases. I will post separately with codes.

Now we are ready environment to start coding with Robotium for Unit testing.

Note : To prevent java.lang.NoClassDefFoundError export the robotium library. To export any library go to project properties > java build path > order and export . Now select robotium and press ok. 

Thanks...:)

2013-05-29

How to search items in Robotium?


In this article we are going to see how to search a Button or a text at an Android device while performing Unit Testing using robotium framework.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

For Buttons : 
-To search a button with a specific text. It will scroll automatically.
solo.searchButton("ButtonToSearch");

-To search a button with a specific text and only if it is visible on the screen(true for visible).  It will scroll automatically if needed.
solo.searchButton("ButtonToSearch", true);

-To search a button with a specific text and minimum number matching (integer). It will scroll automatically if needed.
solo.searchButton("ButtonToSearch", 2);

-To search a button with a specific text ,minimum number matching (integer) and Visibility(true if it is visible on the scvreen). It will scroll automatically if needed. 
solo.searchButton("ButtonToSearch", 2, true );


-To search a Toggle button with a specific text. It will scroll automatically.
solo.searchToggleButton("ButtonToSearch");


-To search a Toggle button with a specific text and minimum number matching (integer). It will scroll automatically if needed.
solo.searchToggleButton("ButtonToSearch", 2);   

For Text : 
-To search a specific text in a text area(Edit text object). It will scroll automatically if needed. 
solo.searchEditText("SearchText");

-To search a specific text in full display. It will scroll automatically if needed.
solo.searchText("SearchText");

-To search a specific text in full display with Visibility(true to search in visible area on screen). It will scroll automatically if needed.
solo.searchText("SearchText", true);

-To search a specific text in full display with minimum number matching (integer) . It will scroll automatically if needed.
solo.searchText("SearchText", 2);

-To search a specific text in full display with minimum number matching (integer) and Scrolling option(true to enable scroll).
solo.searchText("SearchText", 2,true);


-To search a specific text in full display with minimum number matching (integer) ,Scrolling option(true to enable scroll). solo.searchText("SearchText", 2, true, true);

Note :
-All of these functions returns true if the search item is found, false on not found. So, it is useful for validating items on the screen.
-All text that are used as parameter, will be handled as Regular Expression searching.
-In all Match number,  0 means one or more expected matching

Thanks...:)

How to validate items in Robotium?

In this article we are going to see how to validate different items at an Android device while performing Unit Testing using robotium framework. Basically we will check whether the item present or not.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

Check Box : 
-To validate(true/false) a with specific Index whether it is checked or not. 
solo.isCheckBoxChecked(0);
-To validate(true/false) a with specific text of the item whether it is checked or not. 
solo.isCheckBoxChecked("lebelText");

Radio Button :
-To validate(true/false) a with specific Index whether it is selected or not. 
solo.isRadioButtonChecked(0);

-To validate(true/false) a with specific text of the item whether it is selected or not.  
solo.isRadioButtonChecked("lebelText");
 
Spinner Menu / Sub Menu :
-To validate(true/false) if the specific text is selected in any Spinner in screen.So, it returns true if that text is present
solo.isSpinnerTextSelected("lebelText");

-To validate(true/false) if the specific text is selected in indexed (0 based) Spinner in screen. We are just specifying the spinner here.
solo.isSpinnerTextSelected(0, "lebelText");

Toggle Button : 
-To validate(true/false) a with specific Index whether it is checked or not. 
solo.isToggleButtonChecked(0);

-To validate(true/false) a with specific text of the item whether it is checked or not.
solo.isToggleButtonChecked("lebelText");


Text based compound button :

-To Check the specific Text is checked or not. It is used with checked text box or compound button.
solo.isTextChecked("lebelText");


Note : As all are validation functions , so each function returns Boolean value(true/false). So we can use them for conditional purpose.

...Thanks...:)

How to insert text / type in Robotium?

In this article we are going to see how to insert a text or data using at an Android device while performing Unit Testing using robotium framework. Android handles those keys like as soft key board.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

-To enter a text to a specific editable text area (EditText object) . In here myEditText is the EditText item
solo.enterText(myEditText, "InputString");

-To enter a text to a index(0 based) of a editable text area 
solo.enterText(1,"MyString");

-To enter a text in a specific web element . Use any one By method to get web element.
solo.enterTextInWebElement(By.id("uid"), "shantonu");

Like as Entering Text,, we can type the text also. The basic difference, it will keep a delay as well as use different event in android.

-To type a text in a specific editable text area (EditText object)
 solo.typeText(myEditText, "StringToType");

-To type a text to a index(0 based) of a editable text area
solo.typeText(0, "StringToType");

-To enter a text in a specific web element . Use any one By method to get web element.
solo.typeTextInWebElement(By.id("uid"), "StringToType");


-To enter a text in a specific web element object. myWebElement is a web element object.
solo.typeTextInWebElement(myWebElement, "StringToType");


-To enter a text in a specific web element and index(0 based) of matches. Use any one By method to get web element.
solo.typeTextInWebElement(By.id("uid"), "StringToType", 0);

Thanks..:) 

How to drag in Robotium?

In this article we are going to see how to Drag an Item in robotium. Android provides seperate event for dragging.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

-To drag( Simulate touching) from a specified location to a new location.
solo.drag(0, 240, 0, 400, 10);

In here , parameters
a. From position (X coordinate) of initial touch
b. To position (X coordinate) of the drag destination
c. From position (Y coordinate) of initial touch
d. To position (Y coordinate) of the drag destination 
e. Step counter(integer) indicating the number of steps to drag. Step count is the number performing the drag operation in to number of steps. Ex- it we move (0,0) to (240,400) , we can make it in 10 steps or 240 steps. If we make 10 steps, dragging will be (0,0) to (

Thanks..:) 

2013-05-28

How to long press in Robotium?

In this article we are going to see how to click /press long. It is a spatial event like as clicking. Android provide separate event handler for long press. Ex- when we press on screen long time(3s+), the menu comes.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

-To Long click on specific list line of a text view. It returns an ArrayList of the TextView objects (contains the lines). It will get the first ListView that it finds.
solo.clickLongInList(0);

-To Long click on specific list line of a text view and the index. It returns an ArrayList of the TextView objects (contains the lines).
solo.clickLongInList(1,0);

-To Long click on specific list line of a text view , the index and the amount of time (long press time in millisecond integer) . It returns an ArrayList of the TextView objects (contains the lines).
solo.clickLongInList(2, 1,5);

-To Long click on specific coordinate X, Y. This coordinate is the resolution of the screen.Ex-480x800, here X=480(max) and Y=800(max) in portrait mode.
solo.clickLongOnScreen(240, 400);

-To Long click on specific coordinate X, Y and the amount of time (long press time in millisecond integer)
solo.clickLongOnScreen(240,400,5);

-To Long click on a View/Web Element displaying a specific text. It will automatically scroll.
solo.clickLongOnText("TextToClick");

-To Long click on a View/Web Element displaying a specific text and the index for multiple match. It will automatically scroll.
solo.clickLongOnText("Regix", 0);

-To Long click on a View/Web Element displaying a specific text , the index for multiple match and scroll Boolean value . Provide true when if we need to enable scrolling.
solo.clickLongOnText("Regix", 0, true);

-To Long click on a View/Web Element displaying a specific text , the index for multiple match and the amount of time (long press time in millisecond integer)
solo.clickLongOnText("Regix", 0, 5);

-To Long click on specific text and then selects an item(index) from the context menu that appears. It will automatically scroll.
solo.clickLongOnTextAndPress("Regix", 1);

-To Long click on specific view. In here myView is a view object.
solo.clickLongOnView(myView);

-To Long click on specific view and the amount of time (long press time in millisecond integer)
solo.clickLongOnView(myView, 5);

Thanks...:)

How to scroll/ nevigate in Robotium?

In this article we are going to see how to scroll on the screen in robotium. Basically, this is nevigation over items.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

-To scroll down to screen.
solo.scrollDown();
It returns false if not possible(end of screen)

-To scroll down to specific list (AbsListView). In here mylist is a AbsListView item
solo.scrollDownList(myList);
It returns false if not possible(end of screen)

-To scroll down to specific index(zero based) in a ListView
solo.scrollDownList(0);
It returns false if not possible(end of screen)

-To scroll to bottom of the screen
solo.scrollToBottom();      

-To scroll to bottom of the list (AbsListView). In here mylist is a AbsListView item
solo.scrollListToBottom(myList);
It returns false if not possible(end of screen)

-To scroll to bottom to specific ListView index(zero based)
solo.scrollListToBottom(1);      
It returns false if not possible(end of screen)

-To scroll a specified AbsListView and Line (integer)
solo.scrollListToLine(myList, 1);

-To scroll a AbsListView item with specific index and specified line. Both integer
solo.scrollListToLine(1,1);      

-To scroll Horizontally. We can provide two values as parameter, solo.RIGHT or solo.LEFT
solo.scrollToSide(solo.RIGHT);

-To scroll Horizontally to a specific AbsListView item.
solo.scrollViewToSide(myView, solo.LEFT);

Note : We can provide two values as parameter, solo.RIGHT or solo.LEFT

-To scroll to top of the screen
solo.scrollToTop();
      
-To scroll Up. It returns false if not possible(top of screen)
solo.scrollUp();

-To scroll Up to specific AbsListView item
solo.scrollUpList(myList);

-To scroll Up a list view to specific Index(Zero Based)
solo.scrollUpList(1);

-To scroll the list to the top in a specific AbsListView item.
solo.scrollListToTop(myList);
It returns false if not possible(top of screen)

-To scroll the list to the top in a list view index(Zero Based)
solo.scrollListToTop(0);
It returns false if not possible(top of screen)

...Thanks...:)

How to click an item in Robotium?

In this article we are going to see how to click different type of items using Robotium.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

For List items:
- To click a specific list line and returns an Array List of TextView objects. .

solo.clickInList(0);
Parameter is the Line Number to click. Will use the first ListView of it finds.


- To click a specific list line and returns an Array List of TextView objects. .
solo.clickInList(1,0);
Parameter is the Line Number and the List Index to click.

For Buttons : 
-To  click Home Button
solo.clickOnActionBarHomeButton();

-To click the Action Bar Item with specific id( as integer )
solo.clickOnActionBarItem(1);


-To click Button that matches the Id( as integer )
solo.clickOnButton(1);

-To click Button that matches the text
 solo.clickOnButton("Delete");
It will use scrolling automatically

-To click on an image Button with specific index(zero based Integer)
solo.clickOnImageButton(0);

-To click a toggle Button that matches the text
solo.clickOnToggleButton("Enable");

-To click on an Radio Button with specific index(zero based Integer)
solo.clickOnRadioButton(0);

-To click Check Box that matches the Id( as integer )
solo.clickOnCheckBox(0);


For View or Web Element : 
-To click a View /WebElement item that showing a specific text. It will automatically scroll if needed.
solo.clickOnText("ClickHere");


-To click a View /WebElement item that showing a specific text and matching index.
solo.clickOnText("Regix", 0);

-To click a View /WebElement item that showing a specific text, matching and scrolling mode
solo.clickOnText("Regix", 0, true);

Note : 
-The text parameter is handled as Regix matching
-Matching Index will define which one to click on multiple objects found.
-Scrolling true means, it will scroll.

-To click a specific view 
solo.clickOnView(myView);

-To click a specific view without wait(immediately)
solo.clickOnView(myView, true);

-To click a web element with a specified object(using By)
solo.clickOnWebElement(By.id("Id"));

-To click a web element with a specified object(using By) and matching index.
solo.clickOnWebElement(By.id("Id"), 0);

-To click a web element with a specified object(using By) ,matching index and scrolling selection(true/false). If true, scrolling will be performed
solo.clickOnWebElement(By.id("Id"), 0, true);

-To click a specific web element
solo.clickOnWebElement(myWebElement);

There are some other items which can be present for click event. Some are clicked in following ways.

-To click on current EditText item with specific index(zero based Integer)

solo.clickOnEditText(0);

-To click on an image view item with specific index.(zero based Integer)
solo.clickOnImage(0);

-To click on screen position with X coordinate and Y coordinate .
solo.clickOnScreen(240,400);
X an Y coordinate calculated from top upper corner of screen. For example if you have a cell phone of 480x800 resolution. When it is in portrait mode, x value max 480, Y value max 800.

Thanks..:)

2013-05-27

How to delete text in Robotium?

In this article we are going to see how can we clear text from a android item or a web element with robotium framework.
After Initiating solo, we will mainly two type asserts

-To clear text from current edit text item(in here myEditText)
solo.clearEditText(myEditText);        

-To clear text from currently active edit text item's index(zero based)
solo.clearEditText(0);

-To clear text from a web element specified by a locator(i used Xpath, you may use others, see all By options from this post)
solo.clearTextInWebElement(By.xpath("XpathOftheItem"));
       
Thanks...:)

What are the asserts in Robotium(build in)?

In this article we are going to see the build in asserts methods spatially come with robotium framework. These are not predefined Java asserts, these are from solo object specified form mobile.

After Initiating solo, we will mainly two type asserts

1. Memory checking : This asserts the available memory is not low by the system. Actually, it checks whether the current system memory is running low or not.
solo.assertMemoryNotLow();

Actually this is the implementation of this :
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();        ((ActivityManager)activityUtils.getCurrentActivity().getSystemService("activity")).getMemoryInfo(mi);
Assert.assertFalse("Low memory available: " + mi.availMem + " bytes!", mi.lowMemory);

2. Activity Checking : This assert is spatially for checking current activity. It has 4 overloaded methods to get this checking in 4 ways. We assume current activity = NoteEditor

-To check current activity with specified message and name
solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor");


-To check current activity with specified message ,name and new instance or not.
solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor", true);

-To check current activity with specified message and Class (extracted from Name)
solo.assertCurrentActivity("Expected NoteEditor activity",NotesList.class);

-To check current activity with specified message ,Class (extracted from Name) new instance or not.
solo.assertCurrentActivity("Expected NoteEditor activity", NotesList.class, true);

Note :  The Boolean value checks whether it is new activity or not where we use this.

Thanks..:) 

2013-05-20

How to find a webelement or webelements in Robotium?

In this article we will see the functions used for getting a web element from a web application using Robotium while doing testing.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

For Single web element : 

Solo object have a function named  solo.getWebElement(), where we use two parameter to get element. First parameter is for element finder(By object) and 2nd one is the index(nth found), So
-The function returns a web element
-We need to specify By object( the object finder)
-We need to specify index. (Zero based integer index and in here 0 means found 1)

-To select a web element by its class name(Actually it is the type name defines what type of web element that is, like button, text box etc)
solo.getWebElement(By.className("Textbox"),0);

-To select a web element by its css selector
solo.getWebElement(By.cssSelector("CSSAsString"),0);

-To select a web element by its ID
solo.getWebElement(By.id("idAsString"), 0);

-To select a web element by its  Name
solo.getWebElement(By.name("NameAsString"), 0);

 -To select a web element by itsTag Name
solo.getWebElement(By.tagName("tagNameAsString"), 0);

-To select a web element by its text content
solo.getWebElement(By.textContent("contentAsString"), 0);

-To select a web element by its Xpath
solo.getWebElement(By.xpath("xpathAsString"), 0);

For Multiple web element : 

There are two function,

1. solo.getCurrentWebElements(By.[--by xpath or above items--])
This is just multiple version of previous single web element finding

2. solo.getCurrentWebElements(); 

For both cases they provide an array list of WebElements displayed in the active WebView.

Thanks..:)

How to press menu items in Robotium?

In this article we are going to see how to check different items from menu. Basically, the items that comes when we press menu button.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

-To click an items with a text present in the menu.
solo.clickOnMenuItem("ItemName");


-To click an items with a text present in the menu or the sub-menu. If it is true in second parameter, it will check all sub-menu items.
solo.clickOnMenuItem("ItemName", false);


Note: This text will be used as regular expression(it will match with all items of the menu)

-To click a menu item based on it's index(0 Based, integer).
solo.pressMenuItem(0);


-To click a menu item based on it's index(0 Based, integer). In here, we need to mention the number of items present in each row(in integer).
solo.pressMenuItem(0, 3);


Note : In here, Index are sequentially made. Ex- if there are 2 rows and 4 items in each column, index 0=1st row, 1st item and 5=2nd row 2nd item

-To click a Drop Down Menu item based on it's deop down menu index and item index( both are 0 Based integer). First parameter is the index of drop down menu, 2nd one is the item index.
solo.pressSpinnerItem(2, 0);
-I here, Negative number in 2nd parameter(as item index) will move clicking to Up, positive number will be to Down.


..Thanks...:)

How to take a screenshot in robotium?

In this article we are going to see how to take a screenshot in an Android device while performing Unit Testing using robotium framework.

After Initiating solo, we need to use the object of solo.

-To take screenshot with default naming
 solo.takeScreenshot();

-To take screenshot with our given name
solo.takeScreenshot("NameOfTheFile");

-To take screenshot with our given name along with given quality
solo.takeScreenshot("NameOfTheFile",5);

Note :
-Default naming makes the name with current date and time.
-The given quality is measured by compression rate(in integer) 0-100 of the screen shot. 0=lowest size, 100= max quality.
-By default, it saves the screenshot in "/sdcard/Robotium-Screenshots/", So if you are using emulator, do not forget keep an SD card.
-As, it will store, it will need write permission in AndroidManifest.xml
 
(android.permission.WRITE_EXTERNAL_STORAGE)
for latest revision, if we forget to permit this,

Thanks..:)