Is Android Espresso misconfigured or unstable?

I have been running an espresso test for Android for a week. What is real is to implement a server call and wait for it using espresso. This is called an Idle Resource call, and we must follow the rules, which are pretty simple. In fact, I found a solution, but the result is shocking - I will succeed only if I comment on the lines

Espresso.onView(ViewMatchers.withId(R.id.email)).perform(ViewActions.typeText("some shit")); Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText("123")); 

and replacing them with a hook:

 final EditText email = (EditText) act.findViewById(R.id.email); final EditText password = (EditText) act.findViewById(R.id.password); getInstrumentation().runOnMainSync(new Runnable() { public void run() { email.setText("Engineer"); password.setText("2342"); } }); 

..before clicking on a button that starts a new action after an emulated call to the server. These are my whole files: build.gradle:

 apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { applicationId "shoppinglist.kizema.anton.testappespresso" minSdkVersion 14 targetSdkVersion 21 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } packagingOptions { exclude 'LICENSE.txt' exclude 'META-INF/notice.txt' exclude 'META-INF/license.txt' exclude 'META-INF/ASL2.0' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/LICENSE' exclude 'META-INF/NOTICE' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/DEPENDENCIES' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3' // App dependencies, including test compile 'com.android.support:support-annotations:21.0.3' // Testing-only dependencies androidTestCompile 'com.android.support.test:testing-support-lib:0.1' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0' } 

LoginActivity (first activity):

 //set up initial listener private void initLoginHelper(){ loginHelper = new Server() { @Override public void login(String email, String code, String phone, String password, boolean loginByPhoneNumber) { //ask server try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } //done Intent intent = new Intent(LoginActivity.this, SecondActivity.class); startActivityForResult(intent, 0); } }; } //onButtonClick handler public void btnLogInSuka(View v) { performLogin(); } void performLogin() { new Thread(new Runnable() { @Override public void run() { loginHelper.login(emailParam,codeParam,phoneParam,passwordParam,false); } }).start(); } 

And AplicationTest.java (espresso tests): @LargeTest open class ApplicationTest extends ActivityInstrumentationTestCase2 {

 public ApplicationTest() { super(LoginActivity.class); } CountingIdlingResource idleRes; @Override public void setUp() throws Exception { super.setUp(); getActivity(); idleRes = new CountingIdlingResource("server"); Espresso.registerIdlingResources(idleRes); } public void testSample(){ final LoginActivity act = (LoginActivity) getCurrentActivity(); Server aHelper = act.getUserHelper(); MyUserHelperExternalIdleRes helper2 = new MyUserHelperExternalIdleRes(idleRes, aHelper); act.setUserHelper(helper2); //if comment this and uncomment next two lines we receive PerformException final EditText email = (EditText) act.findViewById(R.id.email); final EditText password = (EditText) act.findViewById(R.id.password); getInstrumentation().runOnMainSync(new Runnable() { public void run() { email.setText("Engineer"); password.setText("2342"); } }); // Espresso.onView(ViewMatchers.withId(R.id.email)).perform(ViewActions.typeText("some shit")); // Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText("123")); Espresso.closeSoftKeyboard(); Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).check(ViewAssertions.matches(ViewMatchers.isDisplayed())); Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).perform(ViewActions.click()); Espresso.onView(ViewMatchers.withId(R.id.secondActivityOpened)).check(ViewAssertions.matches(ViewMatchers.isDisplayed())); Espresso.pressBack(); Espresso.closeSoftKeyboard(); Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).perform(ViewActions.click()); } Activity getCurrentActivity() { getInstrumentation().waitForIdleSync(); final Activity[] activity = new Activity[1]; try { runTestOnUiThread(new Runnable() { @Override public void run() { java.util.Collection<Activity> activites = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED); activity[0] = Iterables.getOnlyElement(activites); }}); } catch (Throwable throwable) { throwable.printStackTrace(); } return activity[0]; } class MyUserHelperExternalIdleRes implements Server { private Server aHelper; private CountingIdlingResource udleRes; public MyUserHelperExternalIdleRes(CountingIdlingResource udleRes, Server aHelper) { this.aHelper = aHelper; this.udleRes = udleRes; } @Override public void login(String email, String code, String phone, String password, boolean loginByPhoneNumber) { udleRes.increment(); try { aHelper.login(email,code, phone,password,loginByPhoneNumber); } finally { udleRes.decrement(); } } } 

}

So, if we are actually doing the basic espresso operation: Espresso.onView (ViewMatchers.withId (R.id.password)). Execute (ViewActions.typeText ("123"));

We receive PerformException: we cannot find the button on id. If we make a hook, (with the Runnable setting in the user interface thread) we will succeed with this simple demo. There are other errors with espresso in my main application (the β€œhack” written above did not work, we get the same error). Surely I have some very tricky mistake, I assume that something with the project settings (espresso) is wrong - I'm too bad with gradle. Please help me with this, or provide a link to an example application for Android Studio with espresso tests (I did not find any, all applications are poorly configured (without gradle), and after importing them into Android Studio I can not start them with

 gradlew connectedAndroidTest 
+2
source share
1 answer

I have the same problem, and I think that the main problem is that the button opens a new indent, but it seems that this in itself does not cause an error, which is very strange, this only happens when you typeText into EditText before by calling click ().

PS: I managed to do this, it seems that I had several problems with double dependency, as soon as I decided that the tests worked without any problems and without any work.

My build.gradle part of the espresso ended like this:

 dependencies { repositories { mavenCentral() } // Espresso compile 'org.apache.commons:commons-lang3:3.1' androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') { exclude group: 'com.google.guava' exclude module: 'espresso-idling-resource' } androidTestCompile('com.android.support.test:testing-support-lib:0.1') { exclude group: 'com.google.guava' exclude module: 'espresso-idling-resource' } } 

I also added this before the dependencies:

 configurations { androidTestCompile.exclude group: 'com.android.support' androidTestCompile.exclude module: 'support-v4' androidTestCompile.exclude module: 'appcompat-v7' } 
+1
source

Source: https://habr.com/ru/post/974268/


All Articles