Reputation: 71
it is possible to write a Espresso test in android studio outside the application's code ? i read in the Espresso documentation that is possible to create a black-box tests with espresso but i dont find how to do it
Upvotes: 2
Views: 619
Reputation: 1
Yes, It is possible to write an Espresso test in the android studio outside the application's code.
All the test classes excluding instrumental test should be in the (test) folder as shown in the picture below
Below is an example test class that checks whether the login is correct or not using the espresso test.
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> activityRule
= new ActivityTestRule<>(MainActivity.class);
@Test
public void testLoginSuccess() {
onView(withId(R.id.et_username)).perform(typeText("testuser"), closeSoftKeyboard());
onView(withId(R.id.et_password)).perform(typeText("test123"), closeSoftKeyboard());
onView(withId(R.id.btn_login)).perform(click());
onView(withId(R.id.tv_welcome)).check(matches(isDisplayed()));
}
}
I wrote an explanation about it in my blog with a simple login example here.
Upvotes: 0
Reputation: 2200
You can use UI Automator for cross-app functional UI testing, for example, interacting with a GPS dialog box created using SettingsAPI. Read more about it here.
Click the OK button like this :-
/** UI Automator Google GPS dialog box */
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject uiObject = mDevice.findObject(new UiSelector().text("OK"));
try {
uiObject.click();
}catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
Upvotes: 1
Reputation: 71
Create a separate project with the following dependencies:
compile 'com.android.support.test.espresso:espresso-core:2.2.2'
compile 'com.android.support.test:runner:0.5'
compile 'junit:junit:4.12'
i Use this manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.test.test"xmlns:android="http://schemas.android.com/apk/res/android">
<instrumentation
android:name="android.support.test.runner.AndroidJUnitRunner"
android:targetPackage="com.imc.imc" >
<instrumentation>
</manifest>
i create a new class in src/main/java/com.test.test named MainActivityTest
public class MainActivityTest {
@Rule
public ActivityTestRule<?> mActivityRule = newActivityTestRule("com.imc.imc.MainActivity");
@NonNull
private ActivityTestRule newActivityTestRule(String className) {
return new ActivityTestRule(activityClass(className));
}
private static Class<? extends Activity> activityClass(String className) {
try {
return (Class<? extends Activity>) Class.forName(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
private static int getId(String id) {
Context targetContext = InstrumentationRegistry.getTargetContext();
String packageName = targetContext.getPackageName();
return targetContext.getResources().getIdentifier(id, "id", packageName);
}
@Test
public void mytest() {
onView(withId(getId("button"))).perform(click());;
}
}
but it does not work !!!!!!
Upvotes: 1
Reputation: 437
Take a look at our discussion thread at reddit, there is one solution for this.
Also I am developing a plugin for Android Studio that will make this easy. You can subscribe for news about it and I will notify you when it will be ready.
Upvotes: 0