1. 程式人生 > >uiautomator 程式碼記錄 : 隨機發送簡訊

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);
			
		}
	}
}