zoukankan      html  css  js  c++  java
  • UiAutomator 代码记录: 随机创建新联系人

    package lecturer;
    
    import java.lang.*;
    import java.nio.Buffer;
    import java.util.Random;
    import java.io.File;
    import com.android.uiautomator.testrunner.UiAutomatorTestCase;
    import android.R.string;
    import android.os.RemoteException;
    import com.android.uiautomator.core.UiObject;
    import com.android.uiautomator.core.UiDevice;
    import com.android.uiautomator.core.UiSelector;
    import com.android.uiautomator.core.UiScrollable;
    import com.android.uiautomator.core.UiObjectNotFoundException;
    
    
    public class Lecturer extends UiAutomatorTestCase {
    	public String Random_text(){ //生成随机手机字符串方法
    		String a[] = {"a","b","c","d","e","f","g","h","w","j","k","l","n","m","o","p","q","r","s","t","u","v","w","x","Y","z"}; 
    		String A[] = {"A","B","C","D","E","F","G","H","W","J","K","L","N","M","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    		String symbol[] = {"!","@","#","$","%","^","&","*","(",")","+","=","-",";","'","?","~","|","[","]","{","}","`"};
    		String digital[] = {"1","2","3","4","5","6","7","8","9","0"};
    		String set[][] = {a,A,symbol,digital};
    		Random length = new Random();
    		StringBuffer buffer = new StringBuffer();
    		int length_a = length.nextInt(21)+1;
    		for(int i=0;i<length_a;i++){
    			Random random = new Random();
    			Random random2 = new Random();	 
    			int select = random.nextInt(4);
    			if (set[select].equals(a)){				
    				buffer.append(set[select][random2.nextInt(26)]);
    			}
    			else if(set[select].equals(A)){			
    				buffer.append(set[select][random2.nextInt(26)]);
    			}
    			else if(set[select].equals(symbol)){
    				buffer.append(set[select][random2.nextInt(23)]);
    			}
    			else if(set[select].equals(digital)){
    				buffer.append(set[select][random2.nextInt(10)]);
    			}			  
    		}
    		return buffer.toString();
    	}
    	public String Random_Value(){ //生成随机数值的方法
    		String Value[] = {"1","2","3","4","5","6","7","8","9","0"};
    		StringBuffer Value_buffe =new StringBuffer();
    		for(int j=0; j<12;j++){
    			Random Value_Random = new Random();
    			Value_buffe.append(Value[Value_Random.nextInt(10)]);
    		}
    		return Value_buffe.toString();
    	}
    	public void Screenshot(String name) { //截取当前屏幕图片的方法
    		File file = new File("/mnt/sdcard/log"+name+".png");
    		UiDevice.getInstance().takeScreenshot(file);
    	}
    	public void startapp(String ojb){ //启动apk的方法
    		StringBuffer app = new StringBuffer();
    		app.append("am  start -n");
    		app.append(ojb);
    		try{
    		Process process = Runtime.getRuntime().exec(app.toString());
    		process.waitFor();
    		}
    		catch (Exception e) {
    			e.printStackTrace();
    			System.out.println("NG! Start apk failed....");
    		}		
    	}
    	public void test_lecturer()throws UiObjectNotFoundException,RemoteException {
    		for(int k=0;k<20;k++){
    		try{
    		UiDevice.getInstance().pressBack();
    		sleep(1000);
    		UiDevice.getInstance().sleep();
    		sleep(2000);
    		UiDevice.getInstance().wakeUp();
    		}catch(RemoteException e1) {
    			e1.printStackTrace();
    			System.out.println("NO:"+k+"....wakeup Exception!.... ");
    			sleep(2000);
    			UiDevice.getInstance().sleep();
    			sleep(2000);
    			UiDevice.getInstance().wakeUp();
    		}
    		try{
    		UiObject Unlock = new UiObject(new UiSelector().resourceId("com.android.systemui:id/preview_container")
    							       .className("android.widget.FrameLayout"));
    		assertTrue("Unlock fail!...", Unlock.exists());
    		Unlock.swipeUp(20);//滑屏解锁
    		sleep(1000);
    		}catch(UiObjectNotFoundException e2){
    			e2.printStackTrace();
    			System.out.println("NO:"+k+"....Unlock Exception!.... ");
    			Screenshot("Unlock_Exception"+k);
    			sleep(2000);
    			UiDevice.getInstance().sleep();
    			sleep(2000);
    			UiDevice.getInstance().wakeUp();
    			sleep(2000);
    			UiObject Unlock = new UiObject(new UiSelector().resourceId("com.android.systemui:id/preview_container")
    					   .className("android.widget.FrameLayout"));
    			assertTrue("Unlock fail!...", Unlock.exists());
    			Unlock.swipeUp(20);//滑屏解锁		
    		}
    		startapp("com.android.contacts/com.android.contacts.activities.PeopleActivity");//启动联系人
    		sleep(2000);
    		try{
    		UiObject new_contact = new UiObject(new UiSelector().resourceId("com.android.contacts:id/floating_action_button")
    								    .description("添加新联系人"));
    		assertTrue("new_contact fail!...",new_contact.exists());
    		new_contact.clickAndWaitForNewWindow();
    		}catch (UiObjectNotFoundException e3) {
    			e3.printStackTrace();
    			System.out.println("NO:"+k+"....new_contact Exception!.... ");
    			Screenshot("new_contact"+k);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    		}
    		try{
    		UiObject Name = new UiObject(new UiSelector().text("姓名").className("android.widget.EditText"));
    		assertTrue("Name fail!...", Name.exists());
    		Name.click();
    		Name.setText(Random_text());
    		sleep(1000);
    		}catch (UiObjectNotFoundException e4) {
    			e4.printStackTrace();
    			System.out.println("NO:"+k+"....Enter_the_contact_name Exception!.... ");
    			Screenshot("Enter_the_contact_name"+k);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    		}
    		try{
    		UiObject Pinyin_of_name = new UiObject(new UiSelector().text("姓名拼音").className("android.widget.EditText"));
    		assertTrue("Pinyin_of_name fail!...",Pinyin_of_name.exists());
    		Pinyin_of_name.click();
    		Pinyin_of_name.setText(Random_text());
    		sleep(1000);
    		}catch (UiObjectNotFoundException e5) {
    			e5.printStackTrace();
    			System.out.println("NO:"+k+"....Enter the name Pinyin Exception!.... ");
    			Screenshot("Pinyin_of_name"+k);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    		}
    		try{
    		UiObject nickname = new UiObject(new UiSelector().text("昵称").className("android.widget.EditText"));
    		assertTrue("nickname fail!...",nickname.exists());
    		nickname.click();
    		nickname.setText(Random_text());
    		sleep(1000);
    		}catch (UiObjectNotFoundException e6) {
    			e6.printStackTrace();
    			System.out.println("NO:"+k+"....Enter the nickname Exception!.... ");
    			Screenshot("nickname"+k);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    		}
    		try{
    		UiScrollable Scroll_contact = new UiScrollable(new UiSelector().resourceId("com.android.contacts:id/contact_editor_fragment")
    									       .className("android.widget.ScrollView")); //定位滚动元素
    		assertTrue("Scroll_contact fail!...",Scroll_contact.exists());
    		UiObject number = new UiObject(new UiSelector().text("电话").className("android.widget.EditText"));
    		Scroll_contact.scrollIntoView(number);
    		sleep(1000);
    		number.click();
    		number.setText(Random_Value());
    		sleep(1000);
    		}catch(UiObjectNotFoundException e7){
    			e7.printStackTrace();
    			System.out.println("NO:"+k+"....Scroll and enter the number Exception!.... ");
    			Screenshot("number"+k);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    		}
    		try{
    		UiObject determine = new UiObject(new UiSelector().resourceId("com.android.contacts:id/menu_done")
    							          .className("android.widget.TextView"));
    		assertTrue("determine fail! ...",determine.exists());
    		determine.clickAndWaitForNewWindow();
    		}catch (UiObjectNotFoundException e8) {
    			e8.printStackTrace();
    			System.out.println("NO:"+k+"....determine Exception!.... ");
    			Screenshot("determine"+k);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    			UiDevice.getInstance().pressBack();
    			sleep(1000);
    		}
    		sleep(1000);
    		Screenshot("Screenshot_contacts"+k);
    		sleep(2000);
    	    UiDevice.getInstance().pressBack();
    	    sleep(1000);
    	    UiDevice.getInstance().pressBack();
    	    sleep(1000);
    	    UiDevice.getInstance().pressBack();
    	    sleep(1000);
    		}
    	}
    	
    }
    

  • 相关阅读:
    fedora上部署ASP.NET——(卡带式电脑跑.NET WEB服务器)
    SQL Server 请求失败或服务未及时响应。有关详细信息,请参见事件日志或其它适合的错误日志
    8086CPU的出栈(pop)和入栈(push) 都是以字为单位进行的
    FTP 服务搭建后不能访问问题解决
    指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配
    Linux 安装MongoDB 并设置防火墙,使用远程客户端访问
    svn Please execute the 'Cleanup' command. 问题解决
    .net 操作MongoDB 基础
    oracle 使用绑定变量极大的提升性能
    尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/9708682.html
Copyright © 2011-2022 走看看