uiautomator 代码记录 : 随机发送短信

package sms_test;

import java.lang.*;
import java.util.Random;

import javax.microedition.khronos.egl.EGL10;

import java.io.File;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

import android.R.plurals;
import android.os.RemoteException;

import com.android.uiautomator.core.UiCollection;
import com.android.uiautomator.core.UiObjectNotFoundException;

public class SMS_Test extends UiAutomatorTestCase {
	String startsms = "com.android.mms/com.android.mms.ui.ConversationList";
	public void startapp (String ojb){
		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_SMS_Test() throws UiObjectNotFoundException,RemoteException{
		String a = "abcdefg";
		String b = "1234567";
		String c = "aaabbccddeeffgg112233445566778899";
		String d = "#@+=-)(4451318fdggd";
		String e = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadddddddddddddddddddddddd"
				+ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd";
		String a1 = "10086";
		String a2 = "10086101";
		String a3 = "10010";
		String SMS_content[] = {a,b,c,d,e};
		String Send_the_number[] ={a1,a2,a3};
		try{
		for(int i=0; i<10 ;i++){
		sleep(1000);
		UiDevice.getInstance().sleep();
		if(UiDevice.getInstance().isScreenOn()){
			System.out.println("The screen is on!");
		}else{
			UiDevice.getInstance().wakeUp();
			sleep(1000);
		}
		UiObject Unlocked = new UiObject(new UiSelector().resourceId("com.android.systemui:id/notification_stack_scroller"));
		assertTrue("Unlocked Exception!", Unlocked.exists());
		Unlocked.swipeUp(20);
		sleep(1000);
		startapp(startsms);//启动短信
		System.out.println("start apk....");
		UiObject sms_1 = new UiObject(new UiSelector().resourceId("android:id/list"));
		sms_1.waitForExists(5000);
		assertTrue("sms_1 Exception!", sms_1.exists());
		UiObject new_SMS = new UiObject(new UiSelector().resourceId("com.android.mms:id/action_compose_new").description("新信息"));
		assertTrue("new_SMS Exception!",new_SMS.exists());
		new_SMS.clickAndWaitForNewWindow(); 
		UiObject Enter_the_number = new UiObject(new UiSelector().text("输入名字或号码")
																 .resourceId("com.android.mms:id/recipients_editor"));
		assertTrue("Enter_the_number Exception!", Enter_the_number.exists());
		Enter_the_number.click();
		sleep(2000);
		Random r = new Random();
		int e1=r.nextInt(3);
		Enter_the_number.setText(Send_the_number[e1]); //输入发送号码
		UiObject Enter_the_content = new UiObject(new UiSelector().text("输入文本信息")
																  .resourceId("com.android.mms:id/embedded_text_editor"));
		assertTrue("Enter_the_content Exception!",Enter_the_content.exists());
		sleep(5000);
		Enter_the_content.click();
		sleep(2000);
		Random k = new Random();
		Enter_the_content.setText(SMS_content[k.nextInt(5)]); //输入信息内容
		sleep(5000);
		UiObject send = new UiObject(new UiSelector().resourceId("com.android.mms:id/send_button_sms"));
		assertTrue("send Exception!", send.exists());
		send.clickAndWaitForNewWindow();   
		UiObject CMCC = new UiObject(new UiSelector().text("中国移动")
													 .resourceId("android:id/title"));
		UiObject CUCC = new UiObject(new UiSelector().text("中国联通").resourceId("android:id/title"));
		assertTrue("CMCC Exception!...",CMCC.exists());
		assertTrue("CUCC Exception!...",CUCC.exists());
		if(a1.equals(Send_the_number[e1])){   //判断输入的号码是联通号码还是移动号码
			CMCC.clickAndWaitForNewWindow();
			System.out.println("CMCC send...");		
		}else if(a2.equals(Send_the_number[e1])){
			CMCC.clickAndWaitForNewWindow();
			System.out.println("CMCC send...");
		}else if(a3.equals(Send_the_number[e1])){
			CUCC.clickAndWaitForNewWindow();
			 System.out.println("CUCC send...");
		}
		sleep(1000);
		UiDevice.getInstance().pressBack();
		sleep(1000);
		UiDevice.getInstance().pressBack();
		sleep(1000);
		UiDevice.getInstance().pressBack();
		sleep(1000);
		UiObject Letter = new UiObject(new UiSelector().resourceId("com.android.mms:id/mark_as_read_btn").text("关闭"));
		boolean boolean_letter = Letter.waitForExists(30000);
		if(boolean_letter){
		Letter.clickAndWaitForNewWindow();
		}
		else {
			UiDevice.getInstance().pressBack();
		}
		UiDevice.getInstance().pressBack();
		sleep(1000);
		UiDevice.getInstance().pressBack();
		sleep(1000);
			}
		}catch(UiObjectNotFoundException e5){
			e5.printStackTrace();
			UiDevice.getInstance().pressBack();
			sleep(1000);
			UiDevice.getInstance().pressBack();
			sleep(1000);
			UiDevice.getInstance().pressBack();
			sleep(1000);
			UiDevice.getInstance().pressBack();
			sleep(1000);
			
		}
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值