UI testing with Espresso - Android Testing Patterns #2

UI testing with Espresso - Android Testing Patterns #2 - Hello friends Android tutorial and tips trik, The article you are reading this time with the title UI testing with Espresso - Android Testing Patterns #2, we have prepared this article for you to read and download the information therein. hopefully fill posts article -, article 2), article android, article Espresso, article Patterns, article Testing, article Tutorial, article UI, article with, we can write this you understand. Well, happy reading.

Title : UI testing with Espresso - Android Testing Patterns #2
link : UI testing with Espresso - Android Testing Patterns #2

Baca juga


UI testing with Espresso - Android Testing Patterns #2

[MUSIC PLAYING] Writing good testscan be a challenge.

And writing reliable UItests, it's substantially more difficult.

User interfacesare asynchronous, driven by events,transitions, and data loaded from background threads.

Coding around that withoutany help from a UI testing framework would require a lotof boilerplate and handling of edge cases.

On the other hand, say I giveyou a phone with a sample app and tell you to testa feature of the app that I just implemented.

For example, please makesure that saving a new note in my note-taking app works.

What would you do? Well, I'm pretty sure that youwould look for a Save button.

Once you've foundit, you'd tap on it and then check if the noteis present in the list of saved notes.

That's actually a prettygood scenario for a UI test, and it's simple tounderstand for a person.

But how would we go aboutexpressing it in code? The Espresso frameworkwas created specifically for this purpose--to enable developers to write UI tests that areconcise, reliable, and using a fluent API.

And most importantly, Espressotakes care of synchronization with any UI events sothat, in most cases, you don't have to worry aboutany view state transitions and implementation details.

Looking back at the simpleUI test we just defined, we can see that the basicflow when using Espresso is exactly the same as inour real-life scenario.

First, find a view usingsome matching rules.

Then perform an action on it.

And finally, verifythe resulting state.

Before I go any further andstart writing actual test code, let's make sure thatthe Android test runner and Espresso dependenciesare configured in build.

Gradle.

I'll add the dependenciesand set up the runner here.

If you're using a versionof Android Studio that lets you selectthe test artifact, remember to switch toAndroid instrumentation test under Build Variants.

I'm using AndroidStudio 2.

0, which has a combined view for bothlocal and instrumentation tests.

So I can already seeboth in my project view.

Instrumentation tests go underthe Android test source set.

So I'll create asample test class here and name it NotesScreenTest.

I have to add an annotationon the test class to specify that I'll be writingJunit 4 tests and running them with theAndroid Junit 4 runner.

The note screen inour app is contained in the notes activity.

By adding an activitytest rule, I'm telling the runner to launchthe activity before any test and turn it downafter they finish.

All of this is takencare of with this line.

Now I'll add a newmethod with a descriptive name for the actual test andstart with the basic structure I explained before.

Notice that onView is actuallya method on the Espresso class.

But across my testing code,I will use static imports to be able to express thetests in a more concise way.

All right.

Let's go throughthe arguments next.

We need a matcher in order tofind a view in the current view hierarchy.

Espresso comes with aset of built-in matchers for common view propertieslike With ID, With Text, Is Checked, and many others.

We provide a simplecheat sheet so you don't have to go digging throughthe documentation every time.

In my case, I want to locate thefloating action button, which fortunately is the only itemon the screen with the ID fab_add_notes.

Now that I've isolated the viewthat I need using matchers, it's time to applya view action, or in other words,a user interaction that will besimulated by Espresso.

I just want to click the FAB.

But again, there are many morebuilt-in actions, like typing and even finger swipes, that Ican use to interact with views.

And finally, I verifythe outcome of my actions by using a view assertion.

Since I want to check ifan input field is shown on screen afterpressing the fab, I'll move thisblock of code here.

And I use the matches assertionthat accepts a view matcher.

So to summarizewhat we have so far, the first statement matchesa floating action button and clicks it, which shouldbring up the Add Note screen.

The second statementfinds the edit text that lets a userenter a note title and verifies that itis displayed using the matches view assertion.

Notice that I didn't haveto write any code in between to wait for previous eventsto finish, as Espresso already takes careof that for me.

To run the test, Iright-click on the test class and select Run.

Because it's aninstrumentation test, it requires an emulatoror physical device.

I can see the actions onthe screen as they execute.

Here's a slightlylonger test that includes typing into the addnote screen and saving a note, all using Espresso.

Unless you're writingend-to-end tests, you should keep your testssmall and scoped, which will make them more reliable.

If you want to practiceadding and running UI tests on your own,I suggest you check out our Android Testing Codelab,which I based this video on.

It contains adownloadable project and step-by-step instructionsto get you started.

Join me for the nextepisode of Android Testing Patterns to learn how to handleadapter views in your tests.

Good luck, and happy testing.

[MUSIC PLAYING].



Thus Article UI testing with Espresso - Android Testing Patterns #2

That article UI testing with Espresso - Android Testing Patterns #2 This time, hopefully we can give you all of the benefits. Well, I'll see you in another article post.

You now read the article UI testing with Espresso - Android Testing Patterns #2 the link address http://svedda79.blogspot.com/2016/02/ui-testing-with-espresso-android.html

0 Response to "UI testing with Espresso - Android Testing Patterns #2"

Post a Comment