Start Activity for testing

前端 未结 5 761
青春惊慌失措
青春惊慌失措 2020-12-30 22:12

I \'ve got a Quiz app using Realm db. Every time the user selects an answer she clicks a button and new text for Question appears. Thats it until she reaches the end where

相关标签:
5条回答
  • 2020-12-30 22:21

    First, see this question : Android Monkey Runner

    Then you can see these guides :Monkey Runner

    It makesyou usePython to test your android activity outside of your source. So, you can trigger things and get to specific activitiesl like this :

    #! /usr/bin/env monkeyrunner
    
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
    from random import randint
    
    print "get device"
    device = MonkeyRunner.waitForConnection()
    package = 'my.packaget'
    activity = 'my.package.activity'
    runComponent = package + '/' + activity
    device.startActivity(component=runComponent)
    
    #use commands like device.touch and device.drag to simulate a navigation and open my activity
    
    #with your activity opened start your monkey test
    print "start monkey test"
    for i in range(1, 1000):
        #here i go emulate only simple touchs, but i can emulate swiper keyevents and more... :D
        device.touch(randint(0, 1000), randint(0, 800), 'DOWN_AND_UP')
    
    print "end monkey test"
    

    save it and then run : monkeyrunner test.py

    0 讨论(0)
  • 2020-12-30 22:22

    For Devs using AndroidX for testing, things are a bit changed.

    This is an example UI Test case for testing whether my intended activity opens after clicking on the textview.

    import androidx.lifecycle.Lifecycle
    import androidx.test.core.app.ActivityScenario
    import androidx.test.espresso.Espresso.onView
    import androidx.test.espresso.action.ViewActions
    import androidx.test.espresso.intent.Intents
    import androidx.test.espresso.intent.Intents.intended
    import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
    import androidx.test.espresso.matcher.ViewMatchers.withId
    import com.softway.dhananjay.tournamentapp.tournament.TournamentActivity
    import org.junit.Test
    
    class MainActivityTest {
    
        @Test
        fun tournament_activity_starts_onClick_of_textView() {
    
            Intents.init()
    
            val activityScenario: ActivityScenario<MainActivity> =
                ActivityScenario.launch(MainActivity::class.java)
    
    
            activityScenario.moveToState(Lifecycle.State.RESUMED)
    
            onView(withId(R.id.startTextView)).perform(ViewActions.click())
    
            intended(hasComponent(TournamentActivity::class.java.name))
    
            Intents.release()
    
            activityScenario.moveToState(Lifecycle.State.DESTROYED)
    
        }
    }
    
    0 讨论(0)
  • 2020-12-30 22:28

    You can launch your next activity with a custom intent like this:

    @RunWith(AndroidJUnit4.class)
    public class NextActivityTest {
    
      @Rule
      public ActivityTestRule<NextActivity> activityRule 
         = new ActivityTestRule<>(
            NextActivity.class,
            true,     // initialTouchMode
            false);   // launchActivity. False to customize the intent
    
      @Test
      public void intent() {
        Intent intent = new Intent();
        intent.putExtra("your_key", "your_value");
    
        activityRule.launchActivity(intent);
    
        // Continue with your test
      }
    }
    

    Full example: https://github.com/chiuki/android-test-demo

    Blog post: http://blog.sqisland.com/2015/04/espresso-21-activitytestrule.html

    0 讨论(0)
  • 2020-12-30 22:28

    You can use the intent to launch dialer activity using below code.

        @Rule    
    public IntentsTestRule<DialerActivity> mActivityRule = new IntentsTestRule<>(
                DialerActivity.class);
    
        private static final String PHONE_NUMBER = "1234567890";
        private static final Uri INTENT_DATA_PHONE_NUMBER = Uri.parse("tel:" + PHONE_NUMBER);
        private static String PACKAGE_ANDROID_DIALER = "com.android.phone";
    
        static {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // Starting with Android Lollipop the dialer package has changed. 
    
                PACKAGE_ANDROID_DIALER = "com.android.server.telecom";
            }
        }
    
        @Test    public void testDialerIntent()throws Exception
        {
            intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
            onView(withId(R.id.edit_text_caller_number)).perform(typeText(PHONE_NUMBER));
            onView(withId(R.id.button_call_number)).perform(click());
            intended(allOf(
                    hasAction(Intent.ACTION_CALL),
                    hasData(INTENT_DATA_PHONE_NUMBER),
                    toPackage(PACKAGE_ANDROID_DIALER)));
        }
    
    
    }
    

    For more detailed description refer my blog post - http://qaautomated.blogspot.in/2016/02/how-to-test-dialer-activity-with.html

    0 讨论(0)
  • 2020-12-30 22:39
    private void launchApp(){
            // Launch the app
            Context context = InstrumentationRegistry.getContext();
            final Intent intent = context.getPackageManager()
                    .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
            // Clear out any previous instances
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
    
            // Wait for the app to appear
            device.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
                    LAUNCH_TIMEOUT);
        }
    
    0 讨论(0)
提交回复
热议问题