Android自动化测试框架Espresso(三)——View Action的使用

今天的文章给大家简单介绍一下Espresso进行测试时,如何执行相应的操作?我们的功能很简单,是界面上有一个EditText和一个按钮,EditText中可以输入内容,点击按钮时,内容将被写入SharedPreferences。不多说,看看我们的测试代码

package com.yjp.espressoperformdemo;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withHint;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.junit.Assert.assertEquals;

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

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

    @Test
    public void testMainActivity() {
        Context context = InstrumentationRegistry.getTargetContext();

        //从SharedPreferences取出值
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        String key = context.getResources().getString(R.string.edit_text_content_sp_key);
        String editTextContent = sp.getString(key, "");

        //内容应该为空
        assertEquals(editTextContent, "");

        //看到一个输入内容的EditText,提示信息为请输入要写入的内容
        //然后在EditText中写入内容
        onView(allOf(withId(R.id.content_edit_text), withHint(R.string.content_edit_text_hint)))
                .check(matches(isDisplayed()))
                .perform(replaceText("测试内容"));

        //看到写入按钮并点击
        onView(allOf(withId(R.id.write_to_button), withText(R.string.write_to)))
                .check(matches(isDisplayed()))
                .perform(click());

        //检查SharedPreferences内容是否写入
        editTextContent = sp.getString("edit_text_content", "");
        assertEquals(editTextContent, "测试内容");

        //清空SharedPreferences
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, "");
        editor.apply();
    }
}

很多大家都接触过,下面重点说明几点:

1. 使用InstrumentationRegistry.getTargetContext()可以获取我们的Context

2. 使用withHint()可以检查View的hint的内容

3. 执行操作,就是调用perform()然后传入对应的ViewAction,常用的ViewAction如下图所示。



现在的功能测试中,因为我们没有定义布局和字符串资源,还无法通过编译,接下来我们创建布局和字符串资源。布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    tools:context="com.yjp.espressoperformdemo.MainActivity">

    <EditText
        android:id="@+id/content_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/content_edit_text_hint" />

    <Button
        android:id="@+id/write_to_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/write_to" />

</LinearLayout>

字符串资源

<string name="content_edit_text_hint">请输入内容</string>
<string name="write_to">写入</string>
<string name="edit_text_content_sp_key">edit_text_content</string>

这时候,运行测试用例,会报错,因为现在我们点击按钮不会往SharedPreferences写入内容,实现MainActivity

package com.yjp.espressoperformdemo;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText mContentEditText;

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

        mContentEditText = (EditText) findViewById(R.id.content_edit_text);
        Button writeToButton = (Button) findViewById(R.id.write_to_button);
        writeToButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = mContentEditText.getText().toString();
                SharedPreferences sp =
                        PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                SharedPreferences.Editor editor = sp.edit();
                String key = getResources().getString(R.string.edit_text_content_sp_key);
                editor.putString(key, content);
                editor.apply();
            }
        });
    }
}

再次运行测试,通过。Espresso执行操作,主要就是给perform传入对应的View Action即可。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值