我刚刚开始为现有的 android 应用程序进行 UI 测试,我确实按照文档设置了 espresso,我也在测试设备上禁用了动画。
然后我在里面创建了一个新的测试类src/androidTest
,内容如下:
package com.myApp.customer.ui;
import androidx.test.espresso.Espresso;
import androidx.test.espresso.ViewInteraction;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import static androidx.test.espresso.action.ViewActions.*;
import static androidx.test.espresso.assertion.ViewAssertions.*;
import static androidx.test.espresso.matcher.ViewMatchers.*;
import com.shahry.customer.R;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.is;
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityScenarioRule<MainActivity> activityRule
= new ActivityScenarioRule<>(MainActivity.class);
@Test
public void checkBrowseButton_IsDisplayed(){
ViewInteraction materialButton = Espresso.onView(
allOf(withId(R.id.bBrowse), withText("Browse products"),
isDisplayed()));
materialButton.check(matches(withText("Shop Online")));
}
@Test
public void mainActivityTest() {
ViewInteraction materialButton = Espresso.onView(
allOf(withId(R.id.bBrowse), withText("Browse products"),
isDisplayed()));
materialButton.perform(click());
ViewInteraction linearLayout = Espresso.onView(
allOf(withId(R.id.wPayOffline),
withParent(allOf(withId(R.id.llAppBar),
withParent(IsInstanceOf.<View>instanceOf(android.view.ViewGroup.class)))),
isDisplayed()));
linearLayout.check(matches(isDisplayed()));
ViewInteraction textView = Espresso.onView(
allOf(withId(R.id.tvShopOnline), withText("Shop Online"),
withParent(allOf(withId(R.id.llAppBar),
withParent(IsInstanceOf.<View>instanceOf(android.view.ViewGroup.class)))),
isDisplayed()));
textView.check(matches(withText("Shop Online")));
}
}