Search code examples
javarobotium

How to call a method in another class using robotium


Using Robotium for my Android automation I find myself creating the same steps for each test case.

I always need to "Login" and "Logout", I've been trying to create a FunctionsTestClass so I can simply call rLogin(); and rLogout();

Here is an example:

Adding my complete files.

'package com.myproject.mobile.test;

import android.test.ActivityInstrumentationTestCase2;
import android.app.Activity;
import junit.framework.AssertionFailedError;

import com.bitbar.recorder.extensions.ExtSolo;
import com.jayway.android.robotium.solo.By;

import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

'public class Logout extends ActivityInstrumentationTestCase2<Activity> {

private static final String LAUNCHER_ACTIVITY_CLASSNAME = "com.myproject.mobile.MainActivity";
private static Class<?> launchActivityClass;
static {
    try {
        launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
private static ExtSolo solo; // ExtSolo is an extension of Robotium Solo that helps
                        // collecting better test execution data during test
                        // runs

@SuppressWarnings("unchecked")
public Logout() {
    super((Class<Activity>) launchActivityClass);
}

@Override
public void setUp() throws Exception {
    super.setUp();
    solo = new ExtSolo(getInstrumentation(), getActivity(), this.getClass()
            .getCanonicalName(), getName());
}

@Override
public void tearDown() throws Exception {
    solo.finishOpenedActivities();
    solo.tearDown();
    super.tearDown();
}


public static void logginin() throws Exception {
    try {
    //enter username
        solo.sleep(17000);

        throw e;
    } catch (Exception e) {
        solo.fail(
                "com.myproject.mobile.test.MainActivityTest.testRecorded_scr_fail",
                e);
        throw e;
    }
}

}'

Adding my second file

package com.mypackage.mobile.test;

import android.test.ActivityInstrumentationTestCase2;
import android.app.Activity;
import junit.framework.AssertionFailedError;

import com.bitbar.recorder.extensions.ExtSolo;
import com.jayway.android.robotium.solo.By;
import com.mypackage.mobile.test.*;


import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

'public class Test extends ActivityInstrumentationTestCase2<Activity> {

private static final String LAUNCHER_ACTIVITY_CLASSNAME =     "com.mypackage.mobile.MainActivity";
private static Class<?> launchActivityClass;

static {
    try {
        launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
private static ExtSolo solo; // ExtSolo is an extension of Robotium Solo that helps
                        // collecting better test execution data during test
                        // runs

@SuppressWarnings("unchecked")
public Test() {
    super((Class<Activity>) launchActivityClass);
}

@Override
public void setUp() throws Exception {
    super.setUp();
    solo = new ExtSolo(getInstrumentation(), getActivity(), this.getClass()
            .getCanonicalName(), getName());
}

@Override
public void tearDown() throws Exception {
    solo.finishOpenedActivities();
    solo.tearDown();
    super.tearDown();
}



public void testRecorded() throws Exception {
    try {
    
        Logout.logginin();


    } catch (AssertionFailedError e) {
        solo.fail(
                "com.mypackage.name.MainActivityTest.testRecorded_scr_fail",
                e);
        throw e;
    } catch (Exception e) {
        solo.fail(
                            "com.mypackage.name.MainActivityTest.testRecorded_scr_fail",
                e);
        throw e;
    }
}

    }

Updated the bottom two code to reflect my project.


Solution

  • Don't create testcase with name testLoggingin() . Instead of that creat a class having function login(). So that, whenever its needed you can import it and can call the function to login. And you can check conditions using assert.