Espresso的入门及使用

什么是Espresso?

Espresso? 咦?它不是一个咖啡的名字吗?怎么就变成测试的名称了呢?
哦~~,原来是这样——设计者的意图是想实现“喝杯咖啡的功夫”就可以等待自动测试完成。它可以实现UI自动化测试。在我们日常生活中的,大家肯定想用代码解决任何事情,能不用手操作就不用手,而利用Espresso可以减轻人手操作,自动为你实现。

Espresso API文档使用

Espresso 面向认为自动化测试是开发生命周期不可或缺的一部分的开发者。虽然 Espresso 可用于黑盒测试,但熟悉被测代码库的人员可以开启它的全部功能。

官方的API文档https://developer.android.google.cn/training/testing/espresso

依赖配置

在 build gradle (Moudle app)的dependencies里添加依赖

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation "com.android.support.test.espresso:espresso-contrib:3.0.2"
    androidTestImplementation "com.android.support.test.espresso:espresso-idling-resource:3.0.2"
    androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.2"

在 build gradle (Moudle app)的defaultConfig里添加依赖

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

那么恭喜你依赖配置完成咯~~~~~

实例展示

环境配置好了,接下来通过一个实例来讲一下Espresso到底怎么用。我们这次对一个登陆跳转界面进行测试。

Step 1、先创一个LoginActivity。在activity_login.xml文件里

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:">
        </TextView>

        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入账号">
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:">
        </TextView>

        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码">
        </EditText>
    </LinearLayout>
    <Button
        android:id="@+id/btn_login"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录">
    </Button>
</LinearLayout>

在LoginActivity里

package com.example.fourdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
    private EditText et_account;
    private EditText et_password;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        et_account =findViewById(R.id.et_account);
        et_password =findViewById(R.id.et_password);
        btn_login =findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                accountLogin();
            }
        });
    }

    private void accountLogin(){
        String account = et_account.getText().toString();
        String password = et_password.getText().toString();
        if (TextUtils.isEmpty(account)) {
            Toast.makeText(this, "用户名为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "密码为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (password.length() < 6) {
            Toast.makeText(this, "密码长度小于6", Toast.LENGTH_SHORT).show();
            return;
        }
        if(account.equals("admin") && password.equals("123456")){
            Intent intent = new Intent(LoginActivity.this,JumpActivity.class);
            startActivity(intent);
            finish();
        }else {
            Toast.makeText(LoginActivity.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
        }
    }
}

Step 2、创建SecondActivity,用于跳转,为了方便其跳转显示内容,在其对应的xml文件添加一个textview,用于文字显示,这里我就不写啦。

Step 3、在LoginActivity点Go To Test ,不会的可以查看我之前写的博客https://blog.csdn.net/weixin_45552475/article/details/107481416

创建完成,我们就要写测试代码了,我先把代码贴出来,后面再详细讲解。

package com.example.fourdemo;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.clearText;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.junit.Assert.*;

@RunWith(AndroidJUnit4.class)
@LargeTest//允许测试需要较大消耗
public class LoginActivityTest {

    //指定测试的目标Activity页面
    @Rule
    public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(LoginActivity.class);

    @Test
    public void testLogin() {
        //验证是否显示
        onView(withId(R.id.btn_login)).check(matches(isDisplayed()));

        //不输入任何内容,直接点击登录按钮
        onView(withId(R.id.btn_login)).perform(click());
        //onView(allOf(withId(R.id.btn_login), isDisplayed())).perform(click());

        //只输入用户名
        onView(withId(R.id.et_account)).perform(typeText("admin"), closeSoftKeyboard());
        onView(withId(R.id.btn_login)).perform(click());
        onView(withId(R.id.et_account)).perform(clearText());

        //同时输入用户名和密码,但是密码格式不正确
        onView(withId(R.id.et_account)).perform(typeText("admin"));
        onView(withId(R.id.et_password)).perform(typeText("123"), closeSoftKeyboard());
        onView(withId(R.id.btn_login)).perform(click());
        onView(withId(R.id.et_account)).perform(clearText());
        onView(withId(R.id.et_password)).perform(clearText());

        //输入正确的用户名和密码
        onView(withId(R.id.et_account)).perform(typeText("admin"));
        onView(withId(R.id.et_password)).perform(typeText("123456"), closeSoftKeyboard());
        onView(withId(R.id.btn_login)).perform(click());
    }
}
  • 首先@RunWith(AndroidJUnit4.class),意思是基于JUnit 4进行测试的。@LargeTest//允许测试需要较大消耗

  • 在LoginActivityTest 类里面,我们首先肯定要指定测试的目标是哪个activity界面, @Rule public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(LoginActivity.class);

  • 接下来就是定义一个testLogin()的方法用来测试,在方法里面通过id找到btn_login,用onView(withId(R.id.btn_login)).check(matches(isDisplayed()));检测元素是否可见。

  • 在对Edittext进行文本设置的时候,使用 onView(withId(R.id.edit_name)).perform(typeText("admin"), closeSoftKeyboard());,让其自动打出admin,不过这里有一点需要重视一下。如果你想输入中文目前还做不到通过键盘的方式输入(因为Espresso不知道哪些按键可以输出你要的文字),所以中文只能用replaceText的方法:onView(withId(R.id.edit_name)).perform(replaceText("管理"));

  • 然后就是清理Edittext
    onView(withId(R.id.edit_name)).perform(clearText());

  • 接下来编写代码同时输入用户名和密码,但是密码格式不正确、输入正确的用户名和密码。就可以根据自己的要求对其进行测试了。run 一下LoginActivityTest。

  • 运行结果:系统就可以自动打开模拟器,然后根据你自己设置的代码行为,进行测试啦!!!下面过程都是自动的哦,不需要手动。

控制台显示为
在这里插入图片描述

注解:Espresso 由以下三个基础部分组成:

ViewMatchers 在当前View层级去匹配指定的View
ViewActions 执行Views的某些行为
ViewAssertions 检查Views的某些状态

获取View:

  • onView(withId(R.id.my_view))——根据id匹配
  • onView(withText(“Hello World!”))——根据文本匹配

执行View的行为:

  • 点击——onView(…).perform(click());
  • 输入文本——onView(…).perform(typeText(“Hello World”), closeSoftKeyboard());
  • 滑动(使屏幕外的view显示) 点击——onView(…).perform(scrollTo(), click());
  • 清除文本——onView(…).perform(clearText());

检验View内容:

  • 检验View的本文内容是否匹配“Hello World!”——onView(…).check(matches(withText(“Hello World!”)));
  • 检验View的内容是否包含“Hello World!”——onView(…).check(matches(withText(containsString(“Hello World!”))));
  • 检验View是否显示——onView(…).check(matches(isDisplayed()));
  • 检验View是否隐藏——onView(…).check(matches(not(isDisplayed())));
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值