Robotium as we know is used to automate both Native as well as Hybrid applications. In this blog post we are going to see a very simple demonstration on how to automate a Hybrid google application with an example.
What’s Hybrid application first?
Hybrid application in android is something which uses the same technologies used in Web application development, but, it’s wrapped in a Native android Activity (web view). Web view is a control in android which is used to display web pages within itself.
Thus Hybrid application is the combination of whole web application within a simple (single) native android’s web view control.
Since modern web sites are using javascript heavily, we need to enable the permission of java script in the code as shown below.
webView.getSettings().setJavaScriptEnabled(true);
I don’t want to go more deep into the hybrid application, since the topic will then deviate towards native application development rather automating it.
Now the question of the day is, what are the options available in Robotium to automate those Hybrid application in android?
Let’s see most important ones one by one
In Robotium all the controls within Hybrid application is in turn a web element, hence all the controls in Robotium will be identified using webelements.
To
click the web element, here are the options
As you could see, the clickOnWebElement method has 4 overloaded methods and each of them are pretty self-descriptive.
To
get all the web elements
To
wait for a web element
To
enter text in a web element
To
clear text in a web element
As you could see, which ever option you got in performing operation in view, you can establish the same in web element control also.
Let’s start writing a native hybrid application, I am going to write a very simple application which will load the google page in it. We will call it as a Native google search application
Here is the code
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
// find the view by ID and load google.co.in the it
webView = (WebView) findViewById(R.id.webView1);
// Set Javascript execution enabled
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl("https://www.google.co.in/");
}
}
That’s it!!!
The above code is how you can write a Hybrid application in Android (Okay, I got what you think, yes !!! you can become an android application developer in couple of minutes)
Now, let write the code to automate the google application, I am not going to discuss here on how to create an android test project and add references for Robotium libraries, since those were already discussed in my previous post, just jump in my previous post.
Here is the code for automating the native application in Robotium
import com.robotium.solo.By;
import com.robotium.solo.Solo;
import com.robotium.solo.WebElement;
public class GoogleSearchTest extends
ActivityInstrumentationTestCase2 {
private Solo solo;
public GoogleSearchTest() throws ClassNotFoundException {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void testGoogleSearch() {
// Get the web view
WebView webView = (WebView) solo.getView(R.id.webView1);
// Wait for the web view to open -- 2000 explicit seconds
solo.waitForView(webView, 2000, false);
// Type in text box
WebElement txtSearch = (WebElement) solo.getWebElement(By.name("q"), 0);
txtSearch.setTextContent("Robotium");
// click button
WebElement btnSearch = (WebElement) solo.getWebElement(By.name("btnG"),
0);
solo.clickOnWebElement(btnSearch);
}
}
As you can see from the above code, we are typing in the text box by first finding it using
By.name and also there is index in the parameter of getWebElement() method. Other codes are pretty much same as selenium and rest is Robotium, hence I don’t think we need further education, just jump and start implementing it!!!
This is how we can run test in Android Hybrid application using Robotium.
I figured this feature out with little time googling and reading many articles, I hope this article will give you a complete knowledge on how you can automate a Hybrid application using Robotium.
Thanks,
Karthik KK
Hey there,
This looks like just what I need. I’ve been searching for a solution for hours.
I have one question though. How did you define webView1 as seen in
webView = (WebView) findViewById(R.id.webView1);
Is it the website you want to navigate to? Though you also specify it later.
Anyway, thanks! Even if I don’t understand all of it, this brought me forward quite a bit.
Hi Karthik,
Is it possible to test the android native apps like camera,camcorder,calculator,browser,play store ,maps,voice calls using robotium
i am eagerly waiting for u r reply
Thanks in advance
Raju
Theoretically it should be, but honestly I have never tried.
Thanks,
Karthik KK
What are getInstrumentation() and getActivity() methods?
And i got what i was looking for.
Thanks