Skip to content
This repository was archived by the owner on Nov 7, 2020. It is now read-only.

Custom test views

profjb58 edited this page Mar 25, 2020 · 8 revisions

If you want to create a new test view or don't know how to navigate to a given activity then the following will be of some use.

public enum HiddenViews {
    
    LOGIN("AccioLogin2020", Login.class),

    private String usernamekeyWord;
    private Class<?> classDestination;

    HiddenViews(String usernameKeyWord, Class<?> classDestination) {
        this.usernamekeyWord = usernameKeyWord;
        this.classDestination = classDestination;

    }

    public String getUsernameKeyWord() {
        return usernamekeyWord;
    }

    public Class<?> getClassDestination() {
        return classDestination;
    }
}

The following is the enumeration class located in Helper > HiddenViews. What this allows us to do is set a 'keyword' that when typed into the 'username' slot in the profile settings screen instead navigates to that chosen activity.

This eliminates the use of a button and by default Profile Settings handles these enumerations like a champ. To add a new keyword and class location simply add something in the same format as the following to the enum class...

NAME([keyword], [class name].class), replacing:

  • NAME > Name of your new enumeration value
  • [keyword] > the keyword that has to be entered to enter this new activity
  • [class name] > the location of the new activity class.

To include this new link in your tests simply add the following code:

@Before
    public void setUp() throws InterruptedException {

        // Test credentials & keyword
        String TEST_EMAIL = "jb2200@york.ac.uk";
        String TEST_PASSWORD = "password";
        String KEYWORD = "AccioLayoutDesign2020";
        int THREAD_SLEEP_TIME = 4000; // Time to sleep in milliseconds

        ActivityScenario.launch(Login.class); //Launch the login screen

        onView(withId(R.id.loginButton))
                .perform(click());

        onView(withId(R.id.emailEditText))
                .perform(typeText(TEST_EMAIL));
        onView(withId(R.id.passwordEditText))
                .perform(typeText(TEST_PASSWORD));
        Espresso.closeSoftKeyboard();

        onView(withId(R.id.loginButton))
                .perform(click());

        Thread.sleep(THREAD_SLEEP_TIME);

        RecordedEspressoHelper.openSideBar(RecordedEspressoHelper.SideBarElement.EDIT_PROFILE);

        Thread.sleep(THREAD_SLEEP_TIME/4);

        onView(withId(R.id.settings_input_username))
                .perform(clearText())
                .perform(typeText(KEYWORD));

        Espresso.closeSoftKeyboard();

        onView(withId(R.id.settings_save_settings))
                .perform(scrollTo())
                .perform(click());

        Thread.sleep(THREAD_SLEEP_TIME/4);
    }

Replacing....

  • TEST_EMAIL > your own test email address, IE jb2200@york.ac.uk
  • TEST_PASSWORD > The password you use.
  • KEYWORD > The keyword to be typed in order to enter this new test activity
  • THREAD_SLEEP_TIME > Probably keep at 4000. (4s). Used so firebase has time to update.

Note the following dosen't currently work if data has to be transferred via the Intent. It would be very easy to modify to allow this though.

Clone this wiki locally