Thursday, 13 March 2014

Android Robotium Test -run robotium tests in a specific order

Hello Friends,
                         This is very common scenario when you are running any testsuits. It
by default runs in alphabetical order. I too faces the same problem while writing the
test case using Robotium.

In my case I want to execute the testcase in following order :
MainLoginScreenTest -> EmailSignUpScreenTest -> EmailLoginScreenTest

But it actually runs in alphabetical order like,
 EmailLoginScreenTest -> EmailSignUpScreenTest - > MainLoginScreenTest

Then I follow this link and find the way to come out from this problem. Here is
my code.

1. AllTests.java
package com.android.test;
import junit.framework.TestSuite;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;

public class AllTests extends 
        ActivityInstrumentationTestCase2<Activity>{
 
 public AllTests(Class activityClass) {
  super(activityClass);
 }

 public static TestSuite suite() {
  TestSuite t = new TestSuite();
  t.addTestSuite(MainLoginScreenTest.class);
  t.addTestSuite(EmailSignUpScreenTest.class);
  t.addTestSuite(EmailLoginScreenTest.class);
  
  return t; 
 }
 
 @Override
 public void setUp() throws Exception {
  
 }
 
 
 @Override
 public void tearDown() throws Exception {
 }

}


2.MainLoginScreenTest.java

package com.android.test;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.ImageButton;

import com.android.Login.EmailSignUpScreen;
import com.android.Login.MainLoginScreen;
import com.android.android.R;
import com.robotium.solo.Solo;

public class MainLoginScreenTest extends
          ActivityInstrumentationTestCase2<MainLoginScreen> {
 
 private Solo solo;
 private Activity activity;
 
 public MainLoginScreenTest() {
  super(MainLoginScreen.class);
  // TODO Auto-generated constructor stub
 }
 
 @Override
 public void setUp() throws Exception {
  //setUp() is run before a test case is started. 
  //This is where the solo object is created.
  this.activity = this.getActivity();
  this.solo = new Solo(getInstrumentation(), this.activity);
 }
 
 
 @Override
 public void tearDown() throws Exception {
       solo.finishOpenedActivities();
 }

 public void testDisplay() throws Exception {
   solo.waitForActivity(MainLoginScreen.class);
   ImageButton emailLogin = (ImageButton) this.solo.getView(R.id.btn_email_login);
   assertEquals(emailLogin.getId(), R.id.btn_email_login);
   this.solo.clickOnImageButton(1);
   this.solo.waitForActivity(EmailSignUpScreen.class, 1000);
   assertEquals(EmailSignUpScreen.class, solo.getCurrentActivity().getClass());
   //this.solo.goBack();
 }
}


3.EmailSignUpScreenTest.java

package com.android.test;

import junit.framework.Test;
import junit.framework.TestSuite;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.MediumTest;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.Login.EmailLoginScreen;
import com.android.Login.EmailSignUpScreen;
import com.android.android.R;
import com.robotium.solo.Solo;

public class EmailSignUpScreenTest extends 
           ActivityInstrumentationTestCase2<EmailSignupScreen>{

 private Solo solo;
 private Activity activity;
 
 public EmailSignUpScreenTest() {
  super(EmailSignUpScreen.class);
 }
 public EmailSignUpScreenTest(String name) {
  super(EmailSignUpScreen.class);
  setName(name);
 }
 
 public static final Test suite(  ) {
   TestSuite suite = new TestSuite();
   suite.addTest(new TestSuite(EmailSignUpScreen.class));
   suite.addTest(new TestSuite(EmailLoginScreen.class));
   return suite;
   }

 @Override
 public void setUp() throws Exception {
  //setUp() is run before a test case is started. 
  //This is where the solo object is created.
  this.activity = this.getActivity();
  this.solo = new Solo(getInstrumentation(), this.activity);
 }
 
 @Override
 public void tearDown() throws Exception {
       solo.finishOpenedActivities();
 }

 public void testDisplay() throws Exception {
  String fName = "Sam";
  String lName = "Joshi";
  String email = "sam@gmail.com";
  String password = "abc123";
  String dob = "03-11-2014";
  String zipcod = "201301";
    
  this.solo.typeText((EditText) this.activity.findViewById(R.id.edt_first_name), fName);
  //this.solo.enterText((EditText) this.activity.findViewById(R.id.edt_last_name), lName);
  this.solo.typeText((EditText) this.activity.findViewById(R.id.edt_email), email);
  this.solo.typeText((EditText) this.activity.findViewById(R.id.edt_password), password);
  this.solo.typeText((EditText) this.activity.findViewById(R.id.edt_confirm_password), password);
  this.solo.enterText((EditText) this.activity.findViewById(R.id.edt_birth_date), dob);
  RadioButton rb = (RadioButton) solo.getView(R.id.rb_female);
  solo.clickOnView(rb);
  this.solo.typeText((EditText) this.activity.findViewById(R.id.edt_zip_code), zipcod);
  //Click on the button named "Sign up".
  this.solo.clickOnButton("Sign Up");
  //this.solo.w
  //Check to see if the given text is displayed.
  //assertTrue(this.solo.searchText(text));
 }
 
 @MediumTest
 public void testStartEmailLoginScreen() throws Exception {     
   this.solo.waitForActivity(EmailSignUpScreen.class); 
   TextView tvSignIn = (TextView) this.activity.findViewById(R.id.sign_in_label);
   String text = tvSignIn.getText().toString();
   this.solo.waitForText(text);
   this.solo.clickOnText(text);
   this.solo.waitForActivity(EmailLoginScreen.class, 1000);
   assertTrue(solo.waitForActivity(EmailLoginScreen.class));
 }
}

4.EmailLoginScreenTest.java
package com.android.test;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.EditText;

import com.android.Login.EmailLoginScreen;
import com.android.android.R;
import com.robotium.solo.Solo;

public class EmailLoginScreenTest extends
        ActivityInstrumentationTestCase2<EmailLoginScreen>{

 private Solo solo;
 private Activity activity;
 
 public EmailLoginScreenTest() {
  super(EmailLoginScreen.class);
 }

 @Override
 public void setUp() throws Exception {
  //setUp() is run before a test case is started. 
  //This is where the solo object is created.
  this.activity = this.getActivity();
  this.solo = new Solo(getInstrumentation(), this.activity);
 }
 
 
 @Override
 public void tearDown() throws Exception {
       solo.finishOpenedActivities();
 }

 public void testDisplay() throws Exception {
  String email = "mukesh@gmail.com";
  String password = "abc123";
  
  
  this.solo.typeText((EditText) this.activity.findViewById(R.id.edt_login_email), email);
  //this.solo.enterText((EditText) this.activity.findViewById(R.id.edt_login_password), password);
  //Click on the button named "Sign up".
  this.solo.clickOnButton("Login");
  this.solo.waitForActivity(EmailLoginScreen.class, 2000);
  //Check to see if the given text is displayed.
  //assertTrue(this.solo.searchText(text));
 }
}


Note: Right click on AllTests.java and choose Run As-> Android Junit test ->
               select AllTests>java file.

Hope this post is helpful for some one.
Enjoy Coding......  :)

Mukesh Yadav

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

2 comments:

  1. Hey. I have a test suite with two test classes Test1 and Test2 . I run the test suite. Test1 runs fine and closes the application as i'm writing solo.finishOpenedActivities(), Test2 never runs. Ideally for Test2, the application has to again start.
    How to solve this issue ?

    ReplyDelete
  2. Are you adding the testsuite
    public static TestSuite suite() {
    TestSuite t = new TestSuite();
    t.addTestSuite(Test1.class);
    t.addTestSuite(Test2.class);
    return t;
    }
    If yes then See the above note I mention that.May be you
    are not running the Test suite class, you are running the
    complete project, In this case only first test class will be executed
    every time.

    ReplyDelete

 

Copyright @ 2013 Android Developer Blog.

Blog Widget by LinkWithin