Wednesday, 15 March 2017

Android Espresso

Build.gradle


//In default config
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

//In dependencies
androidTestCompile 'com.android.support.test:runner:0.5'androidTestCompile 'com.android.support.test:rules:0.5'
//Espresso coreandroidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {
exclude group: 'com.android.support', module: 'appcompat' exclude group: 'com.android.support', module: 'support-v4' exclude module: 'support-annotations' exclude module: 'recyclerview-v7'}

Sample Example of UI test using espresso


import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;



import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withHint;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;

public class ActivityTest {
Context appContext;
@Rule
public ActivityTestRule<Activity> mActivityTestRule =
new ActivityTestRule<Activity>(Activity.class);
@Before
public void setUP() {
appContext = InstrumentationRegistry.getTargetContext();
}

@Test
public void firstTest() {
// Context of the app under test.

String toEmail = "sonuash@gmail.com";
String toTextView = appContext.getResources().getString(R.string.sample);
String toSubView = appContext.getResources().getString(R.string.samplestr);


//Type text in edittext through espresso
onView(ViewMatchers.withId(R.id.toAddrFieldView)).perform(typeText(toEmail));
//match text displayed in textview
onView(withId(R.id.toAddrTextView)).check(matches(allOf(withText(toTextView), isDisplayed())));
//match hint displayed in edittes
onView(withId(R.id.subjectFieldView)).check(matches(withHint(toSubView)));
//click on any button / edittext
onView(ViewMatchers.withId(R.id.toolbar)).perform(click());

}
}

No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...