码迷,mamicode.com
首页 > 移动开发 > 详细

Android无线测试之—UiAutomator UiObject API介绍六

时间:2015-06-15 18:21:56      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:

手势操作

1、手势相关操作技术分享

2、相关API介绍

返回值 API 描述
boolean performMultiPointerGesture(PointerCoords[]... touches) 执行单手触控手势,可定义任意手势与形状
boolean performTwoPointerGesture(Point startPoint1, Point startPoint2, Point endPoint1, Point endPoint2, int steps) 执行任意两个手指触控手势,模拟两个手指手势
boolean pinchIn(int percent, int steps) 手势操作,两点向内收缩
boolean pinchOut(int percent, int steps) 手势操作,两点向外扩张

 3、API应用举例

技术分享
package com.test.uiobject;

import java.io.File;

import android.graphics.Rect;
import android.view.KeyEvent;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class Demo extends UiAutomatorTestCase {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String jarName,testClass,testName,androidId;
        jarName="demo";
        testClass="com.test.uiobject.Demo";
        testName="testGesTure";
        androidId="1";
        new UiAutomatorHelper(jarName,testClass,testName,androidId);

    }
    
    public void testGesTure() throws UiObjectNotFoundException{
        //返回home页
        UiDevice.getInstance().pressHome();
        sleep(2000);    
        
        //打开浏览器
        UiObject browser=new UiObject(new UiSelector().text("Browser"));
        browser.click();
        sleep(2000);
        
        //将光标移动到URL输入框行尾,点击backspace键删除里面的URL
        UiObject editText=new UiObject(new UiSelector().className("android.widget.EditText"));
        editText.click();
        sleep(1000);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_MOVE_END);
        UiObject url=new UiObject(new UiSelector().text("Search or type URL"));
        while(!url.exists()){
            UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_DEL);
        }
        sleep(2000);
        
        //输入www.baidu.com 点击enter打开百度首页
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_W);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_W);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_W);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_PERIOD);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_I);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_D);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_U);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_PERIOD);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_C);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_O);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_M);
        sleep(500);
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_ENTER);
        sleep(10000);
        
        //截屏,保存
        UiDevice.getInstance().takeScreenshot(new File("sdcard/image1.png"));
        sleep(2000);
        
        //进入图片所在的位置
        UiDevice.getInstance().pressHome();
        sleep(2000);
        UiObject apps=new UiObject(new UiSelector().descriptionContains("Apps"));
        apps.clickAndWaitForNewWindow();
        UiObject fileManager=new UiObject(new UiSelector().text("File Manager"));
        fileManager.clickAndWaitForNewWindow();
        UiObject listView=new UiObject(new UiSelector().text("image1.png"));
        listView.clickAndWaitForNewWindow();
        
        //放大缩小图片
        UiObject image=new UiObject(new UiSelector().resourceId("com.android.gallery:id/image"));
        image.pinchIn(80, 50);
        image.pinchOut(80, 50);    
    }

}
Demo.java

 

Android无线测试之—UiAutomator UiObject API介绍六

标签:

原文地址:http://www.cnblogs.com/fsw-blog/p/4577170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!