zoukankan      html  css  js  c++  java
  • UiAutomator 代码记录 : 遍历桌面

    package test_one;
    
    import java.lang.*;
    import java.io.File;
    import com.android.uiautomator.core.UiDevice;
    import com.android.uiautomator.core.UiScrollable;
    import com.android.uiautomator.core.UiSelector;
    import com.android.uiautomator.core.UiObject;
    import com.android.uiautomator.testrunner.UiAutomatorTestCase;
    
    import android.R.id;
    import android.R.string;
    import android.os.RemoteException;
    
    import com.android.uiautomator.core.UiWatcher;
    import com.android.uiautomator.core.UiObjectNotFoundException;
    
    @SuppressWarnings("deprecation")
    public class Test_ojb extends UiAutomatorTestCase{
    	public int startapp(String componentName){
    		StringBuffer app = new StringBuffer();
    		app.append("am start -n");
    		app.append(componentName);
    		int ret = 1;
    		try{
    			Process process = Runtime.getRuntime().exec(app.toString());
    			ret = process.waitFor();
    			
    		}catch(Exception e){
    			e.printStackTrace();
    			System.out.println("startapp Exception!!! ");
    			
    		}
    		return ret;
    	}
    
    	 public void test_one_Test()throws UiObjectNotFoundException,RemoteException{		 
    		for(int j=0; j<10; j++){
    		if (UiDevice.getInstance().isScreenOn()) {
    			System.out.println("screen OK!");
    			
    		}else{
    			UiDevice.getInstance().wakeUp();
    			System.out.println("The screen is lit ! ");
    		}
    		UiDevice.getInstance().pressHome();
    		UiObject Unlocked = new UiObject(new UiSelector().resourceId("com.android.systemui:id/notification_stack_scroller"));
    		assertTrue("Unlocked NG!",Unlocked.exists());
    		try {
    			Unlocked.swipeUp(20);
    			System.out.println("OK! The desktop unlocked successfully");
    		} catch (Exception e) {
    			e.printStackTrace();
    			System.out.println("NG! Unlock failed!");
    		}
    		UiObject launcher1 = new UiObject(new UiSelector().resourceId("com.android.launcher3:id/layout")
    								  .className("android.view.View")
    								  .packageName("com.android.launcher3"));
    		UiObject launcher2 = launcher1.getChild(new UiSelector().index(0));
    		UiObject application = launcher2.getChild(new UiSelector().index(2)																
    									  .className("android.widget.TextView")
    									  .packageName("com.android.launcher3"));												
    		assertTrue("application NG!",application.exists());
    		try{
    			application.clickAndWaitForNewWindow();//点击进入应用
    			System.out.println("application click ok!");
    		} catch (UiObjectNotFoundException e){
    			e.printStackTrace();
    			System.out.println("NG! Application click failed! ");
    			UiDevice.getInstance().pressHome();
    			sleep(2000);
    			application.clickAndWaitForNewWindow();
    		}
    		UiObject View_1 = new UiObject(new UiSelector().resourceId("com.android.launcher3:id/apps_customize_pane_content")
    				.className("android.view.View"));
    		UiObject view_2 = View_1.getChild(new UiSelector().className("android.view.View").index(0));
    		UiObject view_3 = view_2.getChild(new UiSelector().className("android.view.View").index(2));
    		for(int i=0;i<view_3.getChildCount();i++){ //遍历桌面
    			try{
    			UiObject obj = view_3.getChild(new UiSelector().className("android.widget.TextView").index(i));
    			assertTrue("NG!"+i+"could not find it", obj.exists());
    			obj.click();
    			System.out.println(i+"start OK!");
    			sleep(5000);
    			UiDevice.getInstance().pressBack();
    			if(view_3.waitForExists(10000)){
    				System.out.println("return!");
    			}else{
    				UiDevice.getInstance().pressBack();
    			}		
    			sleep(1000);
    			}catch (UiObjectNotFoundException e) {
    				e.printStackTrace();
    				System.out.println("NO:"+i+"_NG!");	
    				File Screenshot = new File("/mnt/sdcard/"+"log_"+i+".png");
    				UiDevice.getInstance().takeScreenshot(Screenshot);
    				UiDevice.getInstance().pressHome();
    				sleep(2000);
    				application.clickAndWaitForNewWindow();
    			}
    		}
    			UiDevice.getInstance().pressHome();
    			sleep(1000);
    			UiDevice.getInstance().sleep();
    		 }
    	 }
    }
    


    
                
    
  • 相关阅读:
    一些有关时间的获取
    Devexpress xaf用代码打开菜单(Navigation Item)
    Devexpress xaf BO中字段为RuleRequiredField必输字段时,文本标签默认添加*标记
    Devepxress xaf Dashboard中DetailView控件使其可编辑
    Devexpress WinForm TreeList的三种数据绑定方式(DataSource绑定、AppendNode添加节点、VirtualTreeGetChildNodes(虚拟树加载模式))
    Devexpress xaf针对某个用户登录后在面板中设置导航无效的解决方法
    IIS不能下载config配置文件的解决方法
    DevExpress WPF控件记录
    Devexpress WinForm GridControl实现单元格可编辑状态更改
    Devexpress GridControl中 repositoryItemCheckEdit作为选择列以及作为显示列的使用方法
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/9708683.html
Copyright © 2011-2022 走看看