Android Junit单元测试 基于Uiautomator UI测试(三)

  • 之前提到在androidTest中进行的是UI测试,这次使用的是Uiautomator,使用Java进行Ui测试

  • 打开uiautomator viewer(sdk\tools\bin下)
    在这里插入图片描述

    • 在androidTest中创建class->UiTest
    • 方法名一定要以Test开头
package com.example.adminstator.myapplication;

import android.app.Instrumentation;
import android.content.Context;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import junit.framework.TestCase;


/**
* Created with Android Studio.
* Description:
* User: wjx
* Date: 2019-05-09
* Time: 12:30
*/
public class UiTest extends TestCase {


    public void testA() throws UiObjectNotFoundException, RemoteException {
        //获取设备
        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        UiDevice uiDevice = UiDevice.getInstance(instrumentation);


        //获取上下文
        Context context = instrumentation.getContext();
//        uiDevice.click(102, 227);
        //先回到主界面
        uiDevice.pressHome();
        //获取浏览器对象,并单击
        UiObject browserObject = new UiObject(new UiSelector().text("浏览器"));
        browserObject.clickAndWaitForNewWindow();


        //获取搜索框对象,并单击
        UiObject editObject =  new UiObject(new UiSelector().className("android.widget.EditText"));
        editObject.click();
        uiDevice.pressDelete();
        editObject.setText("www.baidu.com");
        uiDevice.pressEnter();
        //等待网页
        uiDevice.sleep();
    }
}

  • UiDevice类介绍

    • UiDevice代表设备状态
    • UiDevice功能:
      • 获取设备信息:屏幕分辨率,旋转状态,亮灭屏状态等
      • 操作:按键,坐标 操作,滑动,拖拽,灭屏唤醒屏幕,截图等。
      • 监听器的功能
  • 按键与KeyCode使用

    • 手机常见按键:
      • Home
      • Menu
      • Back
      • Volume_up
      • Volume_Down
      • ReventApps
      • Power
      • Dpad //轨迹球太老旧
public void testB() throws UiObjectNotFoundException{        
       //获取UiDevice的方式        
       UiDevice uiDevice = 
       UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
       // getUiDevice().pressHome();此方法容易出现空指针异常     
       UiDevice.getInstance().pressMenu();   
}
  • KEYCODE 键盘映射码:
    • KeyEvent按键事件
    • META Key
      • 辅助功能键:ALT, SHIT, CAPS_LOCK
    public void testC() throws UiObjectNotFoundException{
        //获取UiDevice的方式
        UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        //输入小写abc
        uiDevice.pressKeyCode(KeyEvent.KEYCODE_A);
        uiDevice.pressKeyCode(KeyEvent.KEYCODE_B);
         uiDevice.pressKeyCode(KeyEvent.KEYCODE_C);
        //输入大写ABC
        uiDevice.pressKeyCode(KeyEvent.KEYCODE_A,1);
        uiDevice.pressKeyCode(KeyEvent.KEYCODE_B,1);
        uiDevice.pressKeyCode(KeyEvent.KEYCODE_C,1);
    }
}



在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wjxbless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值