1

As a technical tester without any deeper understanding of java I have a question regarding robotium and the ActivityInstrumentationTestCase2. I have manged to create a test case using eclipse and robotium. It works and I can execute it as a junit test as well as from the command line on a real device.

I now want to move this robotium/solo testcase into our existing test framework where each class/tescase is extended by "abstractchecker". Below is an example of one of our web test cases

package ipoMonitor.checkers;

import ipoMonitor.CheckInfo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.sql.Connection;
import static org.junit.Assert.assertTrue;

public class EmConsoleChecker extends AbstractChecker {

public EmConsoleChecker(CheckInfo curCheck, Connection con, WebDriver driver) {
    super(curCheck, con, driver);
}

@Override
public void runCheck() {
    curCheck.subCheckName = "Homepage";
    driver.get(curCheck.baseUrl);
    assertTrue(driver.findElement(By.cssSelector("BODY")).getText()
                       .matches("^[\\s\\S]*Login[\\s\\S]*$"));
    WriteLog(curCheck, con, " OK");
}

}

Is it somehow possible to keep this standard and have a robotium test class running as a method instead of a class? If so how would the code look like?

My robotium testcase that was originally recorded with extsolo looks like this

public class Test extends ActivityInstrumentationTestCase2<Activity> {

final String[] proj = {"*"};
private static final String LAUNCHER_ACTIVITY_CLASSNAME = "com.somecompany.androidsmsapp.MainActivity";
private static Class<?> launchActivityClass;
static {
    try {
        launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
private Solo solo; 

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

}

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

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

public void testRecorded() throws Exception {
    try {

        Log.i("assert", "---------------NEW RUN-------------------");

            Log.i("assert", "Starting SendSMS");
        solo.waitForActivity("MainActivity");
        // Loads of solo commands executed here                 
        .....
        .....

    } catch (AssertionFailedError e) {
        throw e;
    } catch (Exception e) {
        throw e;
    }
}

}

I have noticed that when you create a robotium test case in eclipse, there are libraries and classpath containers added automatically. Does anyone have a working example that they can share or a link to one? I have searched the net for a looooooooong time without finding any solution to this. Any help is appreciated.

Best Regards, Mattias

1 Answers1

1

Your AbstractChecker is doing a Selenium test, your ActivityInstrumentationTestCase2 does an Android Instrument Test.

With the information you provided it is not actually possible to combine the two and even if it was I don't think you would get the result you expected.

You should look within the AbstractChecker framework and see what functionality you want. Abstract and extract this out into a class that you can use as a composite for yourActivityInstrumentationTestCase2.

Java does not support multiple inheritance therefore you cannot do

Test extends AbstractChecker and ActivityInstrumentationTestCase2 {

A good programming approach is to prefer composition over inheritance, that way you would not get into the situation, and you could compose your Test of a Checker and TestCase.

It is no secret what ActivityInstrumentationTestCase2 does for you here is the source take a look.

If you want to consider composition something like:

public class Test extends ActivityInstrumentationTestCase2 { // Forced extension due to library 

  private final Checker checker;

  public Test(){
    super((Class<Activity>) launchActivityClass);
    checker = SomeFactory.getChecker();
  }

  // do your tests and use your checker

}

So I'm sorry but I think the answer to your question is no, but hopefully there are some keywords in the above to help you research more.

Community
  • 1
  • 1
Blundell
  • 75,855
  • 30
  • 208
  • 233