使用android espresso同时呼叫

时间:2018-07-10 12:15:21

标签: android mockito android-espresso

我正在为我的应用创建咖啡测试。 在我的应用程序中,api中有两个调用,一个接一个。

第一个调用是登录,可以正常工作,它正在调用模拟服务器并带来数据mokados。

第二个调用是带来有关该登录用户的请求的一些数据,但不会影响模拟API。

有人可以帮我吗?

@LargeTest
@RunWith(AndroidJUnit4.class)
public class StartActivityTest {

@Rule
public ActivityTestRule<StartActivity> mActivityTestRule = new ActivityTestRule<>(StartActivity.class);

private UiDevice device;
private MockWebServer server;


@Before
public void setUp() throws Exception {
    server = new MockWebServer();
    server.start(8681);

    this.device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}

@Test
public void startActivityTest() throws Exception {
    String fileName = "login.json";
    server.enqueue(
            new MockResponse()
                    .setResponseCode(200)
                    .setBody(RestServiceTestHelper.getStringFromFile(getInstrumentation().getContext(), fileName)));

    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    ViewInteraction appCompatEditText = onView(
            allOf(withId(R.id.txt_username),
                    childAtPosition(
                            childAtPosition(
                                    withId(R.id.txt_username_layout),
                                    0),
                            0),
                    isDisplayed()));
    appCompatEditText.perform(click());

    ViewInteraction appCompatEditText2 = onView(
            allOf(withId(R.id.txt_username),
                    childAtPosition(
                            childAtPosition(
                                    withId(R.id.txt_username_layout),
                                    0),
                            0),
                    isDisplayed()));
    appCompatEditText2.perform(replaceText("11111111111"), closeSoftKeyboard());

    ViewInteraction appCompatEditText3 = onView(
            allOf(withId(R.id.txt_password),
                    childAtPosition(
                            childAtPosition(
                                    withId(R.id.txt_password_layout),
                                    0),
                            0),
                    isDisplayed()));
    appCompatEditText3.perform(replaceText("1234"), closeSoftKeyboard());

    closeSoftKeyboard();

    ViewInteraction appCompatButton = onView(
            allOf(withId(R.id.btn_login), withText("Acessar"),
                    childAtPosition(
                            childAtPosition(
                                    withId(R.id.login_form),
                                    0),
                            4)));
    appCompatButton.perform(scrollTo(), click());

    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    **// HERE WAS THE CALL FOR THE ORDER DATA API**


    allowPermissionsIfNeeded();

    ViewInteraction floatingActionButton = onView(
            allOf(childAtPosition(
                    allOf(withId(R.id.btn_main_menu),
                            childAtPosition(
                                    withId(R.id.layout_home),
                                    2)),
                    3),
                    isDisplayed()));
    floatingActionButton.perform(click());

    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    try {
        Thread.sleep(250);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    ViewInteraction floatingActionButton2 = onView(
            allOf(childAtPosition(
                    allOf(withId(R.id.btn_main_menu),
                            childAtPosition(
                                    withId(R.id.layout_home),
                                    2)),
                    3),
                    isDisplayed()));
    floatingActionButton2.perform(click());

    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    try {
        Thread.sleep(250);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());

    ViewInteraction appCompatTextView = onView(
            allOf(withId(R.id.title), withText("Sair"),
                    childAtPosition(
                            childAtPosition(
                                    withClassName(is("android.support.v7.view.menu.ListMenuItemView")),
                                    0),
                            0),
                    isDisplayed()));
    appCompatTextView.perform(click());

    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    ViewInteraction editText = onView(
            allOf(withId(R.id.txt_username), withText("11111111111"),
                    childAtPosition(
                            childAtPosition(
                                    withId(R.id.txt_username_layout),
                                    0),
                            0),
                    isDisplayed()));
    editText.check(matches(withText("11111111111")));

}

private static Matcher<View> childAtPosition(
        final Matcher<View> parentMatcher, final int position) {

    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("Child at position " + position + " in parent ");
            parentMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
            return parent instanceof ViewGroup && parentMatcher.matches(parent)
                    && view.equals(((ViewGroup) parent).getChildAt(position));
        }
    };
}

private void allowPermissionsIfNeeded() {
    if (Build.VERSION.SDK_INT >= 23) {
        UiObject allowPermissions = device.findObject(new UiSelector().text("Permitir"));
        if (allowPermissions.exists()) {
            try {
                allowPermissions.click();
            } catch (UiObjectNotFoundException e) {
            }
        }
    }
}

@After
public void tearDown() throws Exception {
    server.shutdown();
}

}

我在调用有关请求的API的那一刻就在代码中做了标记

** //在这里调用ORDER DATA API **

0 个答案:

没有答案