如何在意式浓缩咖啡测试中按ID查看视图?

时间:2019-06-14 10:00:06

标签: java android view android-espresso

我不知道如何使用findViewById.在浓缩咖啡测试中从屏幕上查看视图

我要使用espresso自动测试进入应用程序的一个屏幕,然后我要检查屏幕是否正确。为此,我需要从屏幕上获取工具栏并检查他的一些属性。 首先,我尝试使用ViewInteractions对象进行此操作,但是我可以将其强制转换为View,并且无法访问对象属性。

我想使用findViewById直接获取视图,但是我可以理解如何正确使用它。

public void goToMyOrders (){
        BottomTabBar bottomTabBar = new BottomTabBar();
        bottomTabBar.profileButton.perform(click());
        myOrderButton.perform(click());
        ViewInteraction toolbarView = onView(allOf(withId(R.id.custom_font_toolbar)));
        toolbarView.getContext();
        toolbarView.check(matches(withText("Мои заказы")));
    }

2 个答案:

答案 0 :(得分:0)

不,我认为您对测试一些错误有个想法。首先,Toolbar不会扩展/继承TextView,因此它不能包含文本,但是您可以在TextView内放置一个Toolbar。我准备了简单的代码来向您展示使用Espresso进行自动化测试的想法。

创建一个新的示例应用程序,将此布局放入您的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_percent="0.3"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/colorAccent"/>

<TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/toolbar"
        app:layout_constraintBottom_toBottomOf="@id/toolbar"
        app:layout_constraintLeft_toLeftOf="@id/toolbar"
        app:layout_constraintRight_toRightOf="@id/toolbar"
        android:text="SomeTextBeforeChange"
        android:textColor="#FFFFFF"/>

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:text="Change text after click"/>

在您的MainActivity中:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = findViewById(R.id.button);
    final TextView textView = findViewById(R.id.textview);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setText("Changed text");
        }
    });
}

}

您看到的这个可能与您的View类似-ToolbarTextViewButton会改变TextView的文字。

现在使用Espresso进行自动测试。

  1. 在您的androidTest子文件夹中创建一个新的Class,将其命名为MainActivityTest

  2. 定义一个ActivityTestRule。在Android docs中进行了解 您可能还需要这些Gradle依赖项:

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    规则:
    @Rule public ActivityTestRule<MainActivity> rule = new ActivityTestRule<> (MainActivity.class);

    然后定义一个公共方法,其返回类型为void,并用@Test注释。 在此方法内,您可以通过调用TextView

  3. 来定义Espresso.onView(ViewMatchers.withId(your.textview.id));

要检查它是否包含指定的文本,您需要调用`textview.check(ViewAssertions.matches(ViewMatchers.withText(“ expected text”)));

我不会解释这些方法的确切作用,因为它们的名称说明了一切,但是如果您在理解它们时遇到困难,可以在StackOverflow上找到很多资源。对于上面的代码,我编写了以下简单测试,因此您可以根据需要进行分析:

public class MainActivityTest {

@Rule
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);

@Test
public void checkIfButtonChangesText(){
    // TextView
    ViewInteraction textView = Espresso.onView(ViewMatchers.withId(R.id.textview));
    // Button
    ViewInteraction button = Espresso.onView(ViewMatchers.withId(R.id.button));

    //check if TextView contains text: "SomeTextBeforeChange"
    textView.check(ViewAssertions.matches(ViewMatchers.withText("SomeTextBeforeChange")));

    // analogically with Button, text: "Change text after click"
    button.check(ViewAssertions.matches(ViewMatchers.withText("Change text after click")));

    // perform button click:
    button.perform(ViewActions.click());

    //Check if text has been changed to "Changed text"
    textView.check(ViewAssertions.matches(ViewMatchers.withText("Changed text")));

}

}

答案 1 :(得分:0)

除了ViewInteraction外,您还可以使用ViewAction通过findviewbyId获取视图。

对于YourCase,它就像:

Public View getView(int Rid) {
   onView(allOf(withId(Rid))).perform(new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(TextView.class);
        }

        @Override
        public String getDescription() {
            return "getting text from a TextView"; 
        }

        @Override
        public void perform(UiController uiController, View view) {
            TextView tv = (TextView) view; 
        }
    });
    return tv
}

注意:我将textview用作textview的约束。您也可以从ViewMatchers中使用其他约束

相关问题