Updated 16 April 2019
As Espresso is an emerging automation tool for application testing but how to understand the script that’s the main issue because after recording the test we start playing it then what we see that its too slow and giving error in each and every part of the test script because the reason is that espresso records necessary things as well as the unnecessary things also .so for that we have to edit the test script and make it simple so that it will run the recorded script smoothly .
Following are the tips for writing scripts in Espresso :-
1. When start recording test the time we delay to perform any action then it will also record that like:-
1 2 3 4 5 |
try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace(); } |
So, when you play that script then this code will make a long delay because of the try catch exception.
Solution :-
Remove try catch block set the time as required. like:-
1 |
Thread.sleep(1000); |
2. When you got “single click” error that means that suppose if you want to click the particular button but that is not properly displaying on the screen.This error occur whenever “Scroll to” is used. According to espresso whatever thing displayed to the user on screen then only it will perform any action otherwise it will generate error like this :-
1 |
android.support.test.espresso.PerformException: Error performing 'single click |
Solution :-
Instead of using Scroll to() use swipe up(),reason is that when using swipe up() and swipe down() it will scroll the page to the desired location so thereafter the location will be completely visible in screen .
1 2 3 |
onview (withid(R.id. buttons-id/layout-id)).perform(swipeup()); onview (withid(R.id. buttons-id/layout-id)).perform(swipedown()); |
Here,”buttons-id/layout-id” should be the layout which is currently visible on the screen and then it will scroll the screen to the position where you want to perform the action.
3. In espresso if you want to perform any action on any buttons, text field , menu drawer or layout then in three ways you can locate them like
1 |
onview(Withid(R.id.button-id)).perform(click()); |
1 |
onview(withtext(R.string.button-string)).perform(click()); |
1 |
onview(withtext("button-text name")).perform(click()); |
4. To select item from list on particular position in two ways.
1 2 3 |
ViewInteraction recyclerView = onView( allOf(withId(R.id.recycler_view_id), isDisplayed())); recyclerView.perform(actionOnItemAtPosition(10, click())); |
In this we can locate the position where we have to select the item here “10” is the item’s position and “recycler_view_id” is the id of that recycler view .
1 |
onData(anything()).inAdapterView(withId(R.id.adapterview)).atPosition(1).perform(click()); |
In this we can locate the position where we have to select the item here “1” is the item’s position and “adapterview” is the id of that adapter view .
5. Sometimes while editing text in text field and we close the keypad then it will record the script like this:-
1 2 3 |
ViewInteraction appCompatEditText3 = onView( allOf(withId(R.id.et_username), withText("xyz.com"), isDisplayed())); appCompatEditText3.perform(replaceText("abc.com"), closeSoftKeyboard()); |
So,here while running this script it will try to close the keypad but the keypad is opened only while recording the script not while running the script so it will generate error and the solution for this that remove that “closesoftkeyboard()” then it will run perfectly. Otherwise error will be like this :-
1 |
android.support.test.espresso.NoMatchingViewException:Noviews inhierarchy found matching: |
6. Another problem is “withtext” and “replace text” the problem is while recording the test when we clear some already existing text and adding new text so be sure that the text which is already exist that should exist always because while running the script it will search that text in “withtext” then it will replace with new text entered by the user like:-
1 2 3 |
ViewInteraction appCompatEditText3 = onView( allOf(withId(R.id.et_username), withText("xyz.com"), isDisplayed())); appCompatEditText3.perform(replaceText("abc.com")); |
In this “xyz.com” is already existing text then while running the script it will search this text and when that text not found then it will through error .so,if there is no text i,e “withtext” then you should remove “withtext” like this:-
1 2 3 |
ViewInteraction appCompatEditText3 = onView( withId(R.id.et_username) ); appCompatEditText3.perform(replaceText("abc.com")); |
So while removing “withtext” it will not search for that text it will directly replace it with new one.
Next I’ll post much more basics of Espresso as well as the errors with solutions .
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
1 comments