Thursday, 21 January 2016

How to use View Matchers, View Actions and View Assertions in Espresso?

View Matchers -

Video Tutorial -


Espresso has many ViewMatcher options which are very effective in uniquely locate UI element. You can also combine and create a combination of View Matchers to find element uniquely. In Espresso we can locate the lement very effectively even though with help of these View Matchers. 

In this post you will find all the ViewMatcher options provided by Espresso with example and short .If you spend a little bit time going through this post you will be able to create automated test cases faster.

The View Matcher is written like onView(ViewMatcher) which are commonly used. There are two types of actions that can be performed on View those are -
onView(ViewMatcher).perform(ViewAction)
onView(ViewMatcher).check(ViewAssertion)



// frequently used matchers
// using resurce id
onView(withId(R.id.anuja));
// using visible text
onView(withText("Done"));
// using content description
onView(withContentDescription("profile"));
//using Hint Text
onView(withHint("Sample_text"));
// using spinner text
onView(withSpinnerText("Spinner_text"));
//return TextView with links
onView(hasLinks());

//UI property matchers are mostly used in combination 

onView(allOf(withId(R.id.anuja),isDisplayed()));
onView(allOf(withId(R.id.anuja),isCompletelyDisplayed()));
onView(allOf(withId(R.id.anuja),isClickable()));
onView(allOf(withId(R.id.anuja),isChecked()));
onView(allOf(withId(R.id.anuja),isNotChecked()));
onView(allOf(withId(R.id.anuja),isEnabled()));
onView(allOf(withId(R.id.anuja),hasFocus()));
onView(allOf(withId(R.id.anuja),hasLinks()));
onView(allOf(withId(R.id.anuja),isSelected()));
onView(allOf(withId(R.id.anuja), hasContentDescription()));

//object matcher example 

onView(withClassName(endsWith("EditText")));
onView(withText(startsWith("Hello")));
onView(allOf(withId(R.id.anuja), isDisplayed()));
onView(anyOf(withText("sample")));

//onData is used in case of ListView, GridView and AdapterView 

onData(withText("List")).atPosition(2);

//Root Matchers 

//Matches with Text on dialog 

onView(withText(R.string.hello_world)).inRoot(isDialog()); 

//Matches with Root that takes windows focus 

onView(withText(R.string.hello_world)).inRoot(isFocusable()); 

//Matches with root whoch is autocomplete or action bar spinner 

onView(withText(R.string.hello_world)).inRoot(isPlatformPopup()); 

//Matches with root that can handle touch events 

onView(withText(R.string.hello_world)).inRoot(isTouchable()); 

//Matchers with decor view 

onView(withText(R.string.hello_world)).inRoot(withDecorView(isDisplayed()));

I have mentioned some frequently used combinations of ViewMatcher samples. Apart from this you can create Custom Matcher as per you requirement. I will post that example as well in future posts.


View Actions -

Video Tutorial -


There are various Actions you can perform on the selected element to automate and reduce your manual effort and time. Let us now see most frequently used View Actions in Espresso.


//To type a test inside TextBox 

onView(withText("Enter Name")).perform(typeText("John"));
//To replace the text already written in TextBox 

onView(withText("Enter Name")).perform(replaceText("Tom"));
//To type Text inside the focus view 

onView(withText("Enter Name")).perform(typeTextIntoFocusedView("Tom"));
//To clear the text from TextBox

 onView(withText("Enter Name")).perform(clearText());
//To click a button

 onView(withText("Done")).perform(click());
//To perform double click

 onView(withText("Done")).perform(doubleClick());
//To perform long press operation 

onView(withText("Done")).perform(longClick());
//To swipe up the view

 onView(withId(R.id.sample_view)).perform(swipeUp());
//To swipe down a view

 onView(withId(R.id.sample_view)).perform(swipeDown());
//To swipe left 

onView(withId(R.id.sample_view)).perform(swipeLeft());
//To swipe right

 onView(withId(R.id.sample_view)).perform(swipeRight());
// To scroll ListView 

onView(withId(R.id.list_view)).perform(scrollTo());
//press backpressBack();
//press IME buttons of softkeyboard like done,ok,enter,
//searchpressImeActionButton();
//To close soft keybaord 

closeSoftKeyboard();
//To open phone specific menu 

pressMenuKey();
//To press key with 

keycodepressKey(66);
//To open as Link in the browser

 onView(withText("www.googl.com")).perform(openLinkWithText("www.google.com"));


A test case can never be called complete without assertions and hence it is important to know View Assertions provided by Espresso to complete your test cases.


//check the element is displayed 

onView(withId(R.id.anuja)).check(matches(isDisplayed()));
//check the element displays perticular Text 

onView(withId(R.id.anuja)).check(matches(withText(R.string.hello_world)));
onView(withId(R.id.anuja)).check(matches(withText("Hello world")));
//check if the one element is displayed on right of the other element 

onView(withId(R.id.anuja)).check(isRightOf(withText("Hi")));
//check if the one element is displayed on left of the other element 

onView(withId(R.id.anuja)).check(isLeftOf(withText("Hello")));
//check if the one element is displayed on below of the other element

 onView(withText("how are you")).check(isBelow(withText("Hi")));
//check if the one element is displayed on below of the other element

 onView(withText("Hi")).check(isAbove(withText("How are you")));
//check element does not exists 

 onView(withText("I am fine")).check(doesNotExist());


P.S.-you all are welcome to ask any doubts or post your own samples.

I hope this post helps you find your code coverage for your test suit :)
Please Share your feedback in comments section below and follow QA Automated to get latest post update.Happy Testing :-)

4 comments:

  1. Hi,

    Its really nice article. Good work.
    Where could I get the Test application code, on which above tests are ran?
    If its not possible could you please advise any open source application

    Thanks

    ReplyDelete
  2. Dear Anuja,

    Another quick query- A) how to run Espresso tests on external app i.e. if we only have APK file?
    B) Also how can we build tests on hybrid apps like- Cordova or phonegap based application

    Thanks a lot

    ReplyDelete
    Replies
    1. Hi, Espresso does not allow to run test with apk file. You need to write test case with application code project'. You can build test from hybrid apps too.

      Delete
  3. I have apk file with me. How to inspect elements with apk file using ui automator viewer with Expresso. I need your answer asap. Can I mention in Before annotation metho for desiredcapabilities with apk file.

    ReplyDelete