Before adding any test case it is important that you follow a proper project structure. Click here to know where to add your Espresso instrumented test cases.
Video Tutorial -
By the end of this post you will be ready with your first Espresso test case. There are two ways to write test case suit in Espresso and this post will cover both of them for simple Hello World App.
First way is using Activity Instrumentation.Below is Simple test case which checks the UI Element with given ID is displayed after launching activity ->
1. The class uses two annotations @LargeTest and @Runwith these two annotations are used to specify the behavior for our TestClass.
2. The class extends AcitivtyInstrumentationTestCase2 class with launch activity as MainActivity which we want to test in this example.
3. @Before and @After is annotations and used to specify which method should be executed before each test case and which method should be executed after completion of each test case.
4. @Test annotation is added for each test case method.
5. In the below test case the getAcitivity() launches our Main Activity.
6. onView() is used to select a view for testing and withId() is used to locate the UI element and rest is used to check whether the particular element is displayed or not.
Second way is without using Activity Instrumentation ->
1. The class uses two annotations @LargeTest and @Runwith these two annotations are used to specify the behavior for our TestClass.
2.The below line of code is used to run the test case in particular order.
4.onView() is used to select a view for testing and withId() is used to locate the UI element and rest is used to check whether the particular element is displayed or not.
How to run these test cases ->
1. Click on "Build Variants".
2. Select Android Instrumentation Tests.
3. Select class name or test case name -> right click -> Run
Note -
add below code the defaultConfig before running the test case -
I hope this post helps you find your code coverage for your test suit :)
Please Share your feedback in comments section below and follow QA Automated to get latest post update.Happy Testing :-)
Video Tutorial -
By the end of this post you will be ready with your first Espresso test case. There are two ways to write test case suit in Espresso and this post will cover both of them for simple Hello World App.
First way is using Activity Instrumentation.Below is Simple test case which checks the UI Element with given ID is displayed after launching activity ->
1. The class uses two annotations @LargeTest and @Runwith these two annotations are used to specify the behavior for our TestClass.
2. The class extends AcitivtyInstrumentationTestCase2 class with launch activity as MainActivity which we want to test in this example.
3. @Before and @After is annotations and used to specify which method should be executed before each test case and which method should be executed after completion of each test case.
4. @Test annotation is added for each test case method.
5. In the below test case the getAcitivity() launches our Main Activity.
6. onView() is used to select a view for testing and withId() is used to locate the UI element and rest is used to check whether the particular element is displayed or not.
@RunWith(AndroidJUnit4.class) @LargeTest
public class EspressoTest1 extends ActivityInstrumentationTestCase2<MainActivity>{
public EspressoTest1() {
super(MainActivity.class);
}
@Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
}
@Test
public void test1ChatId() {
getActivity();
onView(withId(R.id.anuja)).check(matches(isDisplayed()));
}
@After public void tearDown() throws Exception {
super.tearDown();
}
}
Second way is without using Activity Instrumentation ->
1. The class uses two annotations @LargeTest and @Runwith these two annotations are used to specify the behavior for our TestClass.
2.The below line of code is used to run the test case in particular order.
@FixMethodOrder(MethodSorters.NAME_ASCENDING)3. @Before and @After is annotations and used to specify which method should be executed before each test case and which method should be executed after completion of each test case.
4.onView() is used to select a view for testing and withId() is used to locate the UI element and rest is used to check whether the particular element is displayed or not.
@RunWith(AndroidJUnit4.class) @LargeTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class EspressoTest2 { @Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>( MainActivity.class); @Before
public void setUp() throws Exception {
//Before Test case execution
} @Test
public void test1ChatId() { onView(withId(R.id.anuja)).check(matches(isDisplayed())); } @After
public void tearDown() throws Exception {
//After Test case Execution
}
}
How to run these test cases ->
1. Click on "Build Variants".
2. Select Android Instrumentation Tests.
First Espresso Test |
3. Select class name or test case name -> right click -> Run
Running First Espresso Test |
Note -
add below code the defaultConfig before running the test case -
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
I hope this post helps you find your code coverage for your test suit :)
Please Share your feedback in comments section below and follow QA Automated to get latest post update.Happy Testing :-)
Dont we need this in default config in build.gradle?
ReplyDeletetestInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Yes We need that you can do it in Edit run configuration as well while running test case
ReplyDeleteHey Anuja,
ReplyDeleteCan i know how to inspect Elements ?
Actually i am very new to Espresso Automation tool
Hi,
ReplyDeleteI am getting below error
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.(BlockJUnit4ClassRunner.java:65)
I am facing the same issue. Any help on the same?
DeleteHi i am new in Android app automation testing, we dont have application source code how can i test without source code of app i have APK file kindly suggest
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehow to launch the app from real device using expresso.. i have tried with your Sample one Hello world its working
ReplyDeletemega filmes hd apk
ReplyDeleteWhen I tried to used following assertion it gave following error.
ReplyDeleteonView(withId(R.id.edt_username)).check(matches((isDisplayed())));
o views in hierarchy found matching: with id: com.mytaxi.android_demo:id/edt_username
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:com.google.maps.api.android.lib6.impl.cj{23cd661 G.ED..C.. ......I. 0,0-0,0}
Yes I use FMWA
Deletesports whatsapp groups
ReplyDeletepublic ActivityTestRule mActivityRule = new ActivityTestRule<>(
ReplyDeleteMainActivity.class);
ActivityTestRule which class it is in your case
Very nice blog. It is very useful for us. Thanks for good information.Visit here :cyberflix tv android
ReplyDeleteI Love your article. You can visit my website : yesmovies.go
ReplyDelete