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


    判断对象是否存在

    1、判断对象是否存在相关API

    返回值 API 描述
    boolean waitForExists(long timeout) 等待对象出现
    boolean waitUntilGone(long timeout) 等待对象消失
    boolean exists() 检查对象是否存在

    2、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="testExists";
            androidId="1";
            new UiAutomatorHelper(jarName,testClass,testName,androidId);
    
        }
        
        public void testExists() throws UiObjectNotFoundException{
            UiDevice.getInstance().pressHome();
            sleep(2000);
            UiObject message=new UiObject(new UiSelector().text("Messaging"));
            message.click();
            sleep(2000);
            
            UiObject text=new UiObject(new UiSelector().text("No conversations."));
            if(text.exists()){
                System.out.println("No conversations, please create a message");
            }
            
            UiObject create=new UiObject(new UiSelector().resourceId("com.android.mms:id/action_compose_new"));
            if(create.waitForExists(5000)){
                create.click();
                UiObject to=new UiObject(new UiSelector().resourceId("com.android.mms:id/recipients_editor"));
                to.click();
                UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_1);
                UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_0);
                UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_0);
                UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_8);
                UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_6);
                UiObject typeMessage=new UiObject(new UiSelector().resourceId("com.android.mms:id/embedded_text_editor"));
                typeMessage.setText("hello, my name is fsw!");
                UiObject button=new UiObject(new UiSelector().resourceId("com.android.mms:id/send_button_sms"));
                button.click();
                sleep(2000);
            }
        }
    }
    Demo.java
  • 相关阅读:
    jemter 90%line的解释
    JMeter 不同线程组间变量传递(亲测 ok)
    nginx的url规则小tips
    redis水平扩展实践,完全配置,无需代码改动
    基于nginx + lua实现的反向代理动态更新
    基于redis 3.x搭建集群环境
    简单监控脚本细节分析
    简单的应用服务器性能监控
    Elasticsearch的脚本化数据导入导出
    A* search算法
  • 原文地址:https://www.cnblogs.com/fsw-blog/p/4578771.html
Copyright © 2020-2023  润新知