mardi 5 décembre 2017

Espresso / How can I move button state between activities in Espresso tests?

I have experience next:

  • each test in my test-project uses separate activity
  • test1 (main screen) belong to activity#1, there is a button on this screen which can change state from STATE#1 (OFF) to STATE#2 (ON)
  • tap on button STATE#1 (from test1) cause raise of another screen which belong to activity#2 (there is test2 starting), then on act.#2 user perform some actions which should change button state to STATE#2
  • but in my test button STATE#2 wouldn't be refreshed and passed to the previous the activity#1

Do I need to sync test data in specific way? If button's state (which is on activity#1) can be changed from another test activity (activity#2)


Here is example of what I'm doing:

First of all test after which button state should be changed

TEST2 - test that should change button1 state

@RunWith(AndroidJUnit4.class)
 public class ChangeBtnState extends MyIdle
    {
       @Rule
        public ActivityTestRule<Activity#2> EspressoTestRule#2 = new ActivityTestRule<>(Activity#2.class, false, true);
        private MyIdle IdlingRecourseActivity#2;

       @Before
        public void SetUpTest2()
        {
         EspressoTestRule#2.getActivity().getSupportFragmentManager().beginTransaction();

         IdlingRecourseActivity#2 = (ESP_idling) EspressoTestRule#2.getActivity().getIdlingResource();
         Espresso.registerIdlingResources(IdlingRecourseActivity#2);
        }
       @Test
        public void StartTestRule2()
        {
         // Just some actions on activity#2 after which button1 state should be from STATE#1 to STATE#2
         ViewInteraction ButtonSendtoOFF = Espresso.onView(allOf(ViewMatchers.withId(android.R.id.SomebuttonONact#2))
         .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
         ButtonSendtoOFF.preform(click()); // here click 
        }

        }

       @After
        public void unregistereSetUpTest2()
        {
            if (IdlingRecourseActivity#2 != null)
            {
                Espresso.unregisterIdlingResources(IdlingRecourseActivity#2);
            }
        }

    }


Now checking does button state changed on activity#1

@RunWith(AndroidJUnit4.class)
 public class CheckBtnState extends MyIdle
    {
       @Rule
        public ActivityTestRule<Activity#1> EspressoTestRule#1 = new ActivityTestRule<>(Activity#1.class, false, true);
        private MyIdle IdlingRecourseActivity#1;

       @Before
        public void SetUpTest1()
        {
         EspressoTestRule#1.getActivity().getSupportFragmentManager().beginTransaction();

         IdlingRecourseActivity#1 = (ESP_idling) EspressoTestRule#1.getActivity().getIdlingResource();
         Espresso.registerIdlingResources(IdlingRecourseActivity#1);
        }
       @Test
        public void StartTestRule1()
        {
        // Check some actions FROM activity#2 to change **button1** from STATE#1 to STATE#2
         ViewInteraction ButtonSTATE = Espresso.onView(allOf(ViewMatchers.withId(android.R.id.Button1), withText("OFF"))
         .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
         //ButtonSTATE.preform(click()); // here click 
        }

       @After
        public void unregistereSetUpTest1()
        {
            if (IdlingRecourseActivity#1 != null)
            {
                Espresso.unregisterIdlingResources(IdlingRecourseActivity#1);
            }
        }

    }


BTW, I have look through a lot of topics and even found examples with intents, but it doesn't work for me, perhaps there is should be specific structure. If you need extra info please add your question in comments.

Aucun commentaire:

Enregistrer un commentaire