Created
July 25, 2013 16:55
-
-
Save dschulten/6081675 to your computer and use it in GitHub Desktop.
Demo code that shows how to use Robotium RC with the Spinner sample app from the SDK and JBehave.
It was necessary to create a RobotiumRC class with separate setup and teardown routines, e.g. to be called from a JUnit setup/teardown or from JBehave @BeforeStories and @AfterStories routines.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Narrative: | |
In order to learn about planets | |
As a hobby astronomer | |
I want to choose a planet | |
Scenario: Choosing a planet | |
Given the application runs SpinnerActivity | |
And Earth is the current planet | |
When I go down 1 in Spinner | |
Then I see planet Mars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.robotiumrc; | |
import static org.hamcrest.Matchers.is; | |
import static org.junit.Assert.assertThat; | |
import static org.junit.Assert.assertTrue; | |
import org.jbehave.core.annotations.AfterStory; | |
import org.jbehave.core.annotations.BeforeStory; | |
import org.jbehave.core.annotations.Given; | |
import org.jbehave.core.annotations.Then; | |
import org.jbehave.core.annotations.When; | |
import org.junit.Assert; | |
import com.android.example.spinner.R; | |
import com.jayway.android.robotium.remotecontrol.solo.Solo; | |
public class ChoosePlanetSteps { | |
String aut = "path to Spinner.apk"; | |
String messenger = "path to SAFSTCPMessenger-debug.apk"; | |
String runner = "path to RobotiumTestRunner-debug.apk"; | |
String instrument = "com.jayway.android.robotium.remotecontrol.client/com.jayway.android.robotium.remotecontrol.client.RobotiumTestRunner"; | |
String avd = "android virtual device name"; | |
RobotiumRC rc = new RobotiumRC(messenger, runner, instrument, aut, avd); | |
Solo solo; | |
@BeforeStory | |
public void setUp() { | |
solo = rc.setUp(); | |
} | |
@AfterStory | |
public void tearDown() { | |
rc.tearDown(); | |
} | |
@Given("the application runs $activity") | |
public void givenTheApplicationRuns(String activity) throws Exception { | |
solo.assertCurrentActivityName("wrong activity", activity); | |
} | |
@Given("Earth is the current planet") | |
public void givenEarthIsTheCurrentPlanet() throws Exception { | |
assertTrue(solo.isSpinnerTextSelected(0, "Earth")); | |
String uid = solo.getText(1); | |
String textViewValue = solo.getTextViewValue(uid); | |
assertThat(textViewValue, is("Earth")); | |
} | |
@When("I go down $items in Spinner") | |
public void whenIGoDownItems(int items) throws Exception { | |
assertTrue(solo.pressSpinnerItem(0, items)); | |
} | |
@Then("I see planet $planet") | |
public void thenISeePlanet(String planet) throws Exception { | |
String view = solo.getView(R.id.SpinnerResult); | |
String value = solo.getTextViewValue(view); | |
Assert.assertThat(value, is(planet)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.robotiumrc; | |
import org.jbehave.core.configuration.Configuration; | |
import org.jbehave.core.configuration.MostUsefulConfiguration; | |
import org.jbehave.core.io.LoadFromClasspath; | |
import org.jbehave.core.junit.JUnitStory; | |
import org.jbehave.core.reporters.Format; | |
import org.jbehave.core.reporters.StoryReporterBuilder; | |
import org.jbehave.core.steps.InjectableStepsFactory; | |
import org.jbehave.core.steps.InstanceStepsFactory; | |
import org.junit.runner.RunWith; | |
import de.codecentric.jbehave.junit.monitoring.JUnitReportingRunner; | |
@RunWith(JUnitReportingRunner.class) | |
public class ChoosePlanetStory extends JUnitStory { | |
public ChoosePlanetStory() { | |
super(); | |
JUnitReportingRunner.recommandedControls(configuredEmbedder()); | |
} | |
@Override | |
public InjectableStepsFactory stepsFactory() { | |
return new InstanceStepsFactory(configuration(), | |
new ChoosePlanetSteps()); | |
} | |
@Override | |
public Configuration configuration() { | |
return new MostUsefulConfiguration() | |
.useStoryLoader(new LoadFromClasspath(this.getClass())) | |
.useStoryReporterBuilder( | |
new StoryReporterBuilder().withDefaultFormats() | |
.withFormats(Format.HTML_TEMPLATE)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.robotiumrc; | |
import com.jayway.android.robotium.remotecontrol.solo.Solo; | |
import com.jayway.android.robotium.remotecontrol.solo.SoloTest; | |
public class RobotiumRC extends SoloTest { | |
public RobotiumRC(String messengerApk, String runnerApk, | |
String instrumentArg, String autApk, String avd) { | |
super(messengerApk, runnerApk, instrumentArg); | |
this.setAUTApk(autApk); | |
this.avdSerialNo = avd; | |
} | |
public Solo setUp() { | |
if (!preparation()) { | |
error("Preparation error"); | |
if (!stopEmulator()) { | |
warn("We failed to stop the emulator launched by us."); | |
} | |
throw new AssertionError("failed to prepare device"); | |
} | |
if (!initialize()) { | |
throw new AssertionError("failed to initialize Solo"); | |
} | |
debug("Begin Test."); | |
return solo; | |
} | |
public void tearDown() { | |
debug("End Test."); | |
if (!terminate()) { | |
warn("Termination of Solo failed!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
Thanks for the article. I get the following error when I try to run the test app as an Android JUnit Test
Pls note that I've renamed the test app project to HelloWorldTest. The App under test is HelloWorld. Any help would be greatly appreciated
[2014-07-24 21:35:11 - HelloWorldTest] ------------------------------
[2014-07-24 21:35:11 - HelloWorldTest] Android Launch!
[2014-07-24 21:35:11 - HelloWorldTest] adb is running normally.
[2014-07-24 21:35:11 - HelloWorldTest] Performing android.test.InstrumentationTestRunner JUnit launch
[2014-07-24 21:35:11 - HelloWorldTest] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Cloned_Tablet'
[2014-07-24 21:35:11 - HelloWorldTest] Uploading HelloWorldTest.apk onto device 'emulator-5554'
[2014-07-24 21:35:11 - HelloWorldTest] Installing HelloWorldTest.apk...
[2014-07-24 21:35:17 - HelloWorldTest] Success!
[2014-07-24 21:35:17 - HelloWorldTest] Project dependency found, installing: HelloWorld
[2014-07-24 21:35:18 - HelloWorld] Application already deployed. No need to reinstall.
[2014-07-24 21:35:18 - HelloWorldTest] Launching instrumentation android.test.InstrumentationTestRunner on emulator-5554
[2014-07-24 21:35:18 - HelloWorldTest] Failed to launch test