Android自动化测试

一个简单的自动化测试代码

这个代码实现了Android自动化单击APP打开,并进行文本输入、按钮单击等操作。

下面是简单设计的Android UI

ExampleInstrumentedTest 自动化测试代码
package com.example.text;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.filters.SdkSuppress;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.Until;

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

import static org.junit.Assert.*;

import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

import java.text.BreakIterator;

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class ExampleInstrumentedTest {

    private static final String BASIC_SAMPLE_PACKAGE = "com.example.text";   //你自己写的APK文件的包路径
    private UiDevice mDevice;

    @Before
    public void setUp() {  //创建了一个 UiDevice 实例,用于后续的设备交互操作,setup会在每个测试方法运行前执行一次
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    }

    @Test
    public void launchChromeFromHomeScreen() throws UiObjectNotFoundException, InterruptedException {
        // 返回主屏幕
        mDevice.pressHome();

        Context context = ApplicationProvider.getApplicationContext();
        final Intent intent = context.getPackageManager().getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);

        //使用 ApplicationProvider.getApplicationContext() 获取当前应用的上下文对象。

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent); //启动应用

        UiObject usernameField=mDevice.findObject(new UiSelector().resourceId("com.example.text:id/main_yonhuming")); 
//使用 UiDevice 的 findObject 方法查找指定资源ID的UI对象
        assertNotNull(usernameField); //使用 assertNotNull 断言来确保找到的对象不是 null
        usernameField.click(); //调用 click 方法点击该对象
        usernameField.setText("admin"); //调用 setText 方法输入文本

        UiObject emailField=mDevice.findObject(new UiSelector().resourceId("com.example.text:id/main_youxiang"));
        assertNotNull(emailField);
        emailField.click();
        emailField.setText("123@neu.com");

        UiObject phoneField=mDevice.findObject(new UiSelector().resourceId("com.example.text:id/main_shujihao"));
        assertNotNull(phoneField);
        phoneField.click();
        phoneField.setText("12345678");

        UiObject passwordField=mDevice.findObject(new UiSelector().resourceId("com.example.text:id/main_password"));
        assertNotNull(passwordField);
        passwordField.click();
        passwordField.setText("123456");

        UiObject paswField=mDevice.findObject(new UiSelector().resourceId("com.example.text:id/main_pasw"));
        assertNotNull(paswField);
        paswField.click();
        paswField.setText("123456");
        mDevice.pressEnter();

        UiObject accpentField=mDevice.findObject(new UiSelector().resourceId("com.example.text:id/main_accpent"));
        assertNotNull(accpentField);
        accpentField.click();

        UiObject main_login_usernameField=mDevice.findObject(new UiSelector().resourceId("com.example.text:id/main_login_username"));
        assertNotNull(main_login_usernameField);
        main_login_usernameField.click();
        main_login_usernameField.setText("admin");

        UiObject main_login_passwordField=mDevice.findObject(new UiSelector().resourceId("com.example.text:id/main_login_password"));
        assertNotNull(main_login_passwordField);
        main_login_passwordField.click();
        main_login_passwordField.setText("123456");
        mDevice.pressEnter();

        UiObject main_loginField=mDevice.findObject(new UiSelector().resourceId("com.example.text:id/main_login"));
        assertNotNull(main_loginField);
        main_loginField.click();

        Thread.sleep(5000); // 暂停5秒

        mDevice.pressHome();

    }

}

主要就是以下几行代码

public class LoginText {


    private static final  String BISS="com.example.text";
    private UiDevice mdevice;

    @Before
    public void setup(){
        mdevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    }

    @Test
    public void launchChromeFromHomeScreen() throws UiObjectNotFoundException, InterruptedException {
        mdevice.pressHome();
        Context context= ApplicationProvider.getApplicationContext();
        final Intent intent=context.getPackageManager().getLaunchIntentForPackage(BISS);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);

        UiObject userbtn=mdevice.findObject(new UiSelector().resourceId("com.example.text:id/main"));
        assertNotNull(userbtn);
        userbtn.click();
        userbtn.setText("dcgfsdfh");

        mdevice.pressEnter();

        Thread.sleep(5000);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值