Android测试工具 UIAutomator入门与介绍(下)

简介:   UI Automator 测试工具定义以及用途

下面列举几个类的方法      


UiDevice


void    clearLastTraversedText()
// 清除上次UI遍历的事件
boolean click(int x, int y)
// 根据坐标点击
boolean drag(int startX, int startY, int endX, int endY, int steps)
// 拖动
void    dumpWindowHierarchy(File dest)
// dump当前的层次化结构到文件中
void    dumpWindowHierarchy(OutputStream out)
// dump当前的层次化结构到流中
void    dumpWindowHierarchy(String fileName)
// dump当前的层次化结构到文件中
UiObject2   findObject(BySelector selector)
// 根据BySelector查找
UiObject    findObject(UiSelector selector)
// 根据UiSelector 查找
List<UiObject2> findObjects(BySelector selector)
// 根据BySelector查找
void    freezeRotation()
// 冻结旋转的状态
String  getCurrentActivityName()
// 获取当前Activity的名字,已经被废弃
String  getCurrentPackageName()
// 获取当前package
int getDisplayHeight()
// Gets the height of the display, in pixels.
int getDisplayRotation()
// Returns the current rotation of the display, as defined in Surface
Point   getDisplaySizeDp()
// Returns the display size in dp (device-independent pixel) The returned display size is adjusted per screen rotation.
int getDisplayWidth()
// Gets the width of the display, in pixels.
static UiDevice getInstance()
// 获取一个对象
static UiDevice getInstance(Instrumentation instrumentation)
// Retrieves a singleton instance of UiDevice
String  getLastTraversedText()
// 获取上一次遍历的文本
String  getLauncherPackageName()
// 获取运行的packagename
String  getProductName()
// Retrieves the product name of the device.
boolean hasAnyWatcherTriggered()
// 检查是否有触发器触发
boolean hasObject(BySelector selector)
// 是否有符合的条件的
boolean hasWatcherTriggered(String watcherName)
// Checks if a specific registered UiWatcher has triggered.
boolean isNaturalOrientation()
// Check if the device is in its natural orientation.
boolean isScreenOn()
// Checks the power manager if the screen is ON.
boolean openNotification()
// Opens the notification shade.
// 打开通知
boolean openQuickSettings()
// 打开设置
<R> R   performActionAndWait(Runnable action, EventCondition<R> condition, long timeout)
// Performs the provided action and waits for the condition to be met.
boolean pressBack()
// Simulates a short press on the BACK button.
boolean pressDPadCenter()
// Simulates a short press on the CENTER button.
boolean pressDPadDown()
// Simulates a short press on the DOWN button.
boolean pressDPadLeft()
// Simulates a short press on the LEFT button.
boolean pressDPadRight()
// Simulates a short press on the RIGHT button.
boolean pressDPadUp()
// Simulates a short press on the UP button.
boolean pressDelete()
// Simulates a short press on the DELETE key.
boolean pressEnter()
// Simulates a short press on the ENTER key.
boolean pressHome()
// Simulates a short press on the HOME button.
boolean pressKeyCode(int keyCode)
// Simulates a short press using a key code.
boolean pressKeyCode(int keyCode, int metaState)
// Simulates a short press using a key code.
boolean pressMenu()
// Simulates a short press on the MENU button.
boolean pressRecentApps()
// Simulates a short press on the Recent Apps button.
boolean pressSearch()
// Simulates a short press on the SEARCH button.
void    registerWatcher(String name, UiWatcher watcher)
// Registers a UiWatcher to run automatically when the testing framework is unable to find a match using a UiSelector.
void    removeWatcher(String name)
// Removes a previously registered UiWatcher.
void    resetWatcherTriggers()
// Resets a UiWatcher that has been triggered.
void    runWatchers()
// This method forces all registered watchers to run.
void    setCompressedLayoutHeirarchy(boolean compressed)
// Enables or disables layout hierarchy compression.
void    setOrientationLeft()
// 设置旋转方向
void    setOrientationNatural()
// Simulates orienting the device into its natural orientation and also freezes rotation by disabling the sensors.
void    setOrientationRight()
// Simulates orienting the device to the right and also freezes rotation by disabling the sensors.
void    sleep()
// 关闭屏幕
boolean swipe(int startX, int startY, int endX, int endY, int steps)
// Performs a swipe from one coordinate to another using the number of steps to determine smoothness and speed.
boolean swipe(Point[] segments, int segmentSteps)
// Performs a swipe between points in the Point array.
boolean takeScreenshot(File storePath, float scale, int quality)
// 截屏
boolean takeScreenshot(File storePath)
// Take a screenshot of current window and store it as PNG Default scale of 1.0f (original size) and 90% quality is used The screenshot is adjusted per screen rotation
void    unfreezeRotation()
// Re-enables the sensors and un-freezes the device rotation allowing its contents to rotate with the device physical rotation.
<R> R   wait(SearchCondition<R> condition, long timeout)
// Waits for given the condition to be met.
void    waitForIdle(long timeout)
// Waits for the current application to idle.
void    waitForIdle()
// Waits for the current application to idle.
boolean waitForWindowUpdate(String packageName, long timeout)
// Waits for a window content update event to occur.
void    wakeUp()
// 点亮屏幕


UiObject


void    clearTextField()
// 清空输入接口
boolean click()
// 点击
boolean clickAndWaitForNewWindow()
// 点击并等待新界面
boolean clickAndWaitForNewWindow(long timeout)
// 点击并等待新界面,设置等待时间
boolean clickBottomRight()
// 点击右下边
boolean clickTopLeft()
// Clicks the top and left corner of the UI element
boolean dragTo(UiObject destObj, int steps)
// 拖动
boolean dragTo(int destX, int destY, int steps)
// Drags this object to arbitrary coordinates.
boolean exists()
// 判断是否存在
Rect    getBounds()
// 返回边界
UiObject    getChild(UiSelector selector)
// 根据条件获取子元素
int getChildCount()
// 获取子元素数量
String  getClassName()
// 获取当前元素的class name
String  getContentDescription()
// Reads the content_desc property of the UI element
UiObject    getFromParent(UiSelector selector)
// Creates a new UiObject for a sibling view or a child of the sibling view, relative to the present UiObject.
String  getPackageName()
// Reads the view's package property
final UiSelector    getSelector()
// Debugging helper.
String  getText()
// Reads the text property of the UI element
Rect    getVisibleBounds()
// 获取可见边界
boolean isCheckable()
// 是否可以点击
boolean isChecked()
// 是否已经选中
boolean isClickable()
// Checks if the UI element's clickable property is currently true.
boolean isEnabled()
// Checks if the UI element's enabled property is currently true.
boolean isFocusable()
// Check if the UI element's focusable property is currently true.
boolean isFocused()
// Check if the UI element's focused property is currently true
boolean isLongClickable()
// Check if the view's long-clickable property is currently true
boolean isScrollable()
// Check if the view's scrollable property is currently true
boolean isSelected()
// Checks if the UI element's selected property is currently true.
boolean longClick()
// 长按
boolean longClickBottomRight()
// Long clicks bottom and right corner of the UI element
boolean longClickTopLeft()
// Long clicks on the top and left corner of the UI element
boolean performMultiPointerGesture(PointerCoords... touches)
// Performs a multi-touch gesture.
boolean performTwoPointerGesture(Point startPoint1, Point startPoint2, Point endPoint1, Point endPoint2, int steps)
// Generates a two-pointer gesture with arbitrary starting and ending points.
boolean pinchIn(int percent, int steps)
// Performs a two-pointer gesture, where each pointer moves diagonally toward the other, from the edges to the center of this UiObject .
boolean pinchOut(int percent, int steps)
// Performs a two-pointer gesture, where each pointer moves diagonally opposite across the other, from the center out towards the edges of the this UiObject.
boolean setText(String text)
// 设置输入内容
boolean swipeDown(int steps)
// Performs the swipe down action on the UiObject.
boolean swipeLeft(int steps)
// Performs the swipe left action on the UiObject.
boolean swipeRight(int steps)
// Performs the swipe right action on the UiObject.
boolean swipeUp(int steps)
// Performs the swipe up action on the UiObject.
boolean waitForExists(long timeout)
// Waits a specified length of time for a view to become visible.
boolean waitUntilGone(long timeout)
// Waits a specified length of time for a view to become undetectable.


下面给大家展示一个UiAutomator的testcase


package cn.cool;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import android.os.RemoteException;
public class Testone extends UiAutomatorTestCase {
   @Override
    protected void setUp() throws Exception {
        super.setUp();
    }
    public void testCool() throws RemoteException, InterruptedException {
    }
    @Override
    protected void tearDown() throws Exception {
    }
}


然后我们可以在实际的项目中,去实现我们真实的业务的代码。


实现完毕呢,

我们就可以用 adb . shell push cool.jar /data/local/tmp/

然后使用


adb shell uiautomator runtest cool.jar -c cn.cool.Testone


就可以执行了。uiautomator 由于只能打成jar包,所以它不能直接在编辑器上进行调试。

相关文章
|
2月前
|
监控 安全 Android开发
【新手必读】Airtest测试Android手机常见的设置问题
【新手必读】Airtest测试Android手机常见的设置问题
|
4月前
|
数据库 Android开发 开发者
Android Studio入门之内容共享ContentProvider讲解以及实现共享数据实战(附源码 超详细必看)
Android Studio入门之内容共享ContentProvider讲解以及实现共享数据实战(附源码 超详细必看)
36 0
|
4月前
|
存储 XML Android开发
Android Studio App开发入门之数据存储中共享参数SharedPreferneces的讲解及使用(附源码 超详细必看)
Android Studio App开发入门之数据存储中共享参数SharedPreferneces的讲解及使用(附源码 超详细必看)
29 0
|
4月前
|
Android开发
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
33 0
|
4月前
|
XML Java Android开发
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
80 0
|
1月前
|
测试技术 API 调度
【Android 从入门到出门】第七章:开始使用WorkManager
【Android 从入门到出门】第七章:开始使用WorkManager
19 3
【Android 从入门到出门】第七章:开始使用WorkManager
|
1月前
|
存储 Android开发 C++
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
30 3
|
1月前
|
Android开发
【Android 从入门到出门】第四章:现代Android开发中的导航
【Android 从入门到出门】第四章:现代Android开发中的导航
22 2
【Android 从入门到出门】第四章:现代Android开发中的导航
|
1月前
|
XML API Android开发
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
26 4
|
1月前
|
存储 XML 编译器
【Android 从入门到出门】第二章:使用声明式UI创建屏幕并探索组合原则
【Android 从入门到出门】第二章:使用声明式UI创建屏幕并探索组合原则
47 3