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

时间:2022-08-24 16:41:09
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); }
}
}