欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Studio 3.0+ Record Espresso Test 自动化测试

程序员文章站 2022-10-08 20:47:39
准备工作 1.将android studio 版本升级到3.0+2.百度下载夜神模拟器 夜神模拟器的基本设置 PS:以上就是夜神模拟器的基本设置 Android Studio 连接夜神模拟器 开始录制自动测试代码 PS:操作模拟器 -> Record Your Test 弹框将自动生成你的行为代码 ......

准备工作

1.将android studio 版本升级到3.0+
2.百度下载夜神模拟器


夜神模拟器的基本设置

 

Android Studio 3.0+ Record Espresso Test 自动化测试

 

 

Android Studio 3.0+ Record Espresso Test 自动化测试

Android Studio 3.0+ Record Espresso Test 自动化测试

 

ps:以上就是夜神模拟器的基本设置

android studio 连接夜神模拟器

Android Studio 3.0+ Record Espresso Test 自动化测试

//夜神模拟器默认的地址
adb connect 127.0.0.1:62001

 

开始录制自动测试代码

在顶部工具栏找到run -> record espresso test -> 选择夜神模拟器双击启动 -> 启动后界面如下图

Android Studio 3.0+ Record Espresso Test 自动化测试

 

ps:操作模拟器 -> record your test 弹框将自动生成你的行为代码 -> 点击ok -> 命名并保存代码(可以一个页面一个页面的录制,然后生成一份一份的测试脚本)

运行自动化测试脚本

Android Studio 3.0+ Record Espresso Test 自动化测试

ps:静待安装,然后模拟器会自动执行你录制的动作,logcat可以查看日志


贴上一份 record espresso test的脚本看看

package com.example.administrator.teagarden.activity.login;


import android.support.test.espresso.viewinteraction;
import android.support.test.filters.largetest;
import android.support.test.rule.activitytestrule;
import android.support.test.runner.androidjunit4;
import android.view.view;
import android.view.viewgroup;
import android.view.viewparent;
import com.example.administrator.teagarden.r;
import org.hamcrest.description;
import org.hamcrest.matcher;
import org.hamcrest.typesafematcher;
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.closesoftkeyboard;
import static android.support.test.espresso.action.viewactions.replacetext;
import static android.support.test.espresso.matcher.viewmatchers.isdisplayed;
import static android.support.test.espresso.matcher.viewmatchers.withclassname;
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.hamcrest.matchers.is;

@largetest
@runwith(androidjunit4.class)
public class splashactivitytest {

    @rule
    public activitytestrule<splashactivity> mactivitytestrule = new activitytestrule<>(splashactivity.class);

    @test
    public void splashactivitytest() {
        // 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(6000);
        } catch (interruptedexception e) {
            e.printstacktrace();
        }

        viewinteraction appcompatedittext = onview(
                allof(withid(r.id.login_edit_user),
                        childatposition(
                                childatposition(
                                        withclassname(is("android.widget.linearlayout")),
                                        2),
                                0),
                        isdisplayed()));
        appcompatedittext.perform(click());

        viewinteraction appcompatedittext2 = onview(
                allof(withid(r.id.login_edit_user),
                        childatposition(
                                childatposition(
                                        withclassname(is("android.widget.linearlayout")),
                                        2),
                                0),
                        isdisplayed()));
        appcompatedittext2.perform(replacetext("152****3478"), closesoftkeyboard());

        viewinteraction appcompatedittext3 = onview(
                allof(withid(r.id.login_edit_mima),
                        childatposition(
                                allof(withid(r.id.logint_edit_layout),
                                        childatposition(
                                                withclassname(is("android.widget.linearlayout")),
                                                4)),
                                0),
                        isdisplayed()));
        appcompatedittext3.perform(replacetext("admin000"), closesoftkeyboard());

        viewinteraction appcompatbutton = onview(
                allof(withid(r.id.login_button), withtext("登录"),
                        childatposition(
                                childatposition(
                                        withclassname(is("android.widget.relativelayout")),
                                        1),
                                2),
                        isdisplayed()));
        appcompatbutton.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(3000);
        } catch (interruptedexception e) {
            e.printstacktrace();
        }

        viewinteraction radiobutton = onview(
                allof(withid(r.id.main_home_radio2), withtext("监控"),
                        childatposition(
                                allof(withid(r.id.main_tab_group),
                                        childatposition(
                                                withclassname(is("android.widget.linearlayout")),
                                                0)),
                                1),
                        isdisplayed()));
        radiobutton.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(3000);
        } catch (interruptedexception e) {
            e.printstacktrace();
        }
        
        viewinteraction radiobutton2 = onview(
                allof(withid(r.id.main_home_radio3), withtext("动态"),
                        childatposition(
                                allof(withid(r.id.main_tab_group),
                                        childatposition(
                                                withclassname(is("android.widget.linearlayout")),
                                                0)),
                                2),
                        isdisplayed()));
        radiobutton2.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(3000);
        } catch (interruptedexception e) {
            e.printstacktrace();
        }

        viewinteraction radiobutton3 = onview(
                allof(withid(r.id.main_home_radio4), withtext("我的"),
                        childatposition(
                                allof(withid(r.id.main_tab_group),
                                        childatposition(
                                                withclassname(is("android.widget.linearlayout")),
                                                0)),
                                3),
                        isdisplayed()));
        radiobutton3.perform(click());
    }

    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));
            }
        };
    }
}

ps:代码其实很简单,照搬照套,还有别的花样么...录制的时候,也就是操作模拟器,生成代码的时候有点卡....有没有人告诉我怎么解决?