How to write a test case method using Robotium in Android - android

I am writing a test case class for one of the activity in my application.
That Activity class contains license checking for the application in android market and also displays the splash screen for 3 seconds. Here I would like to test that activity is displaying the splash screen and checking the license using Robotium instrumentation in Android.
So please tell me how to do this.

To test that your splash screen is shown, you can try this, if you have set up robotium in your setup method, and named it solo:
public void testSplash() {
assertNotNull(solo.getCurrentActivity().findViewById( "the id of the splash" ));
}
public void testLicense() {
String licence = "my licence";
assertEquals(licence, (MyActivity) solo.getCurrentActivity().getLicence());
}

Related

How to check the current Activity in a UI test

For tests I use Espresso and Barista
I have a test in which I need to open another screen by pressing a button. How can I check if this screen opens? Did the screen I need open?
Can I somehow check the chain of screens? To understand that the screens open in the order I need?
If someone throws links to good tutorials on UI tests in Android, I will be very grateful.
An easy solution would be to just check for an element of the new screen to be shown like this:
onView(withId(R.id.id_of_element_in_your_new_screen)).check(matches(isDisplayed()))
If you really want to check out for the current activity that is shown, you could try something like this:
Gather the current activity via InstrumentationRegistry and check for the activity in stage RESUMED.
fun getTopActivity(): Activity? {
InstrumentationRegistry.getInstrumentation().runOnMainSync {
val resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED)
if (resumedActivities.iterator().hasNext()) {
resumedActivities.iterator().next()?.let {
activity = it
}
}
}
return activity
}
You could then check this in a test like this:
#Test
fun checkForActivity() {
val currentActivity = getTopActivity()
assertTrue(currentActivity?.javaClass == YourActivityToCheckAgainst::class.java)
}
I personally use intended(hasComponent(YourActivityToCheckAgainst::class.java.name)), which checks if the last intent was done with a desired activity, set as its component.
I also wrote an extensive Android UI testing tutorial using Espresso + Barista libraries.

slow startup (500ms) with all apps made by My Android studio

I'm a beginner and when i ever i make an app and i test it on real phone
the first activity takes 400-900ms to start, even when it's only show a text view or a button. (just empty App)
on the same phone i have few apps (Games) from Play Store , and they work instantly! there's no 10ms delay!
so I'm wondering why is this happening to me even if i make (Empty App)
I mean seriously, why would it starts slowly with this simple code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setText("Hello World");
}
}
The problem is that the APK you are generating is with debug property on, so this APK is considered to be in development mode that's why it will take longer to launch. You need to generate a production APK (same one you can publish on Play Store) in order to have the same launch speed as the other apps. Check this out : Sign Your APK

Taking Screen shot using espresso

I am using Espresso for my UI Testing in my project. I want to take screen shot of each Activity(Screen). I am using ScreenShooter from GoogleCloudTestLab for taking screen shot.
ScreenShotter.takeScreenshot("main_screen_2", getActivity());
But it only taking the screen shot of the 1st activity which i defined in my ActivityTestRule. How can I take other activity screen shot with in the same testcase.
My understanding is ActivityTestRule is designed to test only one Activity within the testcase so getActivity() will only return the activity that you specified in the ActivityTestRule.
To capture the screenshot, the library currently uses:
View screenView = activity.getWindow().getDecorView().getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
(where activity is the activity the user passes us.)
So because the same activity is being given to takescreenshot, we are only able to capture that activity's view hierarchy at that time. Would you be able to split up your tests to test only one activity per testcase?
Also, we are currently exploring other ways to capture the screen and will add to this thread if we change this method.
Note: If you are using this library to run tests in Firebase Test Lab and you have a prefered way of capturing screenshots (instead of using the library), as long as they end up in the /sdcard/screenshots directory then they will be pulled and uploaded to the dashboard at the end of the test.
I had the same issue since my tests cover flows which span multiple activities. A helper method such as this one can be used to get a reference to the currently active (on top) activity:
/**
* A helper method to get the currently running activity under test when a test run spans across multiple
* activities. The {#link android.support.test.rule.ActivityTestRule} only returns the initial activity that
* was started.
*/
public static final Activity getCurrentActivity(Instrumentation instrumentation)
{
final Activity[] currentActivity = new Activity[1];
instrumentation.runOnMainSync(new Runnable()
{
public void run()
{
Collection<Activity> resumedActivities =
ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(RESUMED);
if (resumedActivities.iterator().hasNext())
{
currentActivity[0] = resumedActivities.iterator().next();
}
}
});
return currentActivity[0];
}
Pass it getInstrumentation() from within your test and you should be good to go.

junit.framework.AssertionFailedError for assertNotNull()

I don't have much experience on android JUnit test. Here I'm doing a simple test application for JUnit.The application contains two activity when the button pressed in the first activity Intent use to start the second activity. But I'm getting two failures as junit.framework.AssertionFailedError. This is the code that I have tried:
public void testLayout(){
buttonId=com.example.androidunittest.R.id.button;
assertNotNull(activity.findViewById(buttonId));
Button view=(Button) activity.findViewById(buttonId);
assertEquals("Incorrect label of the button","Start",view.getText());
}
public void testIntentTriggerViaOnClick(){
buttonId=com.example.androidunittest.R.id.button;
Button view=(Button) activity.findViewById(buttonId);
assertNotNull("Button not allowed to be null",view);
view.performClick();
Intent triggeredIntent=getStartedActivityIntent();
assertNotNull("Intent was null",triggeredIntent);
String data=triggeredIntent.getExtras().getString("URL");
assertEquals("Incorrect data passed via the intent","http://www.google.com", data);
}
The failure line are
assertNotNull("Button not allowed to be null",view); and assertNotNull(activity.findViewById(buttonId)); according to the JUnit test panel result.
Could anyone please explain me what is happening here? Also how to fix the error in the application code?
I have another question, how to run all test class once in eclipse?
What I did is as per this answer , right click on the project -> Run as-> Android JUnit test. But only active class was gave the result.
Any help would be appreciated.

how to use fest for android

I am trying to use FEST with an android app testing
I want to check if some views in the activity got created and initialized correctly
#Test
public void testSplashScreenActivity() throws Exception{
SplashScreenActivity activity = Robolectric.buildActivity(SplashScreenActivity.class).create().start().resume().get();
//here i want to write fest assertions
}
however, when i assertThat(activity.welcomeText)..... nothing is allowed after this line?
i wrote import static org.fest.assertions.api.ANDROID.assertThat;
in my imports, yet the api doesnt seem to be available
what to do to make my test project give me the api i need ?

Categories

Resources