Chih-Kai Yu
Chih-Kai Yu

Reputation: 21

Android Espresso black-box testing

Does anyone try to do black-box testing with Android Espresso?

Could anyone provides me with some simple example?

I had tried some example before, but failed every time!

Example, I had tried this one:

public class ApplicationTest extends ActivityInstrumentationTestCase2
{

private static final String ACTIVITY_CLASSNAME = "com.example.kai_yu.blackboxtest";

private static Class launchActivityClass;
static
{
    try
    {
        launchActivityClass = Class.forName(ACTIVITY_CLASSNAME);
    }
    catch (ClassNotFoundException e)
    {
        throw new RuntimeException(e);
    }
}

public ApplicationTest()
{
    super(launchActivityClass);
}

@Test
public void testClick()
{

}

}

But Android Studio said:

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.kai_yu.blackboxtest"

com.example.kai_yu.blackboxtest is applicationId which is another installed application on my phone

Thank you!

Upvotes: 1

Views: 2987

Answers (3)

anuja jain
anuja jain

Reputation: 1387

@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();
        }
}

There are two ways to write Espresso Test case one is as per shown above The Examples are taken from this blog http://qaautomated.blogspot.in/p/blog-page.html

Where you can find details of hot to run the espresso test case in detail.

Upvotes: 0

piotrek1543
piotrek1543

Reputation: 19351

In Espresso docs you would find this line:

 While it can be used for black-box testing, Espresso's full power is unlocked by those who are familiar with the code base under test."

For that reason Espresso testing is called by gray-box testing.

If you're not familiar with programming in Java or Android, or you want just to write black-box testing in the clearest way as possible try to learn instead of Espresso this framework

Calabash-iOS and Calabash-Android are the underlying low-level libraries that empower the Cucumber tool to run automated functional tests on Android...

Website: https://calaba.sh/

GitHub: https://github.com/calabash

Here would you find how and why to start using this framework: http://blog.teddyhyde.com/2013/11/04/a-better-way-to-test-android-applications-using-calabash/

Upvotes: 0

yogurtearl
yogurtearl

Reputation: 3065

Espresso can only run as part of an instrumentation test. Instrumentation tests can only act upon the app under test ( i.e. the target of the instrumentation ).

UIAutomator might be better for your use case.

https://developer.android.com/tools/testing-support-library/index.html#UIAutomator

Upvotes: 1

Related Questions