RobotiumTech / robotium

Android UI Testing
http://www.robotium.org
Apache License 2.0
2.86k stars 786 forks source link

Application not launch on test started #870

Open TechnoMag82 opened 7 years ago

TechnoMag82 commented 7 years ago

After start test, application not started automatically for testing. I must manually launch application by tap on icon.

Error by timeout: junit.framework.AssertionFailedError: Text string: 'Some button' is not found!

My test: DeliveryActivity is not first Activity in backstack when application launched.

public class DeliveryActivityTest extends ActivityInstrumentationTestCase2 {

  private Solo solo;

  public DeliveryActivityTest() {
    super(DeliveryActivity.class);
  }

  public DeliveryActivityTest(Class<DeliveryActivity> activityClass) {
    super(activityClass);
  }

  public void setUp() throws Exception {
    super.setUp();
    injectInstrumentation(InstrumentationRegistry.getInstrumentation());

    Instrumentation mInstrumentation = getInstrumentation();
    solo = new Solo(mInstrumentation);
    //solo = new Solo(getInstrumentation(), getActivity());
    //solo = new Solo(getInstrumentation(), getActivity());
  }

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

  public void testButton()
  {
    solo.clickOnText("Some button");
  }

}
andrewleo commented 7 years ago

See the getting started here:https://github.com/RobotiumTech/robotium/wiki/Getting-Started this may resolve your problem:

public void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
  }
TechnoMag82 commented 7 years ago

DeliveryActivity extends Fragment.

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

java.lang.RuntimeException: Could not launch activity

andrewleo commented 7 years ago
public NewRobotiumTest() {
super(PACKAGE_NAME, Class.forName(LAUNCHER_ACTIVITY_CLASSNAME););
}

the first parameter is package name; second is launched activity

TechnoMag82 commented 7 years ago

Thanks. Problem solved with code:

public class DeliveryActivityTest extends ActivityInstrumentationTestCase2 {

  private static final String LAUNCHER_ACTIVITY_CLASSNAME = "com.delivery.example";
  private static Class<?> launchActivityClass;
  static {
    try {
      launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
  }
  private Solo solo;

  public DeliveryActivityTest(Class<DeliveryActivity> activityClass) {
    super(activityClass);
  }

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

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

  @SuppressWarnings("unchecked")
  public DeliveryActivityTest() {
    super((Class<DeliveryActivity>) launchActivityClass);
  }

  public void testButton()
  {
    solo.clickOnText("Some button");
  }

}